ffmpegstreammanifestmpeg-dashmpd

Adding multiple audio tracks and subtitles to dash manifest (mpd) with ffmpeg


I'm trying to create a website to stream some videos. For each video, I extract video, audio and subtitles in 3 different folders. It happens that a video has multiple audio tracks and multiple subtitles. I did a lot of research and I don't know how to add all of them in the manifest. Right now, I use this command:

ffmpeg -f webm_dash_manifest \
-i video1.mp4 -f webm_dash_manifest \
-i video2.mp4 -f webm_dash_manifest \
-i audio1.webm -f webm_dash_manifest \
-i audio2.webm -f webm_dash_manifest \
-i subtitles.vtt \
-c copy -map 0 -map 1 -map 2 -map 3 \
-f webm_dash_manifest -adaptation_sets "id=0,streams=v id=1,streams=a" manifest.mpd

My two videos have different resolutions and bitrates, and it works perfectly. But I don't get any subtitles and my two audio tracks are considered like one same audio track which has two different bitrates (just like videos). I think I should have many adaptation_sets, but I don't know how to create them.

How can I create that manifest the right way?


Solution

  • After a few days, I found the solution.

    My goal is to convert a video into mpeg-dash content which is really great for streaming.

    I will encode video to h264, audio to aac, and subtitles to webvtt. It's good settings for a large browser compatibility. vp9 is really nice too but too long to encode for me.

    Tools required:


    Let's suppose we have a 1080p video file "video.mkv" with these streams:

    1. Extracting differents streams

    1.1 Video

    I extract and transcode video stream to differents resolutions and bitrates:

    ffmpeg -i video.mkv -an -sn -c:0 libx264 -x264opts 'keyint=24:min-keyint=24:no-scenecut' -b:v 5300k -maxrate 5300k -bufsize 2650k -vf 'scale=-1:1080' tmp/video/video-1080.mp4
    
    ffmpeg -i video.mkv -an -sn -c:0 libx264 -x264opts 'keyint=24:min-keyint=24:no-scenecut' -b:v 2400k -maxrate 2400k -bufsize 1200k -vf 'scale=-1:720' tmp/video/video-720.mp4
    
    ffmpeg -i video.mkv -an -sn -c:0 libx264 -x264opts 'keyint=24:min-keyint=24:no-scenecut' -b:v 600k -maxrate 600k -bufsize 300k -vf 'scale=-1:360' tmp/video/video-360.mp4
    

    1.2 Audio

    ffmpeg -i video.mkv -map 0:1 -ac 2 -ab 192k -vn -sn tmp/audio/audio-it.mp4
    
    ffmpeg -i video.mkv -map 0:2 -ac 2 -ab 192k -vn -sn tmp/audio/audio-en.mp4
    

    1.3 Subtitle

    ffmpeg -i video.mkv -map 0:3 -vn -an tmp/subtitle/subtitle-it.vtt
    ffmpeg -i video.mkv -map 0:4 -vn -an tmp/subtitle/subtitle-en.vtt
    

    You can use the "-loglevel warning" option to see less informations.

    2. Fragment video and audio

    2.1 Video

    mp4fragment tmp/video/video-1080.mp4 tmp/video/f-video-1080.mp4
    mp4fragment tmp/video/video-720.mp4 tmp/video/f-video-720.mp4
    mp4fragment tmp/video/video-360.mp4 tmp/video/f-video-360.mp4
    

    2.2 Audio

    mp4fragment tmp/audio/audio-it.mp4 tmp/audio/f-audio-it.mp4
    mp4fragment tmp/audio/audio-en.mp4 tmp/audio/f-audio-en.mp4
    

    3. Split files and create the dash manifest

    mp4dash --mpd-name=manifest.mpd tmp/video/f-video-1080.mp4 tmp/video/f-video-720.mp4 tmp/video/f-video-360.mp4 tmp/audio/f-audio-it.mp4 tmp/audio/f-audio-en.mp4 \[+format=webvtt,+language=it\]tmp/subtitle/subtitle-it.vtt \[+format=webvtt,+language=en\]tmp/subtitle/subtitle-en.vtt
    

    You can now delete the tmp folder

    rm -rf tmp
    

    (and your source file if you don't need it anymore)

    You have now your mpeg-dash content which can be streamed. You have to serve your files (allow cors and enable byte range request). I use angular and rx-player as player. I can switch language, subtitles and the video quality is adaptative to the client's bandwidth !

    Rx-player: https://github.com/canalplus/rx-player