Friday, December 02, 2016

Adding Subtitle and Scaling Film using ffmpeg

Linux Version :
# lsb_release -da
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.4 LTS
Release:        14.04
Codename:       trusty

You will need to install :
root@kecap:/tmp# apt-get install ffmpeg
ffmpeg used for adding and scaling film, for another information you can google it.

root@kecap:/tmp# apt-get install fontconfig
because i want use another font for my subtitle, in this case it's Arial

root@kecap:/tmp# apt-get install ttf-mscorefonts-installer
you need to install this package to get microsoft fonts, but if after installing this package and you got empty directory (like what happen to me), simple...just copy arial font from windows to /usr/share/fonts/truetype/msttcorefonts/ and run fs-cache.

Now, we go to main topic.

Adding subtitle using ffmpeg
Basic :
ffmpeg -i input.mp4 -vf subtitles=mysub.srt output.mp4
Advanced :
ffmpeg -i input.mp4 -vf subtitles="f=mysub.srt:force_style='FontName=Arial Bold,FontSize=22,PrimaryColour=&H0009FAFD,OutlineColour=&H00000000,WrapStyle=2,Borderstyle=1,Outline=1,Shadow=1'" -y output.mp4
for .ASS tag description please read this

Scaling film using ffmpeg
Basic :
ffmpeg -i input.mp4 -vf scale=1028:720 output.mp4
Advanced :
ffmpeg -i input.mp4 -vf scale=-1:720 -c:v libx264 -profile:v high -level:v 3.1 -c:a copy output.mp4

Combining two ffmpeg command
ffmpeg -i input.mp4 -vf "scale=-1:720,subtitles="f=mysub.srt:force_style='FontName=Arial Bold,FontSize=22,PrimaryColour=&H0009FAFD,OutlineColour=&H00000000,WrapStyle=2,Borderstyle=1,Outline=1,Shadow=1'"" -c:v libx264 -profile:v high -level:v 3.1 -y output.mp4

ffmpeg & bash
If you writing ffmpeg script inside bash, you will get "funny error"
Parse error, at least 3 arguments were expected, only 1 given in string....
the point is the bash script seems to produce input (Namely the 'c' key) which interferes with the ffmpeg process. just add >/dev/null at the end of the ffmpeg command to prevent ffmpeg from reading its standard input, see example below:
ffmpeg -i input.mp4 -vf scale=1028:720 output.mp4 >/dev/null
for more information you can read it here