ffmpeg: some useful tricks

Last updated on Thu, 2009-12-17 19:51. Originally submitted by fabio on 2009-05-20 23:12.

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 720x576 it converts to a 720x406 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 640x360 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
Posted in:

I want to extract images from

Submitted by Andy (not verified) on Mon, 2013-11-25 16:41.

I want to extract images from a ogg file using ffmpeg.

At the moment I am extracting them straight to the hard drive using these arguments:

-i - -qscale 1 h:\\out\\img-%05d.jpg
But i would like to extract directly to a stream.

What would be the arguments to use please?

thanks

Good stuff - thanks

Submitted by AKE (not verified) on Wed, 2012-08-29 07:51.

Thanks for the tips. Very helpful! (If you have new ones -- would welcome an update / extension to the article!)

DV index

Submitted by Gil Megidish (not verified) on Wed, 2009-11-04 11:00.

Awesome tricks!

I have copied several DVs to my hdd, with Windows Movie Maker I see they have index. What I do atm, is manually export individual videos clips as new files. I tried with ffmpeg, but I doesn't support this index. I also tried reading the dreadful opendml documentation and quickly gave up. Any ideas?

I'm not sure I understood

Submitted by fabio on Wed, 2009-11-04 11:21.

I'm not sure I understood what you are trying to accomplish..

Anyway, you might want to capture the files from the camcorder using dvgrab, a little linux utility to do that. Dvgrab has a nice option called --autosplit which let you split your camera stream automatically on new recording start to separate on different files.

Cool stuff; thank you for the article

Submitted by Steve (not verified) on Wed, 2009-08-12 17:34.

I'll try it out sometime. I have a VHS tape with poor sound quality, but have the LP with the same song.