使用ffmpeg将ASS字幕打进视频流中

2019-03-05 09:45:49 浏览数 (1)

在某些视频格式标准中(也就是容器中)是不支持字幕的,例如将mkv文件转码成为ts文件或者mp4文件后,有时候会发现字幕不翼而飞了,这对有些英语不是很好,需要看到字幕的人就不那么顺利了。不过没关系,在转码的时候,可以将字幕打入视频流中,这样就可以在播视频时,将字幕输出出来了,具体方法如下:

首先要了解字幕又很多种,例如srt,例如txt,还有其他的格式,不过这里主要分享的时ASS的

  1. 首先要使用已经支持ass的ffmpeg,怎么才能确定ffmpeg是否已经支持ass了呢:

可以通过ffmpeg -codecs参数来查看

点击(此处)折叠或打开

  1. [StevenLiu@localhost ffmpeg]$ ffmpeg -codecs|grep ass
  2. ffmpeg version N-65018-gad91bf8 Copyright (c) 2000-2014 the FFmpeg developers
  3.   built on Jul 27 2014 20:36:24 with Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
  4.   configuration: --enable-opengl --enable-libx265 --enable-libx264 --enable-libmp3lame --enable-gpl --enable-libfaac --enable-nonfree --prefix=/usr/ --enable-libfreetype --enable-libass
  5.   libavutil 52. 92.101 / 52. 92.101
  6.   libavcodec 55. 69.100 / 55. 69.100
  7.   libavformat 55. 49.100 / 55. 49.100
  8.   libavdevice 55. 13.102 / 55. 13.102
  9.   libavfilter 4. 11.102 / 4. 11.102
  10.   libswscale 2. 6.100 / 2. 6.100
  11.   libswresample 0. 19.100 / 0. 19.100
  12.   libpostproc 52. 3.100 / 52. 3.100
  13.  DES... ass ASS (Advanced SSA) subtitle
  14. [StevenLiu@localhost ffmpeg]$

从上面的倒数第二行中可以看到,已经支持了ASS的解码,编码;

可以找到ass的字幕文件,文件内容大致如下:

点击(此处)折叠或打开

  1. [StevenLiu@localhost ffmpeg]$ head -n 30 ~/Downloads/a.ass
  2. [Script Info]
  3. Title: AMV For Dreamers Subtitle
  4. ScriptType: v4.00
  5. WrapStyle: 0
  6. ScaledBorderAndShadow: yes
  7. Collisions: Normal
  8. Scroll Position: 37
  9. Active Line: 38
  10. Video Zoom Percent: 0.5
  11. PlayResX: 720
  12. PlayResY: 480
  13. Last Style Storage: Default
  14. Video Aspect Ratio: c1.5
  15. Video Position: 5818
  16. YCbCr Matrix: TV.601
  17. [V4 Styles]
  18. Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
  19. Style: Default,Arial,20,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,2,2,10,10,10,1
  20. Style: Lyric,Adobe 黑体 Std R,24,&H14FFFFFF,&H000000FF,&H326A31DE,&H32272732,-1,0,0,0,100,100,0,0,1,1.5,1,1,20,20,16,1
  21. [Events]
  22. Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
  23. Dialogue: 0,0:00:01.36,0:00:23.21,Lyric,,0,0,0,,敬启 读着这封信的你
  24. Dialogue: 0,0:00:23.21,0:00:29.22,Lyric,,0,0,0,,你在哪里 做些什么呢?
  25. Dialogue: 0,0:00:30.69,0:00:34.63,Lyric,,0,0,0,,十五岁的我
  26. Dialogue: 0,0:00:34.63,0:00:44.49,Lyric,,0,0,0,,有着无法向任何人诉说的烦恼
  27. Dialogue: 0,0:00:46.05,0:00:53.69,Lyric,,0,0,0,,如果是写给未来的自己的信的话
  28. Dialogue: 0,0:00:53.69,0:00:59.95,Lyric,,0,0,0,,想必一定能坦率地说出口吧
  29. Dialogue: 0,0:01:00.65,0:01:04.62,Lyric,,0,0,0,,此刻 快要认输 快要落泪
  30. [StevenLiu@localhost ffmpeg]$

找到了对应的文件以后,可以考虑将该文件的字幕打入到对应的视频文件中,下面聚一个例子:

点击(此处)折叠或打开

  1. [StevenLiu@localhost ffmpeg]$ ffmpeg -i ~/Movies/objectC/facebook.mp4 -i ~/Downloads/a.ass -vf ass=~/Downloads/a.ass a.ts
  2. ffmpeg version N-65018-gad91bf8 Copyright (c) 2000-2014 the FFmpeg developers
  3.   built on Jul 27 2014 20:36:24 with Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
  4.   configuration: --enable-opengl --enable-libx265 --enable-libx264 --enable-libmp3lame --enable-gpl --enable-libfaac --enable-nonfree --prefix=/usr/ --enable-libfreetype --enable-libass
  5.   libavutil 52. 92.101 / 52. 92.101
  6.   libavcodec 55. 69.100 / 55. 69.100
  7.   libavformat 55. 49.100 / 55. 49.100
  8.   libavdevice 55. 13.102 / 55. 13.102
  9.   libavfilter 4. 11.102 / 4. 11.102
  10.   libswscale 2. 6.100 / 2. 6.100
  11.   libswresample 0. 19.100 / 0. 19.100
  12.   libpostproc 52. 3.100 / 52. 3.100
  13. Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/Users/StevenLiu/Movies/objectC/facebook.mp4':
  14.   Metadata:
  15.     major_brand : isom
  16.     minor_version : 512
  17.     compatible_brands: isomiso2avc1mp41
  18.     encoder : bbs.chinaffmpeg.com 孙悟空
  19.     description : This File is Created by Easy RealMedia Tools@!
  20.   Duration: 02:00:27.85, start: 0.000000, bitrate: 893 kb/s
  21.     Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 640x480, 797 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
  22.     Metadata:
  23.       handler_name : VideoHandler
  24.     Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 87 kb/s (default)
  25.     Metadata:
  26.       handler_name : SoundHandler
  27. Input #1, ass, from '/Users/StevenLiu/Downloads/a.ass':
  28.   Duration: N/A, bitrate: N/A
  29.     Stream #1:0: Subtitle: ssa
  30. [Parsed_ass_0 @ 0x7fad1b40c5e0] Added subtitle file: '/Users/StevenLiu/Downloads/a.ass' (3 styles, 51 events)
  31. Output #0, mpegts, to 'a.ts':
  32.   Metadata:
  33.     major_brand : isom
  34.     minor_version : 512
  35.     compatible_brands: isomiso2avc1mp41
  36.     description : This File is Created by Easy RealMedia Tools@!
  37.     encoder : bbs.chinaffmpeg.com 孙悟空
  38.     Stream #0:0(und): Video: mpeg2video, yuv420p, 640x480, q=2-31, 200 kb/s, 25 fps, 90k tbn, 25 tbc (default)
  39.     Metadata:
  40.       handler_name : VideoHandler
  41.       encoder : Lavc55.69.100 mpeg2video
  42.     Stream #0:1(und): Audio: mp2, 44100 Hz, stereo, s16, 384 kb/s (default)
  43.     Metadata:
  44.       handler_name : SoundHandler
  45.       encoder : Lavc55.69.100 mp2
  46. Stream mapping:
  47.   Stream #0:0 -> #0:0 (h264 (native) -> mpeg2video (native))
  48.   Stream #0:1 -> #0:1 (aac (native) -> mp2 (native))
  49. Press [q] to stop, [?] for help
  50. Fontconfig warning: ignoring UTF-8: not a valid region tag
  51. [Parsed_ass_0 @ 0x7fad1b40c5e0] fontconfig: cannot find font 'Adobe 黑体 Std R', falling back to 'Verdana106 Negreta'
  52. [Parsed_ass_0 @ 0x7fad1b40c5e0] Glyph 0x656C not found, selecting one more font for (Adobe 黑体 Std R, 200, 0)
  53. [Parsed_ass_0 @ 0x7fad1b40c5e0] fontconfig: cannot find glyph U 656C in font 'Adobe 黑体 Std R', falling back to 'MS Gothic'
  54. [Parsed_ass_0 @ 0x7fad1b40c5e0] Glyph 0x8BFB not found, selecting one more font for (Adobe 黑体 Std R, 200, 0)
  55. [Parsed_ass_0 @ 0x7fad1b40c5e0] fontconfig: cannot find glyph U 8BFB in font 'Adobe 黑体 Std R', falling back to 'SimSun'
  56. frame= 3163 fps=461 q=31.0 Lsize= 11136kB time=00:02:06.64 bitrate= 720.3kbits/s
  57. video:3877kB audio:5935kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 13.490704%

根据上面的内容可以看到,在输入的内容中,有音频流,视频流,还有字幕流,但是输出的内容中,只有视频流,音频流,并且输出的文件为ts,接下来看一下效果:

0 人点赞