← Back to Tutorials & Tips

FFmpeg Tutorial


FFmpeg is a command line video processing tool. Below you will find many common tasks you might need to do with a video clip and explanations on how to do these with FFmpeg. This document assumes that you have FFmpeg setup and installed.

Table of Contents

  1. FFmpeg Basics
  2. Extracting a Video Clip Out of a Video
  3. Extracting Audio Out of a Video
  4. Extracting a Frame (Picture) Out of a Video
  5. Combine Video and Audio Into One File
  6. Making Title or Image Slides
  7. Combine (Stitch) Multiple Videos Into One Video
  8. Speed Up (Time-lapse) or Slow Down a Video
  9. Change Aspect Ratio (ie: 16:9 -> 4:3) - Letterboxing
  10. Zooming

I. FFmpeg Basics

Time Format

If you are new to FFmpeg, you will find a few things a bit odd about it. We will start with the time format. Times can be written in either HH:MM:SS where HH is the hours, MM is the minutes, SS is the seconds. Times may also be written as the total number of seconds. For example, if you want to specify 5 minutes and 23.5 seconds, you could write it in the following ways:

  • 00:05:23.5
  • 323.5

Codec vs. Container

In general for video processing, you need to know the difference between a codec versus a container. A container is the file format that you will be storing the video and audio in. This can be AVI, MPG, MOV, etc. Typically, the file extension represents the container type.

The codec is the format that the video or audio is stored in. FFmpeg will attempt to automatically choose the video and audio codec based on the file extension you use. For example, for an AVI file, unless you specify otherwise, FFmpeg will use MPEG-4 for the video codec, and MPEG-2 for the audio codec. NOT ALL CONTAINERS ARE COMPATIBLE WITH EACH CODEC. Unless you have a specific need, it is best to let FFmpeg chose the codecs.

General Command Structure

When sending parameters to FFmpeg, the order you send them in matters. Input parameters must come before output parameters. Also, the input parameters must come before the input file, and output parameters must come before the output file. Here is the general structure of an FFmpeg command:

ffmpeg [input parameters] -i [input file] [output parameters] [output file]

Video/Audio Syncronization

If you are not starting at the beginning of an input file, it is best to tell FFmpeg to ensure that the video and audio are synchronized. This can be achieved by using the “-async 1” parameter. You will see it used in the examples below.

Video Quality

As soon as you do any processing on a video, you will lose quality. To minimize the losses, keep the video/audio bitrate that you are using high. Typically for HD (1280x720) videos, I use 14000 kbit/s. For VGA (640x480) videos, I use 8000 kbit/s. For audio, I use 128 kbit/s. If you don’t specify a bitrate, FFmpeg will use 200 kbit/s for video and 64 kbit/s for audio.

Those bitrates are constant bitrates, and are useful when you have multiple videos that you later want to combine using the method I use in this tutorial. If want to use a variable bitrate you can use the “-qscale” parameter. You can specify a number from 0-31, 0 being the highest quality and 31 being the worst. In general I use the values 3-11. This parameter is an output parameter.

Back to Top

II. Extracting a Video Clip Out of a Video

ffmpeg -async 1 -ss 00:05:34 -t 60 -i input.avi -b 14000k -ab 128k -ar 44100 output.avi

Extracts a video from “input.avi” starting at 5m 34s and going for 60s, encoding it at a bitrate of 14000kbit/s video and 128kbit/s audio with an audio sampling rate of 44100Hz and outputs it as “output.avi”.

ffmpeg -async 1 -ss 00:05:34 -t 60 -i input.avi -vcodec copy -acodec copy output.avi

This does the same as the above command, except it tries to copy the raw video and audio data which prevents loss of quality. However, this doesn’t work with all formats and can make combining videos from multiple sources a pain. I recommend using the first command instead.

Back to Top

III. Extracting Audio Out of a Video

ffmpeg -i input.avi output.wav

Extracts the audio channel out of “input.avi” and outputs it to “output.wav” with the default settings for a wav file. You can use different file formats, but keep in mind that MP3 is not supported in all versions of FFmpeg and using anything other than FLAC or WAV files will result in a loss of quality.

Back to Top

IV. Extracting a Frame (Picture) Out of a Video

ffmpeg -ss 00:05:34.05 -i input.avi -vframes 1 -sameq output.jpg

Extracts a picture from input.avi at the time 5m 34s 5ms and outputs it as output.jpg. If you want the best quality, you can output it to a PNG file instead of JPEG file.

Back to Top

V. Combine Video and Audio Into One File

ffmpeg -shortest -i input.avi -i input.wav -map 0:0 -map 1:0 output.avi

Takes the video from “input.avi”: and the audio from “input.wav” and combines them together to form “output.avi”. The reason we use the “-shortest” parameter is to stop the output as soon as one of the inputs ends. If you allow it to continue, this can cause unpredictable behavior during playback. Most commonly, the symptom would be that combined videos experience video/audio synchronization issues.

The “-map” parameter is used to map an input channel to an output channel. In general, channel 0 is for video and 1 is for audio in a device that has mono audio. It is applied in the order of the input parameters, so the first “-map” goes with the first “-i”. The general form is:

-map [output_channel]:[input_channel]

Back to Top

VI. Making Title or Image Slides

ffmpeg -loop_input -t 5 -i title.jpg title_slide.avi

Takes the image “title.jpg” and turns it into a 5 second video file, “title_slide.avi”. The parameter “-loop_input” tells FFmpeg that you want to repeat the input until output ends, otherwise it would just record one frame (picture) and end the video since it has reached the end of the input.

In general, if you want to combine videos, you will need to add an audio track. I usually use a silent audio file (right click and “save as” to download). To do this, simply take the command for combining a video with audio, and replace “-shortest” with “-loop_input -t 5”.

Note: the silent audio file I linked to is only 3 minutes long, if you try to go longer than this, you will have problems. The file was created in Audacity using the Generate->Silence... menu item.

Back to Top

VII. Combine (Stitch) Multiple Videos Into One Video

mencoder -ovc copy -oac copy 0.avi 1.avi 2.avi ... -o merged.avi

Takes the videos 0.avi, 1.avi, 2.avi, etc. and concatenates them together into one big video file, merged.avi. Mencoder is a seperate command that is another command line video processing command.

“-ovc copy” and “-oac copy” tell Mencoder that you want to copy the data out of the videos, rather than encode it (it has already been encoded by FFmpeg). This works with very few formats. It was tested and works with the default settings of the mpg container and the default settings for the avi container.

IMPORTANT: All the video files must have the same video and audio properties for this to work. That means they must be the same resolution, have the same number of audio channels (e.g., stereo vs mono), and must be encoded with the same bitrate (video and audio), framerate, and audio sampling rate. That means every command you use to process these videos must have the -b, -ab, -ar, and -r parameters specified.

Back to Top

VIII. Speed Up (Timelapse) or Slow Down a Video

Back to Top

IX. Change Aspect Ratio (ie: 16:9 -> 4:3) - Letterboxing

Back to Top

X. Zooming

Back to Top