Monday, July 2, 2012

Some small tricks with ffmpeg



FFMPEG is a tool for handling and editing multimedia stuffs. It supports all the popular media formats and has a lot of cool options and features. You probably have ffmpeg installed already in your machine if you are using some video or audio editing tool.  But if you dont, you can install ffmpeg easily by installing libav-tools. In Ubuntu and other Debian-based distros, the installing command is:

sudo apt-get install libav-tools

Here are some small tricks with ffmpeg that I frequently use when I need a fast tool to do some simple multimedia editing tasks:

1 - Convert an audio file to another format

When you need to convert an audio file to another audio format, like when you want to have an .ogg file to change the login sound of Ubuntu, ffmpeg is the fastest tool you can use. The command to convert an audio file to another formate with ffmpeg is:
 ffmpeg -i input.mp3 output.ogg 

 2 - Convert a video file to another format

The command to covert a video file to another format is quite similar, but you can also use an additional option to convert the video resolution too:
 ffmpeg -i input.avi -s 800x600 output.flv 

In this command, the -s parameter means the size of the output video and 800x600 is the its resolution. You can omit this option if you want to keep the original resolution.

To know more about the formats supported by ffmpeg, please check this link.

3 - Crop an audio file

The command to crop an audio file with ffmpeg is:
ffmpeg -acodec copy -i input.mp3 -ss 00:00:25 -t 00:02:00 output.wav

In this command "-acodec copy" is to keep the original sound, "-ss 00:00:25" is to tell where to start cropping and "-t 00:02:00" is the length of the cropped output file. You can change the time variables as you prefer.

4 - Crop a video file

The command to crop a video file with ffmpeg is somehow similar:
ffmpeg -vcodec copy -acodec copy -i input.avi -ss 00:00:25 -t 00:02:00 output.flv

The only change in this command is that you need to define the video encoder when cropping video files, the usual option is "-vcodec copy" to keep the original video.

5 - Extract the audio part from a video

You can use ffmpeg to extract audio from a video file. However, you will need to identify the format of the audio in the video file first. The tool to do this task is ffprobe ( which also comes within the libav-tools package). To identify the audio format of a video file, the command will be:
ffprobe videofile.avi

Here is the sample output of this command. The highlighted part is the audio properties of the video file, the audio format here is .aac:

Some small tricks with ffmpeg

After you know the audio format of the video file, the command to extract the audio part will be (I use the sample file aa.mp4 here):
ffmpeg -i aa.mp4 -vn -acodec copy output.aac

6 - Extract images from a video

If you want to extract images from a video with ffmpeg, the command is:
ffmpeg -i video.mp4 img%d.jpg

This command will create 25 images for every second of the input video. The output images will be named img1.jpg, img2.jpg, img3.jpg ..

If you want to change the frame rate into 10 images for every second, you can use the "-r" option. The command to extract 10 images for each second of the input video will be:
ffmpeg -i video.mp4 -r 10 img%d.jpg

If you want to extract images from a certain time, you can use the "-ss" and "-t" options as you use for cropping audio and video files. The command to extract 15 images for each second of the video and the process starts at the 30th second and ends at the 40th second will be:
ffmpeg -i video.mp4 -r 15 -ss 00:00:30 -t 00:00:10 img%d.jpg

***************************

These are 6 small tricks that I often use with ffmpeg. But as I have said, ffmpeg has a lot of options and features (I myself dont know all of them). So if you really want to know everything about ffmpeg, you can check its official document (note: its a huge wall of text).

No comments:

Post a Comment