Mar 052011
 

ffmpegFFmpeg can be considered the Swissknife of audio and video applications, with many options and possibilities. You probably already have it installed on your computer as a dependency of a program you use to watch videos or listen to music. In this article we will see some use from the command line without using graphics applications.



But first, a presentation of ffmpeg:

FFmpeg is a free software / open source project that produces libraries and programs for handling multimedia data. The most notable parts of FFmpeg are libavcodec, an audio/video codec library used by several other projects, libavformat, an audio/video container mux and demux library, and the ffmpeg command line program for transcoding multimedia files. FFmpeg is published under the GNU Lesser General Public License 2.1+ or GNU General Public License 2+ (depending on which options are enabled)

Components

The project is made of several components:

  • ffmpeg is a command line tool to convert one video file format to another. It can also grab and encode in real time from a TV card.
  • ffserver is an HTTP and RTSP multimedia streaming server for live broadcasts. It can also time shift live broadcast.
  • ffplay is a simple media player based on SDL and on the FFmpeg libraries.
  • ffprobe is a command line tool to show media information.
  • libavcodec is a library containing all the FFmpeg audio/video encoders and decoders. Most codecs were developed from scratch to ensure best performance and high code reusability.
  • libavformat is a library containing demuxers and muxers for audio/video container formats.
  • libavutil is a helper library containing routines common to different parts of FFmpeg. This library includes adler32, crc, md5, sha1, lzo decompressor, Base64 encoder/decoder, des encrypter/decrypter, rc4 encrypter/decrypter and aes encrypter/decrypter.
  • libpostproc is a library containing video postprocessing routines.
  • libswscale is a library containing video image scaling and colorspace/pixelformat conversion routines.
  • libavfilter is the substitute for vhook which allows the video/audio to be modified or examined between the decoder and the encoder.

First, check the formats available

FFmpeg supports most of the popular formats, we don’t need to worry a lot about that. Formats supported by FFmpeg include MPEG, MPEG-4 (Divx), ASF, AVI, Real Audio/Video and Quicktime. To see a list of all the codecs/formats supported by FFmpeg, run the following command:
ffmpeg -formats
This will print a long list of formats, on the left of any format you will see an E (means that it can encode in that format) and/or a D(means that it can decode that format)

#1 Audio Conversion

Say you have the audio file named my_audio.wav and want to convert it in a mp3.

ffmpeg -i my_audio.wav my_audio.mp3

-i is input file
Really easy, it isn’t ? change the extension of the output file with any supported format to get different formats.

#2 Video Conversion

The basic usage it’s like the example saw for the audio so you can simply write:

ffmpeg -i my_video.mpeg -s 500×500 my_video.flv

-s ‘size’ set video resolution size (Width x Height)

This will convert my_video.mpeg file to my_video.flv and will resize the video resolution to 500×500

#3 Extract images from a video

Sometimes is useful to extract some images from a movie, and ffmpeg can do this easily:

ffmpeg-i my_video.flv my_video.mpeg

This will create 25 images for every 1 second, but it may serve us to have more or less images, this can be achieved with the parameter -r

-r fps Set frame rate (default 25)

ffmpeg -i my_video.mpeg -s 500×500 my_video.flv

With this command you’ll get 1 image for every second.

Start e duration

You can also give a start time and the duration with the flags:

-ss position Seek to given time position in seconds. “hh:mm:ss[.xxx]” syntax is also supported.

-t duration Restrict the transcoded/captured video sequence to the duration specified in seconds. “hh:mm:ss[.xxx]” syntax is also supported.

This command will take 25 images images every second beginning at the tenth second, and continuing for 5 seconds

ffmpeg -i test.mpg image%d.jpg

#4 Extract audio from a video

With ffmpeg you can also mix video and audio, so we can extract an mp3 track from a video:

ffmpeg -i test.mpg -r 1 image%d.jpg

In this example we have used the flag -f.

-f fmt Force the format.

To get the same result it’s also possible to use the option to disable video capture:

-vn Disable video recording

ffmpeg -i test.mpg -r 25 -ss 00:00:10 -t 00:00:05 images%05d.png

#5 Create a screencast

With ffmpeg it’s also possible to create a simple screencast, capturing your desktop.

To do this we’ll use some of the flags show in the former example:

ffmpeg -i video.avi -f mp3 audio.mp3

Note: 0.0 is display.screen number of your X11 server, same as the DISPLAY environment variable.

This will save 25 frame per second of your wxga screen (or you can use -s with a resolution like -s 1024×768) and put them in a mpg video in /tmp.

#6 Convert images to movie

Say you have a lot of images named `img001.jpg’, `img002.jpg’ and so on in sequence, you can convert them into a movie with this command:

ffmpeg -i video.avi -vn audio.mp3

#7 Capture a video from the webcam

To record video run ffmpeg with arguments such as these:

ffmpeg -f x11grab -r 25 -s wxga -i :0.0 /tmp/outputFile.mpg

To record both audio and video use:

ffmpeg -f image2 -i img%d.jpg /tmp/a.mpg

These are just some examples, ffmpeg can do a lot in editing audio and video, and in the net there are a lot of examples about it.

[amazon_enhanced asin=”0596100760″ price=”All” background_color=”332610″ link_color=”FFFFFF” text_color=”D4CE99″ /]

References:

Project site with documentation http://www.ffmpeg.org/ffmpeg-doc.html

Youtube channel with information on ffmpeg http://www.youtube.com/view_play_list?p=E0AACC679489E4ED

Popular Posts:

Flattr this!

  11 Responses to “7 Tricks with ffmpeg”

  1. I have tried to convert pal to ntsc video 720×480.
    the conversion seems to go ok but the resulting
    dvd wont play on a ntsc dvd player.

  2. Nice guide. I always heard of the great power of ffmpeg but never saw anyone explain it such a simple language. Great job. Bookmarked. 🙂

  3. “First, check you format”? 😛

  4. Excellent examples for a variety of uses of ffmpeg – a great way to get started. Thanks.

  5. Wonderful article on ffmpeg.

  6. hi, nice article and video, hi, I am .net developer, I am creating web application for convert video,I just need to ask can i get List of command lines for convert mp4 to avi, avi to mp4, mp4 to flv, flv to mp4,mp4 to 3gp.

    thanks,

  7. Great post for a beginner. Thanks for sharing. 🙂

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

*