I am trying to output an mp4 video file through PHP. When it is used through a flash player (eg. flowplayer) it is working great. But when I'm trying to use it as a source on an html5 video tag or to call directly the php file, it doesn't work.
The code I use is the following:
$filesize = filesize($file);
header("Content-Type: video/mp4");
if ( empty($_SERVER['HTTP_RANGE']) )
{
header("Content-Length: $filesize");
readfile($file);
}
else //violes rfc2616, which requires ignoring the header if it's invalid
{
rangeDownload($file);
}
and rangeDownload
function is from http://mobiforge.com/developing/story/content-delivery-mobile-devices Appendix A.
Even when I use a Content-Range
header (Content-Range:bytes 0-31596111/31596112
), it stucks on downloading 30.13 MB of the video.
Finally I've found a way to make it work
header("Content-Type: $mediatype");
if ( empty($_SERVER['HTTP_RANGE']) )
{
header("Content-Length: $filesize");
$fh = fopen($file, "rb") or die("Could not open file: " .$file);
# output file
while(!feof($fh))
{
# output file without bandwidth limiting
echo fread($fh, $filesize);
}
fclose($fh);
}
else //violes rfc2616, which requires ignoring the header if it's invalid
{
rangeDownload($file);
}
It is working in direct link of the php file and inside html5 video tag.
But in order to work in Flowplayer (and maybe in other flash/html5 players) you need to add a mp4 extension (eg. view.php?id=XXX&file=type.mp4)
This could have to do with your browser and what plugin it uses to view video files ie) quicktime. The reason it works with Flash is flash handles buffering and time sync and such. It is usually not recommended to let the browser handle playing media files, because it completely depends on the browser configuration and the plugins they have installed.
Some browsers automatically download media files, it's completely configurable by browser and end user.
来源:https://stackoverflow.com/questions/8348838/mp4-file-through-php-not-playing-as-html5-video