mp4 file through php not playing as html5 video
Asked Answered
A

2

0

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.

Alas answered 1/12, 2011 at 21:55 Comment(4)
So, is your php.ini max_execution_time longer than the video? Did you probe for other header differences (Firebug) between static and readfiled file?Towers
I suggest to check the maximum memory settings in php.ini - seems like both functions you use to send video to client are reading whole file into memory and then sending it to user, you probably need to read and send it using small chunks (e.g. 2MB).Kinakinabalu
mp4 is not recommended for html5 video. look here for a chart of which browsers currently support which video file types for html5 video. en.wikipedia.org/wiki/HTML5_videoHebetate
@Towers I've set 'max_execution_time' to 0 but nothing changed. Alex Z: memory_limit is at 250M, I believe it's more than enoughAlas
A
1

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)

Alas answered 3/12, 2011 at 11:30 Comment(0)
Q
0

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.

Quickstep answered 1/12, 2011 at 22:28 Comment(1)
Thank you for the advice Matthew, but the mp4 video is playing in Safari/Chrome perfectly when accessed directly. The problem is when I wrap it for security reasons in a PHP file.Alas

© 2022 - 2024 — McMap. All rights reserved.