1

I'm trying to feed an mp4 file to flash player via php and the video is downloaded completely before starting playback.

$src = '/var/www/user/data/www/domain.com/video.mp4';
if(file_exists($src) and is_readable($src)) {
    header('Content-Type: video/mp4');
    header('Content-Length: '.filesize($src));
    readfile($src);
} else die('error');

I've tried curl with similar results. Any ideas what's causing this delay?

Chris
  • 13
  • 4
  • When you say "similar results" do you mean that cURL takes ~20 seconds before returning any output as well? – Peter Sobot Oct 02 '12 at 18:02
  • Yes, the video is downloaded before it starts playback. If I open the mp4 file directly in firefox, it's being played in it's built-in video player right away. This doesn't happen when opening the php directly (100% download before it's displayed) – Chris Oct 02 '12 at 20:16

2 Answers2

2

Most likely your Flash player is hoping you'll handle HTTP Range requests so it can get started faster on the playback.

The HTML5/Flash audio player jPlayer has a section in their developer guide about this. Scroll to the part about Byte-Range Requests:

Your server must enable Range requests. This is easy to check for by seeing if your server's response includes the Accept-Ranges in its header.

Also note that they offer a PHP solution for handling Range requests if you have to use PHP instead of a direct download.

smartReadFile.php
https://groups.google.com/forum/#!msg/jplayer/nSM2UmnSKKA/bC-l3k0pCPMJ

jimp
  • 16,999
  • 3
  • 27
  • 36
  • Thanks for pointing me the right direction, but the video still loads fully before starting playback. – Chris Oct 02 '12 at 20:01
  • My headers contain: HTTP/1.1 206 Partial Content Cache-Control: public, must-revalidate, max-age=0 Pragma: no-cache Accept-Ranges: bytes Content-Length: 9634096 Content-Range: bytes 2671042-12305137/12305138 Content-Disposition: inline; filename=video.mp4 content-transfer-encoding: binary Last-Modified: Tue, 02 Oct 2012 18:12:28 +0100 Keep-Alive: timeout=15, max=98 Connection: Keep-Alive Content-Type: video/mp4 – Chris Oct 02 '12 at 20:04
  • 1
    Turns out it was a metadata issue with the mp4 file as I tried a different one and it worked! I've implemented the smartReadFile function you suggested, so that's settled. Many thanks! – Chris Oct 02 '12 at 21:13
  • I was just reading your comments. I'm glad you got it working! – jimp Oct 02 '12 at 22:33
0

Another option would be to just have apache send the file it self as opposed to reading it in php and dumping it to the output using X-Sendfile.

First make sure apache is compiled with sendfile support then alter your output code to be:

header ('X-Sendfile: ' . $src);
header ('Content-Type:  video/mp4');
header ('Content-Disposition: attachment; filename="' . $filename . '"');
exit;

This is normally faster than doing it via PHP.

Doon
  • 19,719
  • 3
  • 40
  • 44