Question

I have a simple a html5 video tag pointing to a mp4 video through php. My problem is that the video starts playing only after it has been completely loaded by the browser.

How should I output video with php? I have used http ranges headers but nothing.

How should I configure apache so that can start playing the video at once (once it has downloaded only a part) ? is there any module should I enable?

html code:

<video width="640" height="264" preload="none" controls="" class="video-js vjs-default-skin" id="approval-playing-video"> 
   <source type="video/mp4" src="dam/index/get-video-preview?id=186&amp;md5=3386d3701e403c69e1c4323168f6ee74"></source> 
</video>

Php (ZF2 framework) server code:

    $response = new Stream();
    $response->setStream(fopen($file, 'r'));
    $response->setStatusCode(200);
    $response->setStreamName(basename($file));

    $headers = new Headers();
    $headers->addHeaders(array(
        'Content-Type'      => 'video/mp4',
        'Accept-Ranges'     => 'bytes',
        'Content-Length'    => filesize($file)
    ));

    return $response->setHeaders($headers)->setContent($file);

System test: Centos, Apache 2, PHP 5.4 Test with Firefox 24

Any suggestion will be really helpfull.

Was it helpful?

Solution

First you need to make sure your server can play a video properly formatted for "fast start":

You can download the file at http://vjs.zencdn.net/v/oceans.mp4 and host it on your server. If it works fine (ie video starts before full download) then your server/script config is ok.

If the above works the issue is probably that your mp4 is not properly formatted. You can try to use handbrake or MP4Box to repack your file. You need to activate the "web optimized" option (aka fast start) with handbrake.

For MP4Box (linux): MP4Box -inter 500 yourFile.mp4

More info on this here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top