Roshan Book

My Tech Notebook

Tag Archives: FFmpeg

FFmpeg cheat sheet

Source: http://www.webupd8.org/2009/08/ffmpeg-cheat-sheet-19-best-practices.html

ffmpeg is a multiplatform, open-source library for video and audio files. It is usualy available in your distribution repositories, so search for it and install it. For Ubuntu:

sudo apt-get install ffmpeg

Because certain codecs are not activated for FFmpeg in Ubuntu Jaunty, I suggest you download and install these ffmpeg fixed packages (unfortunately they are available for 32bit only).

Now that you have installed ffmpeg, you can start trying these commands which cover almost all needs: video conversion, sound extraction, encoding file for iPod or PSP, and more:

Getting info from a video file

ffmpeg -i video.avi

Turn X images to a video sequence

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

This command will transform all the images from the current directory (named image1.jpg, image2.jpg, etc…) to a video file named video.mpg.

Turn a video to X images

ffmpeg -i video.mpg image%d.jpg

This command will generate the files named image1.jpg, image2.jpg, …

The following image formats are also available : PGM, PPM, PAM, PGMYUV, JPEG, GIF, PNG, TIFF, SGI.

Encode a video sequence for the iPpod/iPhone

ffmpeg -i source_video.avi input -acodec aac -ab 128kb -vcodec mpeg4 -b 1200kb -mbd 2 -flags +4mv+trell -aic 2 -cmp 2 -subcmp 2 -s 320x180 -title X final_video.mp4

Explanations :

* Source : source_video.avi
* Audio codec : aac
* Audio bitrate : 128kb/s
* Video codec : mpeg4
* Video bitrate : 1200kb/s
* Video size : 320px x 180px
* Generated video : final_video.mp4

Encode video for the PSP

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

Explanations :

* Source : source_video.avi
* Audio codec : aac
* Audio bitrate : 32kb/s
* Video codec : xvid
* Video bitrate : 1200kb/s
* Video size : 320px x 180px
* Generated video : final_video.mp4

Extracting sound from a video, and save it as Mp3

ffmpeg -i source_video.avi -vn -ar 44100 -ac 2 -ab 192 -f mp3 sound.mp3

Explanations :

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

Convert a wav file to Mp3

ffmpeg -i source_song.wav -vn -ar 44100 -ac 2 -ab 192 -f mp3 final_song.mp3

Convert .avi video to .mpg

ffmpeg -i source_video.avi final_video.mpg

Convert .mpg to .avi

ffmpeg -i source_video.mpg final_video.avi

Convert .avi to animated gif (uncompressed)

ffmpeg -i source_video.avi animated_gif.gif

Mix a video with a sound file

ffmpeg -i song.wav -i source_video.avi final_video.mpg

Convert .avi to .flv

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

Convert .avi to dv

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

Or:

ffmpeg -i source_video.avi -target pal-dv final_video.dv

Convert .avi to mpeg for dvd players

ffmpeg -i source_video.avi -target pal-dvd -ps 2000000000 -aspect 16:9 final_video.mpeg

Explanations :

* target pal-dvd : Output format
* ps 2000000000 maximum size for the output file, in bits (here, 2 Gb)
* aspect 16:9 : Widescreen

Compress .avi to divx

ffmpeg -i source_video.avi -s 320x240 -vcodec msmpeg4v2 final_video.avi

Compress Ogg Theora to Mpeg dvd

ffmpeg -i source_video.ogm -s 720x576 -vcodec mpeg2video -acodec mp3 final_video.mpg

Compress .avi to SVCD mpeg2

NTSC format:

ffmpeg -i source_video.avi -target ntsc-svcd final_video.mpg

PAL format:

ffmpeg -i source_video.avi -target pal-svcd final_video.mpg

Compress .avi to VCD mpeg2

NTSC format:

ffmpeg -i source_video.avi -target ntsc-vcd final_video.mpg

PAL format:

ffmpeg -i source_video.avi -target pal-vcd final_video.mpg

Multi-pass encoding with ffmpeg

ffmpeg -i source_file -pass 2 -passlogfile ffmpeg2pass final_file-2

Fixing FFmpeg

A common question is “why doesn’t the repository FFmpeg convert to mp3, aac, mpeg4, etc?”. Ubuntu doesn’t make it very clear to the general user why the repository FFmpeg is limited or how to fix this issue. This is reflected in the number of forum questions relating to this topic.

Why is FFmpeg from the repository so limited?
All application software Ubuntu installs by default must be considered free, but Ubuntu views some encoders and formats to be proprietary or commercial. Users must install these packages on their own. See the Ubuntu Licensing Policy for more details.

How do I fix FFmpeg?
There are several options:

A. Compile FFmpeg yourself
B. Install the extra libraries
C. Use the Medibuntu repository

A. Compiling FFmpeg yourself (for all Ubuntu versions)
The official FFmpeg answer is to compile it yourself, giving you the option to get what you want with the bug-fixes, enhancements, and benefits of the most recent FFmpeg revision. This is an excellent solution and is explained here:

HOWTO: Install and use the latest FFmpeg and x264

This is the option that I personally use, and although it is not too hard, compiling may not be for everyone for a variety of reasons and the next options could be considered easier.

B. Installing the extra libraries
This is the quickest and easiest option for most users. FFmpeg from the repository does not include some encoders for formats such as: H.263, aac (libfaac), mp3 (libmp3lame), H.264 (libx264), xvid (libxvid), and mpeg4. You can fix this by installing the extra FFmpeg libraries. Open up Terminal and enter:

Code:
sudo apt-get install ffmpeg libavcodec-extra-52

Note: This package does not support libfaac (an AAC encoder), AMR-NB encoding, and AMR-WB decoding. I recommend using libavcodec-extra-52 from the Medibuntu repository if you need this functionality. See option C. Medibuntu.

C. Medibuntu
Medibuntu is a third-party repository that contains packages that are unable to be included in the official Ubuntu repositories. To install FFmpeg from Medibuntu open Terminal (Applications -> Accessories -> Terminal) and run the following:

Code:
sudo wget http://www.medibuntu.org/sources.list.d/$(lsb_release -cs).list --output-document=/etc/apt/sources.list.d/medibuntu.list && sudo apt-get -q update && sudo apt-get --yes -q --allow-unauthenticated install medibuntu-keyring && sudo apt-get -q update

This huge command (adapted from Medibuntu – Community Ubuntu Documentation) will install the repository information to your computer then update and authenticate the new repository. Now install FFmpeg:

Ubuntu Natty Narwhal 10.10, Ubuntu Maverick Meerkat 10.10, Ubuntu Lucid Lynx 10.04 & Ubuntu Karmic Koala 9.10

Code:
sudo apt-get install ffmpeg libavcodec-extra-52

Ubuntu Hardy Heron 8.04

Code:
sudo apt-get install ffmpeg

That’s it. Now you have a non-crippled version of FFmpeg.

Undoing Changes Made By This Guide
B. Uninstalling the unstripped or extra libraries

Code:
sudo apt-get remove ffmpeg libavcodec-*-5*

C. Uninstalling FFmpeg and the Medibuntu Repository

Code:
sudo apt-get autoremove ffmpeg medibuntu-keyring && sudo rm /etc/apt/sources.list.d/medibuntu.list && sudo apt-get update
%d bloggers like this: