A complete ffmpeg tutorial

Introduction

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

chankey@linuxstall:$ sudo apt-get install ffmpeg

Uses

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:

chankey@linuxstall:$ 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:

chankey@linuxstall:$ 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.

chankey@linuxstall:$ 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

chankey@linuxstall:$ 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

chankey@linuxstall:$ 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.

chankey@linuxstall:$ 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

chankey@linuxstall:$ ffmpeg -i s_audiofile.wav -acodec mp3 -ab 192k f_audiofile.mp3

avi to mpg

chankey@linuxstall:$ ffmpeg -i s_videofile.avi f_videofile.mpg

mpg to avi

chankey@linuxstall:$ ffmpeg -i s_videofile.mpg f_videofile.avi

avi to flv

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

avi to dv

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

avi to gif

chankey@linuxstall:$ ffmpeg -i s_videofile.avi f_giffile.gif

Mixing video with a sound file

chankey@linuxstall:$ ffmpeg -i s_audiofile.wav -i s_videofile.avi f_videofile.mpg

avi to mpeg (for dvd players)

chankey@linuxstall:$ 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

chankey@linuxstall:$ ffmpeg -i s_videofile.avi -s 320x240 -vcodec msmpeg4v2 f_videofile.avi

Compress Ogg Theora to mpeg dvd

chankey@linuxstall:$ ffmpeg -i s_videofile.ogm -s 720x576 -vcodec mpeg2video -acodec mp3 f_videofile.mpg

Compress avi to SVCD mpeg2

NTSC format:

chankey@linuxstall:$ ffmpeg -i s_videofile.avi -target ntsc-svcd f_videofile.mpg

PAL format:

chankey@linuxstall:$ ffmpeg -i s_videofile.avi -target pal-svcd f_videofile.mpg

Compress avi to VCD mpeg2

NTSC format:

chankey@linuxstall:$ ffmpeg -i s_videofile.avi -target ntsc-vcd f_videofile.mpg

PAL format:

chankey@linuxstall:$ ffmpeg -i s_videofile.avi -target pal-vcd f_videofile.mpg

Multipass encoding

chankey@linuxstall:$ ffmpeg -i fichierentree -pass 2 -passlogfile ffmpeg2pass fichiersortie-2

About the author

Chankey Pathak is the founder of Linux Stall. He is a Perl developer at Wokana Technologies. He is a Linux and Perl enthusiast. Check out his latest website on Tech News. You may follow him on Google+.