PHP output MP4 video streaming function

不言
Release: 2023-03-24 20:50:02
Original
8171 people have browsed it

The content of this article is about PHP output MP4 video streaming function, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

function GetMp4File($file) { 
    $size = filesize($file); 
    header("Content-type: video/mp4"); 
    header("Accept-Ranges: bytes"); 
    if(isset($_SERVER['HTTP_RANGE'])){ 
        header("HTTP/1.1 206 Partial Content"); 
        list($name, $range) = explode("=", $_SERVER['HTTP_RANGE']); 
        list($begin, $end) =explode("-", $range); 
        if($end == 0){ 
            $end = $size - 1; 
        } 
    }else { 
        $begin = 0; $end = $size - 1; 
    } 
    header("Content-Length: " . ($end - $begin + 1)); 
    header("Content-Disposition: filename=".basename($file)); 
    header("Content-Range: bytes ".$begin."-".$end."/".$size); 
    $fp = fopen($file, 'rb'); 
    fseek($fp, $begin); 
    while(!feof($fp)) { 
        $p = min(1024, $end - $begin + 1); 
        $begin += $p; 
        echo fread($fp, $p); 
    } 
    fclose($fp); 
} 
GetMp4File("demo.mp4");
Copy after login

Related recommendations:

php output formatted string function printf()


The above is the detailed content of PHP output MP4 video streaming function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template