FFmpeg 简易内嵌字幕压制

Posted by Allen on 2017-08-28
Coding

I was planned to download a Blibli video with funny bullet screen build-in. But the Chrome extension Bilibili Helper is only able to download source video and separate .ass file.

So I tried to google the way how to hardcode .ass subtitles into .mp4 video, and I found FPSLuoziASSSubs . It’s a simple Mac OS X command line tool which is so easy to use, really surprised me.

Step 1

First, you need a computer with Mac OS X, and make sure Homebrew is installed.

Step 2

Install ffmpeg and libass with Homebrew.

1
brew install ffmpeg --with-libass

You should see follow lines once the installation finished.

1
2
🍺  /usr/local/Cellar/ffmpeg/3.3.3: 243 files, 51.0MB, built in 9 minutes 5 seconds
➜ ~ ls

Step 3

Keep your .mp4 and .ass file names simple, should not contain any special characters and blank spaces. Create a new file named render.sh, paste follow content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/bin/sh
# FFmpeg 简易内嵌字幕压制脚本 for Unix-like
# 版本:1.0
# 作者:@FPS罗兹
# 日期:2015年4月3日
# MIT协议,所有人可自由修改发布

# 输出视频参数
# 文件路径可使用绝对路径或相对路径,这里不再赘述

# 片源路径
FILE_PATH="FPSAR.mp4"
# 字幕文件路径
ASS_PATH="fpsar.ass"
# 输出视频尺寸
# 保留片源宽高比,使用"-1:720"即代表输出视频为720P
SCALE="-1:540"
# 输出码率
# 默认为平均视频+音频码率,请按需斟酌,"1200k"即代表1200kbps平均总码率
BITRATE="1200k"

# 压制
# 强制使用H.264,Slow预设以及所有线程
# 保留片源视频帧率与音频码率格式
# 输出文件名为"片源名+码率.mp4"
EXEC=ffmpeg
$EXEC -i $FILE_PATH -vf ass=$ASS_PATH,scale=$SCALE -b:v $BITRATE -c:v libx264 -preset slow -threads 0 "$FILE_PATH$BITRATE".mp4

Step 4

Edit the config file as you wish, then run render.sh with bash.

1
bash render.sh

Done! You’re welcome.


Comments: