Why is PHP interfering with my HTML5 MP4 video?

后端 未结 3 1945
梦谈多话
梦谈多话 2021-01-03 08:22

I\'m writing a web app that serves H.264 encoded MP4 video. In Chrome and Safari, it does this via an HTML5 video tag.

In order to control access to these videos, th

相关标签:
3条回答
  • 2021-01-03 08:32

    When streaming files to HTML5 Embedded video player you still have to add headers that inform the player with information about the video.

    you can't just expect to run a read readfile() command and things will magically work, sorry bud, but programming is not that easy. (Wish it was).

    heres a small application you can use to stream properly or just learn from.

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

    0 讨论(0)
  • 2021-01-03 08:47

    Maybe. Try adding also the content length header:

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

    If this still doesn't work, check for any output before readfile (echo's or whitespace before <?php). Check also that you don't have whitespace after ?> or simply omit ?> (it's not mandatory if you have nothing after).

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

    if (empty($_SERVER["HTTP_RANGE"])) {
        //do your current stuff...
    }
    else { //violes rfc2616, which requires ignoring  the header if it's 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
    }
    
    0 讨论(0)
  • 2021-01-03 08:54

    See comments!

    Using readfile is not recommended for streaming video files since it loads the whole file into memory before outputting. This causes serious problems with memory running out.

    Try reading and outputting the file chunk by chunk.

    0 讨论(0)
提交回复
热议问题