ffmpeg: some useful tricks

If you are interested in audio/video conversions and general manipulation you should really have a look at ffmpeg.

ffmpeg it’s a little command line utility which will let you do some really great things with multimedia files.

During the time, I collected some examples of usages of ffmpeg which I found useful. I posted here some and continue to update this page everytime I find a new ffmpeg trick.

Converting DV to avi

You just copied your holidays video from your DV camera. Then.. you find out that you are wasting 300 GB of your HD. Well.. you should definitely compress those videos!

Given a 16:9 interlaced DV file (video + audio data) with a resolution of 720×576 it converts to a 720×406 MPEG4 avi at 800 kbit/s with MP3 audio at 64 kbit/s at 44100 Hz.

ffmpeg -i all_grabs.dv -s 720x406 -b 800k -acodec libmp3lame -ab 64k -vcodec mpeg4 -deinterlace -ar 44100 full_encoding.avi

Create a video from an image still and an audio file

Given an image (a good resolution might be of 640×360 for youtube videos) and an audio file, creates a video which is basically a still from the image with the audio file in the background.

The 391 below is a magic number which represents the length of the audio file expressed in seconds. Obviously this can be extracted from the audio file with a regular expression, but I’ve always been too lazy to do that :-).

cat `for i in $(seq 1 391); do echo -n " black_still.jpg "; done;` | ffmpeg -r 1 -f mjpeg -i - -r 1 out1.mp4

ffmpeg -i out1.mp4 -i audio.mp3 -acodec copy finish.mp4

Extracting audio from a video file

Given a video file (video + audio) it extract the audio track and save it to a separate file.

ffmpeg -i video.avi -acodec copy audio.mp3

Reencoding the original audio track:

ffmpeg -i all_grabs.dv -acodec libmp3lame -ab 64k -ar 48000 full_encoding.mp3

-ab is the audio bitrate (here 64kbit/s)
-ar is the sample frequency (here 48000Hz)

Extracting a part of the video

Extracts from your full video a smaller part of it.

ffmpeg -i input_video.avi -ss 00:13:02 -t 18 -b 900k -acodec libmp3lame -ab 128k -vcodec mpeg4 extracted_part.avi

-ss is the starting time in hh:mm:ss[.xxx]
-t the length of the extracted part

Replacing audio in a video file

Given a video file with video and audio embedded in it (Input #0) create a video file replacing the original audio with another video file.

Input #0, avi, from 'full_encoding_fixed.avi':
  Duration: 01:44:34.56, start: 0.000000, bitrate: 876 kb/s
    Stream #0.0: Video: mpeg4, yuv420p, 720x406 [PAR 153:149 DAR 55080:30247], 25 tbr, 25 tbn, 25 tbc
    Stream #0.1: Audio: mp3, 44100 Hz, stereo, s16, 64 kb/s
Input #1, mp3, from 'full_encoding.mp3':
  Duration: 01:44:34.87, start: 0.000000, bitrate: 64 kb/s
    Stream #1.0: Audio: mp3, 48000 Hz, stereo, s16, 64 kb/s

The ffmpeg command:

ffmpeg -i full_encoding_fixed.avi -i full_encoding.mp3  -map 0.0:1 -map 1:0 -f avi -vcodec copy -acodec copy everything.avi

Mencoder bonus: fixing/adding video file index

Video files with a broken/missing index are usually unseekable. You might try the following mencoder command to fix that.

mencoder is another open source media manipulating tool.

mencoder -idx full_encoding.avi -ovc copy -oac copy -o full_encoding_fixed.avi
Scroll to Top