FFmpeg FTW!

FFmpeg FTW!

FFmpeg is a popular command-line tool for converting, encoding, decoding, and editing audio and video files, and if you work with audio or video it's a tool you'll find useful over and over again. It's free, fast, cross-platform, widely used, and well supported.

In this post I'll provide a brief overview of how to get FFmpeg and examples of a few of the things I like to use it for. These examples are just the tip of the iceberg – FFmpeg's feature set is massive and includes numerous capabilities I've never used, ranging from real-time encoding of audio and video to metadata extraction, support for specialized multimedia hardware, and much more.

Installation

Ubuntu and other common Linux distros include FFmpeg, so if you're running Linux you may already have it. (You may not have the latest version, however, because some distros bundle a fork of FFmpeg.) For Windows users, you'll need to download and install it yourself.

To install FFmpeg, go to the FFmpeg download page and select your operating system, and you’ll find options for installing source code or binaries. If you don’t have unusual codec requirements or a desire to share libraries or work with the source code, the version you’ll want to install is the static binary for your platform. On Windows, this option will give you a few standalone EXE files, probably packaged in a ZIP container.

Extract ffmpeg.exe, and either put it in the folder where you’ll be working or somewhere in your search path. Note that you only need the stand-alone EXE file for most tasks.

The following sections cover a few of the ways you can edit video files with FFmpeg.

Trimming video

One of the most common video editing needs is trimming a video — removing extraneous material that was recorded before or after the subject of the video. For example, in a recording of a presentation you might want to trim the beginning so that the speaker’s voice begins a second or two after the video starts, or trim the end to eliminate extra material after the speaker has finished.

Here's an example of an FFmpeg command to trim a video:

ffmpeg -i inputfile.mp4 -c copy -ss 00:05:00 -t 00:22:00 outfile.mp4

And here's an explanation of the various command line options used:

  • The -i inputfile.mp4 option specifies the input file.
  • Th -c copy option tells FFmpeg to simply copy the desired section of video without re-encoding it, which will preserve the quality of the original video with no degradation. (You could also specify a codec here if desired.)
  • The the -ss 00:05:00 option sets the start time for the trimmed output video, in HH:MM:SS format. So here we're saying to start at the 5-minute mark.
  • The -t 00:22:00 option specifies the duration of the output trimmed video, so we'll be extracting 22 minutes of video starting at the 5-minute mark. (This option uses t as an abbreviation for transcode duration. FFmpeg has more command-line options than there are letters in the alphabet, so some of the abbreviations aren't intuitively obvious.)
  • The final option specifies the output file, outfile.mp4.

Changing playback speed

Here's an example of an FFmpeg command to change the playback speed of a video:

ffmpeg -i inputfile.mp4 -filter:v "setpts=2.0" outfile.mp4

This example will make the video play back 2X slower – note the "setpts=2.0" argument. If you want to speed up a video, use a number less than 1; for example "setpts=0.5" will make the output video play back 2X faster than the original.

Concatenating videos

To combine multiple videos into a single video, first create a text file that lists the videos to be combined, in the order they should appear in the output file. Each input file should be on its own line in the format file 'filename.xxx' as shown in this example:

file 'file1.mp4'
file 'file2.mp4'
file 'file3.mp4'

Assuming that file were named videos.txt, here's the command to concatenate the videos into a single outfile.mp4 file:

ffmpeg -f concat -i videos.txt -c copy outfile.mp4

Cropping a video

You can use FFmpeg to extract a cropped portion of a video, by specifying the width and height of the desired region as well as the X/Y offset of the top left corner of the desired region using the -vf "crop=W:H:X:Y" command line option.

For example, here's an example I used to extract a 1440 wide by 1080 tall section from the top left corner (0:0) of a video:

ffmpeg -i inputfile.mp4 -vf "crop=1440:1080:0:0" -c:v libx264 -crf 17 -c:a copy outfile.mp4

Those settings work well for the MP4 files I capture with my Nikon D850, but you may need to make adjustments for other formats. The documentation is your friend.

Converting MOV to MP4

My older DSLRs captured MOV files, and I've often needed to convert those to MP4 for various purposes. Here's the command for doing that:

ffmpeg -i inputfile.mov -vcodec h264 -acodec aac -strict -2 outfile.mp4

FFmpeg supports many other video formats, that's just a specific example, You can also use the h265 codec instead of h264 if preferred; see the documentation for all the details.

Reducing resolution and file size

Modern DSLRs capture very high resolution and create huge video files that are inconvenient or impossible to post on social media platforms or send via email.

Here's a command I often use to reduce a 4K video to 960x720 resolution to make the file size much smaller:

ffmpeg -i inputfile.mp4 -vf scale=960:720 outfile.mp4 -hide_banner

The -hide_banner option isn't necessary, but I like to use it to keep the output a bit tidier. (By default, FFmpeg has extremely verbose console output.)

Creating time-lapse videos

One of my favorite uses for FFmpeg is creating time-lapse videos from a batch of still images. That's a more complex task than some of the basic editing scenarios covered above, so I've written a separate post covering in detail how to create time-lapse videos with FFmpeg.