ffmpeg给视频添加字幕


在网上下载了一些美剧,但是下载下来发现都是没有字幕的,就打算自己给视频添加字幕了。

需要准备的东西:

  1. 视频文件:最好是官方原版的,避免视频被剪辑过导致和字幕匹配不上
  2. 字幕文件:可以在http://www.subhd.tv/网站上找到,一般为ass或者srt格式
  3. ffmpeg工具:ffmpeg是一个非常牛逼的视频处理工具,很多视频软件(如暴风影音等)都是通过它实现的。支持windows、linux以及macos等多个平台。
给视频添加字幕的操作非常简单,执行一行命令即可:

ffmpeg -i video.mkv -i subtitle.ass -c copy output.mkv
替换掉已有字幕:

ffmpeg -i input.mkv -i input.ass -c copy -map 0 -map -0:s -map 1 output.mkv

参数说明:

  • -map 0:选择第一个输入文件的所有流
  • -map -0:s:删除第一个输入文件中的subtitles流(即删除字幕)
  • -map 1:选择第二个输入文件中的流

如果ffmpeg要实现添加字幕的功能需要在编译时开启--enable-filter=subtitles --enable-libass

ffmpeg -i plane.mp4 -vf subtitles=p.srt  psrt.mp4
#合并为视频,而不是增加字幕流。

ffmpeg中srt相关用例

  1 1
  2 00:00:02,940 --> 00:00:31,060
  3 <font face="Microsoft YaHei" size="30" color="#eddfc1">测试波音727哪个座位最安全!^M                                                                                                                                                                                        
  4 <font size="30">It's a test!</font></font>

[Script Info]
; Script generated by Aegisub 3.2.2
; http://www.aegisub.org/
Title: Default Aegisub file
ScriptType: v4.00+
WrapStyle: 0
ScaledBorderAndShadow: yes
YCbCr Matrix: TV.709
PlayResX: 1920
PlayResY: 1080

[Aegisub Project Garbage]
Last Style Storage: Default
Video AR Mode: 4
Video AR Value: 1.777778
Video Zoom Percent: 0.750000
Scroll Position: 104
Active Line: 129
Video Position: 3144

[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: Titles,Microsoft YaHei,53,&H00C1DFED,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2.67327,2.67327,2,29,29,29,1
Style: Titles_L,Microsoft YaHei,53,&H00C1DFED,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2.67327,2.67327,1,29,29,29,1
Style: Character_L,Microsoft YaHei,53,&H00C1DFED,&H00FFFFFF,&H00000000,&H00000000,-1,0,0,0,100,100,0,0,1,2.67327,2.67327,4,29,29,29,1
Style: Character_B,Microsoft YaHei,53,&H00C1DFED,&H00FFFFFF,&H00000000,&H00000000,-1,0,0,0,100,100,0,0,1,2.67327,2.67327,2,29,29,29,1
Style: Author,Microsoft YaHei,40,&H00C1DFED,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2.67327,2.67327,1,29,29,29,1
Style: QR code - BG,Arial,53,&H00FFFFFF,&H000019FF,&H00000000,&H00000000,0,0,0,0,400,400,0,0,1,0,0,5,0,0,0,1
Style: QR code - code,Arial,53,&H00000000,&H000019FF,&H00000000,&H00000000,0,0,0,0,200,200,0,0,1,0,0,5,0,0,0,1
Style: Titles - 1,Microsoft YaHei,53,&H00C1DFED,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2.67327,2.67327,5,29,29,29,1
Style: Titles - 2,Microsoft YaHei,53,&H00000000,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,0,0,2,29,29,29,1
Style: Titles - 3 -,Microsoft YaHei,53,&H0001031B,&H00FFFFFF,&H00000000,&H00000429,0,0,0,0,100,100,0,0,1,0,2.67327,2,29,29,29,1
Style: Subtitle,Microsoft YaHei,67.5,&H00C1DFED,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2.5,2.5,2,29,29,80,1
Style: Default,Arial,53,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,5.34653,5.34653,2,27,27,27,1
Style: Note_top,Microsoft YaHei UI,60,&H00C1DFED,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2.67327,2.67327,8,29,29,80,1
Style: Note_sentence,Microsoft YaHei,37,&H00C1DFED,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2.67327,2.67327,2,29,29,29,1
Style: Note_middle,Microsoft YaHei,72,&H00C1DFED,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2.67327,2.67327,5,29,29,29,1
Style: Subtitle_Valyrian,Microsoft YaHei,67.5,&H0027C0E2,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2.5,2.5,2,29,29,80,1
Style: Note_rotation,Microsoft YaHei,48,&H00C1DFED,&H00FFFFFF,&H00000000,&H00000000,-1,0,0,0,100,100,0,-35,1,2.67327,2.67327,2,29,29,29,1
Style: Note_rotation_Right2,Microsoft YaHei,48,&H00C1DFED,&H00FFFFFF,&H00000000,&H00000000,-1,0,0,0,100,100,0,-10,1,2.67327,2.67327,2,29,29,29,1
Style: Note_rotation_Left,Microsoft YaHei,48,&H00C1DFED,&H00FFFFFF,&H00000000,&H00000000,-1,0,0,0,100,100,0,10,1,2.67327,2.67327,2,29,29,29,1
Style: Note_rotation - 22,Microsoft YaHei,48,&H00C1DFED,&H00FFFFFF,&H00000000,&H00000000,-1,0,0,0,100,100,0,-25,1,2.67327,2.67327,2,29,29,29,1
Style: Note_rotation - 16,Microsoft YaHei,48,&H00C1DFED,&H00FFFFFF,&H00000000,&H00000000,-1,0,0,0,100,100,0,-16,1,2.67327,2.67327,2,29,29,29,1
Style: Note_rotation_Left - 22,Microsoft YaHei,48,&H00C1DFED,&H00FFFFFF,&H00000000,&H00000000,-1,0,0,0,100,100,0,22,1,2.67327,2.67327,2,29,29,29,1
Style: Author - Martin,Microsoft YaHei,40,&H00C1DFED,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2.67327,2.67327,3,29,29,29,1
Style: Open-event,Microsoft YaHei,45,&H009865AD,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2.67327,2.67327,2,29,29,29,1
Style: Open-Targaryen,Microsoft YaHei,45,&H0023187C,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2.67327,2.67327,1,29,29,29,1
Style: Open-Arryn,Microsoft YaHei,45,&H00B77623,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2.67327,2.67327,1,29,29,29,1
Style: Open-Hightower,Microsoft YaHei,45,&H00E4E1E2,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2.67327,2.67327,1,29,29,29,1
Style: Open-Velaryon,Microsoft YaHei,45,&H00D3C451,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2.67327,2.67327,1,29,29,29,1

[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
Dialogue: 0,0:00:02.94,0:00:10.06,Subtitle,,0,0,0,,降落伤亡测试!\N{\fs40}It's a test!
Dialogue: 0,0:00:10.94,0:00:13.06,Subtitle,,0,0,0,,降落在沙漠的!\N{\fs40}On the !
Dialogue: 0,0:00:20.94,0:00:23.06,Subtitle,,0,0,0,,瓦列利安家族会找你算账的!\N{\fs40}House Velaryon is coming for you!
Dialogue: 0,0:00:23.98,0:00:26.73,Subtitle,,0,0,0,,海蛇会砍下你那该死的麻脸脑袋!\N{\fs40}The Sea Snake will have your poxy fucking head!



使用ffmpeg可以很轻松的把ass/vtt/lyric转换为srt文件,命令如下:


ffmpeg -i a.ass b.srt
ffmpeg -i c.vtt d.srt
ffmpeg -i e.lyric f.srt


使用ffplay现在字幕需要使用subtitles filter,具体命令如下:


./ffplay test.mp4 -vf subtitles=test.srt


对于FFmpeg中的srt demuxer的实现逻辑,基本上是参考SRT基本格式实现的,其中并未支持各种格式,具体代码请查看srtdec.c



srt字幕以其简单、体积小、易查看、易掌握等优点,深得人们的喜爱,但便利的代价就是样式少,无法实现复杂的特效。

阅读量: 661
发布于:
修改于: