PHP: iPad does not play MP4 videos delivered by PHP, but if accessed directly it does

前端 未结 3 1719
花落未央
花落未央 2020-11-30 10:31

I am trying to find a solution for this problem for some days already, I tried all advices I could find here on stackoverflow and other platforms. And still, there is no sol

3条回答
  •  独厮守ぢ
    2020-11-30 10:57

    Thanks for your contribution, very important... But even your code didn't make it for my iphone.

    Even if I still don't know why, the following code worked for me. Probably for the line:

    header('HTTP/1.1 416 Requested Range Not Satisfiable');
    

    I got this from here: https://github.com/tikiatua/internal-assets-plugin/issues/9

    $fp = fopen($filepath, "rb");
        $size = filesize($filepath);
        $length = $size;
        $start = 0;
        $end = $size - 1;
        header('Content-type: video/mp4');
        header("Accept-Ranges: 0-$length");
        if (isset($_SERVER['HTTP_RANGE'])) {
            $c_start = $start;
            $c_end = $end;
            list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
    
            if (strpos($range, ',') !== false) {
                header('HTTP/1.1 416 Requested Range Not Satisfiable');
                header("Content-Range: bytes $start-$end/$size");
                exit;
            }
    
            if ($range == '-') {
                $c_start = $size - substr($range, 1);
            } else {
                $range = explode('-', $range);
                $c_start = $range[0];
                $c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size;
            }
    
            $c_end = ($c_end > $end) ? $end : $c_end;
    
            if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
                header('HTTP/1.1 416 Requested Range Not Satisfiable');
                header("Content-Range: bytes $start-$end/$size");
                exit;
            }
    
            $start = $c_start;
            $end = $c_end;
            $length = $end - $start + 1;
            fseek($fp, $start);
            header('HTTP/1.1 206 Partial Content');
        }
    
        header("Content-Range: bytes $start-$end/$size");
        header("Content-Length: ".$length);
    
        $buffer = 1024 * 8;
    
        while(!feof($fp) && ($p = ftell($fp)) <= $end) {
            if ($p + $buffer > $end) {
                $buffer = $end - $p + 1;
            }
            set_time_limit(0);
            echo fread($fp, $buffer);
            flush();
        }
    
        fclose($fp);
        exit;
    

提交回复
热议问题