Why is PHP interfering with my HTML5 MP4 video?

后端 未结 3 1944
梦谈多话
梦谈多话 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: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 ). 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
    }
    

提交回复
热议问题