A complete ffmpeg tutorial


ffmpeg is a very fast video and audio converter that can also grab from a live audio/video source. It can also convert between arbitrary sample rates and resize video on the fly with a high quality polyphase filter.

Installing ffmpeg

sudo apt-get install ffmpeg


Below are some tasks which you can perform using ffmpeg.

Getting information of a video

To get information of a video file use -i option as:

ffmpeg -i /path/videofile.avi

Making video from images

You can combine the images to make a video (like movie maker in windows) using ffmpeg as:

ffmpeg -f image2 -i image%d.jpg videofile.mpg

The above command will take all the images from the current directory (image1.jpg, image2.jpg, etc) and will join them to make a video file videofile.mpg.

Making images from video file

This is just opposite of the last step. This will take a video and will generate images from it.

ffmpeg -i videofile.mpg image%d.jpg

Encoding video for iPod/iPhone

Source file: s_videofile.avi
Audio codec: aac
Audio bitrate: 128 kb/s
Video codec: mpeg4
Video bitrate: 1200 kb/s
Video size: 320×180
Generated video: f_videofile.mp4

ffpmeg -i s_videofile.avi input -acodec aac -ab 128kb -vcodec mpeg4 -b 1200 kb -mbd 2 -flags +4mv+trell -aic 2 -cmp 2 -subcmp 2 -s 320*180 -title X f_videofile.mp4

Encoding video for PSP

Source : s_videofile.avi
Audio codec : aac
Audio bitrate : 32 kb/s
Video codec : xvid
Video bitrate : 1200 kb/s
Video size : 320×180
Generated video : f_videofile.mp4

ffmpeg -i s_videofile.avi -b 300 -s 320x240 -vcodec xvid -ab 32 -ar 24000 -acodec aac f_videofile.mp4

Video to Audio

You can extract the audio from a video file and save it as mp3 format.

ffmpeg -i s_videofile.avi -vn -ar 44100 -ac 2 -ab 192 -f mp3 soundfile.mp3

Source video : s_video.avi
Audio bitrate : 192kb/s
output format : mp3
Generated sound : soundfile.mp3

wav to mp3

ffmpeg -i s_audiofile.wav -acodec mp3 -ab 192k f_audiofile.mp3

avi to mpg

ffmpeg -i s_videofile.avi f_videofile.mpg

mpg to avi

ffmpeg -i s_videofile.mpg f_videofile.avi

avi to flv

ffmpeg -i s_videofile.avi -ab 56 -ar 44100 -b 200 -r 15 -s 320x240 -f flv f_videofile.flv

avi to dv

ffmpeg -i s_videofile.avi -s pal -r pal -aspect 4:3 -ar 48000 -ac 2 f_videofile.dv

avi to gif

ffmpeg -i s_videofile.avi f_giffile.gif

Mixing video with a sound file

ffmpeg -i s_audiofile.wav -i s_videofile.avi f_videofile.mpg

avi to mpeg (for dvd players)

ffmpeg -i s_videofile.avi -target pal-dvd -ps 2000000000 -aspect 16:9 f_videofile.mpeg

ps 2000000000 is the maximum size for the output file in bits hence it is 2 GB here.

Compress avi to divx

ffmpeg -i s_videofile.avi -s 320x240 -vcodec msmpeg4v2 f_videofile.avi

Compress Ogg Theora to mpeg dvd

ffmpeg -i s_videofile.ogm -s 720x576 -vcodec mpeg2video -acodec mp3 f_videofile.mpg

Compress avi to SVCD mpeg2

NTSC format:

ffmpeg -i s_videofile.avi -target ntsc-svcd f_videofile.mpg

PAL format:

ffmpeg -i s_videofile.avi -target pal-svcd f_videofile.mpg

Compress avi to VCD mpeg2

NTSC format:

ffmpeg -i s_videofile.avi -target ntsc-vcd f_videofile.mpg

PAL format:

ffmpeg -i s_videofile.avi -target pal-vcd f_videofile.mpg

Multipass encoding

ffmpeg -i fichierentree -pass 2 -passlogfile ffmpeg2pass fichiersortie-2