使用ffmpeg将多个音轨和字幕添加到破折号清单(mpd) [英] Adding multiple audio tracks and subtitles to dash manifest (mpd) with ffmpeg
问题描述
我正在尝试创建一个网站以流式传输一些视频.对于每个视频,我分别在3个不同的文件夹中提取视频,音频和字幕.碰巧一个视频有多个音轨和多个字幕.我做了很多研究,但我不知道如何在清单中添加所有这些内容.现在,我使用以下命令:
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
我的两个视频具有不同的分辨率和比特率,并且效果很好.但是我没有任何字幕,而且我的两个音轨都被视为一个音轨,具有两个不同的比特率(就像视频一样).我认为我应该有很多adaptive_sets,但是我不知道如何创建它们.
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.
如何创建正确的清单?
推荐答案
几天后,我找到了解决方案.
After a few days, I found the solution.
我的目标是将视频转换为mpeg-dash内容,这对于流媒体来说确实很棒.
My goal is to convert a video into mpeg-dash content which is really great for streaming.
我将视频编码为h264,将音频编码为aac,将字幕编码为webvtt.这是与大型浏览器兼容的好设置.vp9真的很好,但是对我来说太长了.
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.
所需工具:
- ffmpeg: https://www.ffmpeg.org/download.html
- mp4dash&mp4fragment: https://www.bento4.com/downloads/
假设我们有一个带有以下流的1080p视频文件"video.mkv":
Let's suppose we have a 1080p video file "video.mkv" with these streams:
- 0:视频流
- 1:音频流,它是语言
- 2:音频流,英语
- 3:字幕流,它是语言
- 4:英语字幕流
我提取视频流并将其转码为不同的分辨率和比特率:
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音频
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字幕
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
您可以使用"-loglevel警告"选项查看较少的信息.
You can use the "-loglevel warning" option to see less informations.
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音频
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.分割文件并创建破折号清单
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
您现在可以删除tmp文件夹
You can now delete the tmp folder
rm -rf tmp
(以及您不再需要的源文件)
(and your source file if you don't need it anymore)
您现在有了可以流式传输的mpeg-dash内容.您必须提供文件(允许cors并启用字节范围请求).我使用angular和rx-player作为播放器.我可以切换语言,字幕,并且视频质量可以适应客户的带宽!
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播放器: https://github.com/canalplus/rx-player
这篇关于使用ffmpeg将多个音轨和字幕添加到破折号清单(mpd)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!