Created
May 22, 2017 15:23
-
-
Save mems/b121bba11adc48dfd068bb7a8d113a8c to your computer and use it in GitHub Desktop.
Chunked transport encoding video
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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