quasarstream / PHP-FFmpeg-video-streaming

📼 Package media content for online streaming(DASH and HLS) using FFmpeg
https://www.quasarstream.com/op/php/ffmpeg-streaming?u=php-ff
MIT License
514 stars 117 forks source link

Cannot transcode Video only files (DASH) #46

Closed userlip closed 4 years ago

userlip commented 4 years ago

Hey I tried transcode an MP4 file with no audio to HLS and DASH. Transcoding to HLS worked fine but it does not work to DASH.

I tried the following Adaptions: ->setAdaption('id=0,streams=v') ->setAdaption('id=0,streams=v id=1,streams=a')

Both failed.

I also tried to Transcode to HLS first, upload it and then try to do the HLS to DASH transcoding but that threw an error of ("Invalid Kilobit rate")

To Reproduce Try to Transcode an MP4 file that has no sound to DASH

Desktop/Server (please complete the following information):

aminyazdanpanah commented 4 years ago

I do not know exactly what happened to your stream but I guess you used the HEVC format for DASH packaging and tried to play it on the web. You should note that HEVC format is not playable on the web player. Instead, you should use X264.

Use the following code to create an MPD manifest file and segment files and also publish a master playlist repeatedly every after the specified number of segment intervals.

$format = new Streaming\Format\X264();
$format->on('progress', function ($video, $format, $percentage){
    echo sprintf("\rTranscoding...(%s%%) [%s%s]", $percentage, str_repeat('#', $percentage), str_repeat('-', (100 - $percentage)));
});

$log = new Logger('FFmpeg_Streaming');
$log->pushHandler(new StreamHandler('ffmpeg-streaming.log')); // path to log file

$ffmpeg = Streaming\FFMpeg::create(['timeout' => 0], $log);

$video = $ffmpeg->open('/var/www/html/videos/vid.avi');

$video->dash()
    ->setFormat($format)
    ->generateHlsPlaylist()
    ->autoGenerateRepresentations([360, 480])
    ->save('/var/www/html/packaged/stream.mpd');

Play your streams using hls.js and dash.js:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Streaming</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="shortcut icon" href="logo.png">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/dashjs/3.1.0/dash.all.debug.js"></script>
    </head>
    <body>
        <div class="container" >
            <div id="hls-player">
                <video id="hls-video" width="100%" controls autoplay poster="poster.jpg"></video>
            </div>
            <div id="dash-player">
                <video id="dash-video" width="100%" controls autoplay poster="poster.jpg"></video>
            </div>
        </div>
    </body>
    <script>

        $(document).ready(function() {
            // HLS Player
            let video = document.getElementById('hls-video');
            if (Hls.isSupported()) {
                let hls = new Hls();
                hls.loadSource('/packaged/master.m3u8');
                hls.attachMedia(video);
                hls.on(Hls.Events.MANIFEST_PARSED, function () {
                    //video.play();
                });
            } else if (video.canPlayType('application/vnd.apple.mpegurl')) {
                video.src = '/packaged/master.m3u8';
                video.addEventListener('loadedmetadata', function () {
                    //video.play();
                });
            }

            //DASH Player
            let url = "/packaged/stream.mpd";
            let player = dashjs.MediaPlayer().create();
            player.initialize(document.querySelector("#dash-video"), url, true);

        });
    </script>
</html>

I tested these above codes with a video that has no audio stream and it worked fine. Furthermore, I am going to rewrite this part of the project and I will give more examples in the documentation.