简体   繁体   中英

How to make live streaming using php?

I have live-streaming link where its format extension is .m3u8. and I want it to be live in to my page. I tried this code but it does'nt work

<?php
$file = 'http://93.87.85.70/PLTV/88888888/224/3221226661/04.m3u8';
$fp = @fopen($file, 'rb');
$size   = filesize($file); // File size
$length = $size;           // Content length
$start  = 0;               // Start byte
$end    = $size - 1;       // End byte
header('Content-type: video/mp4');
//header("Accept-Ranges: 0-$length");
header("Accept-Ranges: bytes");
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();
?>

Is there something wrong or I am needed to add something there? Or if this code is wrong what another method am gonna use.

.m3u8 is a playlist file, not an MP4 video. It's commonly used with HLS streams.

An HLS stream is made up of a whole collection of files. There will be audio/video file segments every several seconds, possibly at several bitrates, with the playlist. The playlist is updated regularly.

Even if you did want to proxy these files, your script is not the way to do it. Best to leave it up to the web server (Nginx, Apache, whatever you're using), as that's what it does best. Your script isn't respecting the upstream content type, headers, etc. It's also ignoring any errors on fopen() . I wouldn't use fopen() on a URL anyway... the wrappers aren't always enabled, and then you don't have access to the real status codes and headers. And finally, you've hardcoded the upstream path so your users would only get the playlist and not the media files.

All you have to do is use a client-side player than handles HLS, like JWPlayer or similar. This has nothing to do with PHP. It's done with JavaScript and the Media Source Extensions API.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM