孙悟空 发表于 2014-7-28 00:05:46

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


在某些视频格式标准中(也就是容器中)是不支持字幕的,例如将mkv文件转码成为ts文件或者mp4文件后,有时候会发现字幕不翼而飞了,这对有些英语不是很好,需要看到字幕的人就不那么顺利了。不过没关系,在转码的时候,可以将字幕打入视频流中,这样就可以在播视频时,将字幕输出出来了,具体方法如下:
首先要了解字幕又很多种,例如srt,例如txt,还有其他的格式,不过这里主要分享的时ASS的
[*]首先要使用已经支持ass的ffmpeg,怎么才能确定ffmpeg是否已经支持ass了呢:
        可以通过ffmpeg -codecs参数来查看        $ ffmpeg -codecs|grep ass
ffmpeg version N-65018-gad91bf8 Copyright (c) 2000-2014 the FFmpeg developers
built on Jul 27 2014 20:36:24 with Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
configuration: --enable-opengl --enable-libx265 --enable-libx264 --enable-libmp3lame --enable-gpl --enable-libfaac --enable-nonfree --prefix=/usr/ --enable-libfreetype --enable-libass
libavutil      52. 92.101 / 52. 92.101
libavcodec   55. 69.100 / 55. 69.100
libavformat    55. 49.100 / 55. 49.100
libavdevice    55. 13.102 / 55. 13.102
libavfilter   4. 11.102 /4. 11.102
libswscale      2.6.100 /2.6.100
libswresample   0. 19.100 /0. 19.100
libpostproc    52.3.100 / 52.3.100
DES... ass                  ASS (Advanced SSA) subtitle
$ 从上面的倒数第二行中可以看到,已经支持了ASS的解码,编码;可以找到ass的字幕文件,文件内容大致如下:$ head -n 30 ~/Downloads/a.ass

Title: AMV For Dreamers Subtitle
ScriptType: v4.00+
WrapStyle: 0
ScaledBorderAndShadow: yes
Collisions: Normal
Scroll Position: 37
Active Line: 38
Video Zoom Percent: 0.5
PlayResX: 720
PlayResY: 480
Last Style Storage: Default
Video Aspect Ratio: c1.5
Video Position: 5818
YCbCr Matrix: TV.601


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: Default,Arial,20,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,2,2,10,10,10,1
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


Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
Dialogue: 0,0:00:01.36,0:00:23.21,Lyric,,0,0,0,,敬启 读着这封信的你
Dialogue: 0,0:00:23.21,0:00:29.22,Lyric,,0,0,0,,你在哪里 做些什么呢?
Dialogue: 0,0:00:30.69,0:00:34.63,Lyric,,0,0,0,,十五岁的我
Dialogue: 0,0:00:34.63,0:00:44.49,Lyric,,0,0,0,,有着无法向任何人诉说的烦恼
Dialogue: 0,0:00:46.05,0:00:53.69,Lyric,,0,0,0,,如果是写给未来的自己的信的话
Dialogue: 0,0:00:53.69,0:00:59.95,Lyric,,0,0,0,,想必一定能坦率地说出口吧
Dialogue: 0,0:01:00.65,0:01:04.62,Lyric,,0,0,0,,此刻 快要认输 快要落泪
$ 找到了对应的文件以后,可以考虑将该文件的字幕打入到对应的视频文件中,下面聚一个例子:$ ffmpeg -i ~/Movies/objectC/facebook.mp4 -i ~/Downloads/a.ass -vf ass=~/Downloads/a.ass a.ts
ffmpeg version N-65018-gad91bf8 Copyright (c) 2000-2014 the FFmpeg developers
built on Jul 27 2014 20:36:24 with Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
configuration: --enable-opengl --enable-libx265 --enable-libx264 --enable-libmp3lame --enable-gpl --enable-libfaac --enable-nonfree --prefix=/usr/ --enable-libfreetype --enable-libass
libavutil      52. 92.101 / 52. 92.101
libavcodec   55. 69.100 / 55. 69.100
libavformat    55. 49.100 / 55. 49.100
libavdevice    55. 13.102 / 55. 13.102
libavfilter   4. 11.102 /4. 11.102
libswscale      2.6.100 /2.6.100
libswresample   0. 19.100 /0. 19.100
libpostproc    52.3.100 / 52.3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/Users/StevenLiu/Movies/objectC/facebook.mp4':
Metadata:
    major_brand   : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf55.22.102
    description   : bbs.chinaffmpeg.com 孙悟空
Duration: 02:00:27.85, start: 0.000000, bitrate: 893 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 640x480, 797 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 87 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Input #1, ass, from '/Users/StevenLiu/Downloads/a.ass':
Duration: N/A, bitrate: N/A
    Stream #1:0: Subtitle: ssa
Added subtitle file: '/Users/StevenLiu/Downloads/a.ass' (3 styles, 51 events)
Output #0, mpegts, to 'a.ts':
Metadata:
    major_brand   : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    description   : bbs.chinaffmpeg.com 孙悟空
    encoder         : Lavf55.49.100
    Stream #0:0(und): Video: mpeg2video, yuv420p, 640x480, q=2-31, 200 kb/s, 25 fps, 90k tbn, 25 tbc (default)
    Metadata:
      handler_name    : VideoHandler
      encoder         : Lavc55.69.100 mpeg2video
    Stream #0:1(und): Audio: mp2, 44100 Hz, stereo, s16, 384 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      encoder         : Lavc55.69.100 mp2
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> mpeg2video (native))
Stream #0:1 -> #0:1 (aac (native) -> mp2 (native))
Press to stop, [?] for help
Fontconfig warning: ignoring UTF-8: not a valid region tag
fontconfig: cannot find font 'Adobe 黑体 Std R', falling back to 'Verdana106 Negreta'
Glyph 0x656C not found, selecting one more font for (Adobe 黑体 Std R, 200, 0)
fontconfig: cannot find glyph U+656C in font 'Adobe 黑体 Std R', falling back to 'MS Gothic'
Glyph 0x8BFB not found, selecting one more font for (Adobe 黑体 Std R, 200, 0)
fontconfig: cannot find glyph U+8BFB in font 'Adobe 黑体 Std R', falling back to 'SimSun'
frame= 3163 fps=461 q=31.0 Lsize=   11136kB time=00:02:06.64 bitrate= 720.3kbits/s   
video:3877kB audio:5935kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 13.490704%

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

mingyuejingque 发表于 2014-7-28 09:20:41

顶起
页: [1]
查看完整版本: 使用ffmpeg将ASS字幕打进视频流中