Why does PHP interfere with my HTML5 MP4 video?

I am writing a web application that serves H.264 encoded MP4 video. In Chrome and Safari, it does this with an HTML5 tag.

To control access to these videos, their content is served through PHP using a really simple mechanism:

header('Content-type: video/mp4');
readfile($filename);
exit;

No matter what I do, the videos will not be broadcast. Additionally:

  • If I changed the source code for direct access to files using the same video tag, but referring to a copy of the video processed by Apache without PHP pass-through, streaming works fine.
  • Even when streaming does not work, I can always right-click on the gray HTML5 player and upload the file through the PHP pass-through - and it works fine offline.

Any ideas? I am pulling my hair out!

+3
source share
3 answers

May be. Try adding the content length header as well:

header('Content-length: '.filesize($filename));

If this still does not work, check for any output before readfile( echoor spaces before <?php). Check also that after ?>you have no spaces or just omit it ?>(this is not necessary if nothing happens after that).

As Bruno mentioned, to support streaming you also need to obey the header Range. Here's a simplified example that only considers the left border:

if (empty($_SERVER["HTTP_RANGE"])) {
    //do your current stuff...
}
else { //violes rfc2616, which requires ignoring  the header if it invalid
    preg_match("/^bytes=(\d+)-/i",$_SERVER["HTTP_RANGE"], $matches);
         $offset = (int) $matches[1];
    if ($offset < $filesize && $offset >= 0) {
        if (@fseek($fp, $offset, SEEK_SET) != 0)
            die("err");
        header("HTTP/1.1 206 Partial Content");
        header("Content-Range: bytes $offset-".($filesize - 1)."/$filesize");
    }
    else {
        header("HTTP/1.1 416 Requested Range Not Satisfiable");
        die();
    }
        //fread in loop here
}
+7
source

See comments!

readfile , . .

.

0

HTML5 , .

readfile(), , , . ( ).

heres , .

http://stream.xmoov.com/download/xmoov-php/

0

Source: https://habr.com/ru/post/1754085/


All Articles