TOC
====================
- video向视频文件里添加字幕是件很常见的事,本文使用FFmpeg将字幕文件集成到视频文件里。 在CentOS上编译安装FFmpeg
字幕文件转换
字幕文件有很多种,常见的有 .srt
, .ass
文件等,下面使用FFmpeg进行相互转换。 将.srt
文件转换成.ass
文件
ffmpeg -i subtitle.srt subtitle.ass
将.ass
文件转换成.srt
文件
ffmpeg -i subtitle.ass subtitle.srt
集成字幕,选择播放
这种字幕集成比较简单,播放时需要在播放器中选择相应的字幕文件。
ffmpeg -i input.mp4 -i subtitles.srt -c:s mov\_text -c:v copy -c:a copy output.mp4
嵌入SRT字幕到视频文件
单独SRT字幕
字幕文件为subtitle.srt
ffmpeg -i video.avi -vf subtitles=subtitle.srt out.avi
嵌入在MKV等容器的字幕
将video.mkv
中的字幕(默认)嵌入到out.avi
文件
ffmpeg -i video.mkv -vf subtitles=video.mkv out.avi
将video.mkv
中的字幕(第二个)嵌入到out.avi
文件
ffmpeg -i video.mkv -vf subtitles=video.mkv:si=1 out.avi
嵌入ASS字幕到视频文件
ffmpeg -i video.avi -vf "ass=subtitle.ass" out.avi
不能加载fontconfig文件
Fontconfig error: Cannot load default config file
\[Parsed\_ass\_0 @ 0000000002bfa3e0\] No usable fontconfig configuration file found,
using fallback.
Fontconfig error: Cannot load default config file
出现类似错误的原因是无法加载字体配置文件。
环境变量
FFmpeg使用的默认字体文件是使用系统环境变量中对应的文件位置。
默认文件名:fonts.conf
环境变量 FONTCONFIG_FILE
可以覆盖默认配置文件
环境变量 FONTCONFIG_PATH
可以覆盖默认配置文件目录
Linux 默认配置文件:fonts.conf
使用步骤:
- 以root权限登录
- 建立
/etc/fonts
目录 - 下载
fonts.conf
文件到/etc/fonts
目录 - 设置
fonts.conf
文件权限为可读
Linux/Mac 系统
在~/.bashrc 最后添加:
export FONTCONFIG\_PATH=/opt/X11/lib/X11/fontconfig
使之有效:
source ~/.bashrc
编辑 /opt/X11/lib/X11/fontconfig/fonts.conf
在字体目录添加 /Library/Fonts
Windows系统
在系统环境变量中添加:
name: FONTCONFIG_PATH
value: J:/ffmpeg
(我本机ffmpeg
目录,与fonts.conf
同目录即可)
将fonts.conf
文件放至上述指定目录。
以下为yaosansi使用的fonts.conf
文件:
mono
monospace
sans serif
sans-serif
sans
sans-serif
Times New Roman
serif
sans-serif
serif
monospace
sans-serif
Times New Roman
Thorndale AMT
Arial
Albany AMT
Courier New
Cumberland AMT
GulimChe
false
DotumChe
false
BatangChe
false
GungsuhChe
false
0x0020
0x00A0
0x00AD
0x034F
0x0600
0x0601
0x0602
0x0603
0x06DD
0x070F
0x115F
0x1160
0x1680
0x17B4
0x17B5
0x180E
0x2000
0x2001
0x2002
0x2003
0x2004
0x2005
0x2006
0x2007
0x2008
0x2009
0x200A
0x200B
0x200C
0x200D
0x200E
0x200F
0x2028
0x2029
0x202A
0x202B
0x202C
0x202D
0x202E
0x202F
0x205F
0x2060
0x2061
0x2062
0x2063
0x206A
0x206B
0x206C
0x206D
0x206E
0x206F
0x3000
0x3164
0xFEFF
0xFFA0
0xFFF9
0xFFFA
0xFFFB
30
参考
- http://www.ffmpeg.org/
- https://trac.ffmpeg.org/wiki/HowToBurnSubtitlesIntoVideo
- http://ffmpeg.org/ffmpeg-all.html#toc-subtitles-1
- http://en.wikipedia.org/wiki/SubStation_Alpha#Advanced_SubStation_Alpha
- http://ffmpeg.org/ffmpeg-filters.html#ass
- http://www.stata.com/support/faqs/unix/fontconfig-error/
- http://freedesktop.org/software/fontconfig/fontconfig-user.html
- http://www.freedesktop.org/wiki/Software/fontconfig/
- http://www.stata.com/support/faqs/unix/fonts.conf/lux64/fonts.conf
- http://blog.neten.de/posts/2013/10/06/use-ffmpeg-to-burn-subtitles-into-the-video/