Friday, December 02, 2016

Adding Subtitle and Scaling Film using ffmpeg

  1. Linux Version :
  2. # lsb_release -da
  3. Distributor ID: Ubuntu
  4. Description:    Ubuntu 14.04.4 LTS
  5. Release:        14.04
  6. Codename:       trusty

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

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

  1. 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 :
  1. ffmpeg -i input.mp4 -vf subtitles=mysub.srt output.mp4
Advanced :
  1. 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 :
  1. ffmpeg -i input.mp4 -vf scale=1028:720 output.mp4
Advanced :
  1. 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
  1. 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"
  1. 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:
  1. ffmpeg -i input.mp4 -vf scale=1028:720 output.mp4 >/dev/null
for more information you can read it here