Add text to .avi or mpeg4 video files on Ubuntu 12.04

In order to add text to a video file first we need to make sure we have all the editing tools and codecs

 

sudo apt-get install ubuntu-restricted-extras

sudo apt-get install ffmpeg x264

sudo apt-get install frei0r-plugins mjpegtools

 

Once all these are installed we can add text by running the command below

sudo ffmpeg -i /media/Verbatim/ty/new/130251.mp4 -vf drawtext="fontfile=/usr/share/fonts/truetype/freefont/FreeSans.ttf: text='Your own text to add here':fontcolor=white@1.0:fontsize=70:x=300: y=600" -y 130251_num8.mp4

 

  • where -i specifies the input file
  • -vf specifies what you want to do to the file
  • drawtext adds the text Your own text to add here with a colour of white fontsize 70 and at the x y cooridainates  300 600 
  • output file name is 130251_num8
  • output file format is mp4

​​The code above will let ffmpeg choose the audio and video codecs which most times is not what you want because it can decrease the quality of the video. In order to select the codecs to apply, use the code below.

sudo ffmpeg -i /home/drupalpro/Desktop/ty/130257.mp4 -vf drawtext="fontfile=/usr/share/fonts/truetype/freefont/FreeSans.ttf: text='130257':fontcolor=red@1.0:fontsize=70:x=350: y=650" -vcodec libx264 -crf 18 -acodec copy 130257.mp4

  • only change is -vcodev libx264 which select the h.264 codec which is the best
  • -crt which sets the quality (high is bad and low is good) -crt 16 is supposed to be visually lossless to the human eye but -crt 0 is true lossless and will take a long time to encode
  • and -acodec copy which just copies the original

 

FFmpeg also alows yout to sync your audio to video.

ffmpeg -i input.avi -itsoffset 0.2 -i input.avi -map 0:0 -map 1:1  -acodec copy -vcodec copy synced.avi

Check out http://alien.slackbook.org/blog/fixing-audio-sync-with-ffmpeg/ for a detailed explanation.