Skip to content

Instantly share code, notes, and snippets.

@mems
Created May 22, 2017 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mems/b121bba11adc48dfd068bb7a8d113a8c to your computer and use it in GitHub Desktop.
Save mems/b121bba11adc48dfd068bb7a8d113a8c to your computer and use it in GitHub Desktop.
Chunked transport encoding video
<?php
if(isset($_GET['videofile'])){
// PHP encode transfer automatically if you use flush() and output_buffering is activated (don't need to use special header nor chunk metadata)
// http://php.net/manual/en/outcontrol.configuration.php#ini.output-buffering
header('Transfer-Encoding: chunked');
header('Content-Encoding: none');
// No cache
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Expires: 0');
// Metadata
header('Content-Type: video/mp4');
$file = 'video.mp4';
$handle = fopen($file, 'rb');
// Send your content in chunks
while (!feof($handle)) {
$chunk = fread($handle, 10000);
echo dechex(strlen($chunk)) . "\r\n";
echo $chunk;
echo "\r\n";
flush();// Note: some browsers have a buffer of 4096 bytes
usleep(200000);
}
// 10KB/200ms -> 50KB/s
fclose($handle);
// last chunk
echo "0\r\n\r\n";
flush();
exit();
}
?><!DOCTYPE html>
<html>
<head>
<title>Test chunked content encoding</title>
</head>
<body>
<video src="?videofile" type="video/mp4" controls></video>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment