Sunday, February 26, 2012

Using FFMPEG



FFMPEG is a complete, cross-platform solution to record, convert and stream audio and video. It includes libavcodec; the leading audio/video codec library. FFMPEG is a free software project and it is published under the GNU Lesser General Public License 2.1+ or GNU General Public License 2+ (depending on which options are enabled).

To install FFMPEG in Ubuntu just type:
sudo apt-get install ffmpeg

This post contains a collection of useful examples of using FFMPEG from the command line.

Video Information
Get information on the video (codec, frame rate, etc.).
ffmpeg -i myvideo.avi

Video Encoding
To encode the video using a different codec we specify it using the -vcodec option.
ffmpeg -i myvideo.avi -vcodec mpg4 video_out.avi

Now let's say we want a better quality compression and a different frame rate from the original. We specify 1Mb/s bitrate using the -b option (default is 200kb/s) and we change the frame rate to 12 fps using the -r option.
ffmpeg -i myvideo.avi -vcodec mpg4 -b 1M -r 12 video_out.avi
Other values for the -vcodec option include:
copy:
 Copy raw codec data as is (do not reencode)
rawvideo:
 Raw video (uncompressed)
xvid:
 XviD (MPEG-4 Part2)
wmv2:
 Windows Video Media

Extracting Video Frames
The following example extracts the frames from the video and saves them as frame_001.png, frame_002.png,...
ffmpeg -i myvideo.avi -f image2 frame_%03d.png

Cropping a video to a specific time interval
The following example extracts a scene from the input video that start at 1 minute 45 seconds and has a duration of 30 seconds.
ffmpeg -i myvideo.avi -ss 00:01:45 -t 00:00:45 myscene.avi

Slow down or speed up a video
Let's say we need to slow down a video now. Unfortunately, decreasing the frame rate with the -r option will not do, since ffmpeg will drop frames to keep the duration of the video the same. We need to use mjpegtools to do that. To install mjpegtools type:
sudo apt-get install mjpegtools
For this example the original video has a frame rate of 30fps and we want to make it play at 1/3 of the speed. So we have to trick ffmpeg to believe that the original video had a 10fps frame rate. The yuvfps tool does exactly that. Make sure that the input frame rate (-s option) and the output frame rate (-r option) are the same.
ffmpeg -i myVideo.avi -f yuv4mpegpipe - | yuvfps -s 10:1 -r 10:1 | ffmpeg -f yuv4mpegpipe -i - -vcodec mpeg4 -b 12M  mySlowVideo.avi
Note that this will not work unless your video has one of the yuv pixel formats. If your pixel format is not compatible (rgba for example) you would have to convert it like this:
ffmpeg -i myVideo.avi -pix_fmt yuv420p myVideo2.avi