Learn
FFmpeg
Guides
Syntax and Examples

Mastering FFmpeg Command Syntax: An In-Depth Guide

FFmpeg is a powerful tool for handling multimedia data. Its command-line interface allows for a wide range of operations, including conversion, encoding, filtering, and more. This guide will delve deep into the FFmpeg command syntax, providing examples that cover complex scenarios and edge-cases.

Understanding FFmpeg Command Syntax

The basic FFmpeg command syntax is as follows:

ffmpeg [global_options] {[input_file_options] -i input_url}... {[output_file_options] output_url}...
  • global_options: These are options that apply to the entire operation, such as logging level.
  • input_file_options: These are options that apply to the input file(s), such as codecs, filters, etc.
  • -i input_url: This is the input file(s). You can specify multiple input files.
  • output_file_options: These are options that apply to the output file(s), such as codecs, filters, etc.
  • output_url: This is the output file.

Basic Conversion

The most basic use of FFmpeg is to convert media files from one format to another. Here's an example of converting an MP3 file to an OGG file:

ffmpeg -i input.mp3 output.ogg

And here's an example of converting an MP4 video to a WebM video:

ffmpeg -i input.mp4 output.webm

Selecting Codecs

You can specify the codecs for the input and output files using the -c option. Here's an example of converting an MP3 file to an OGG file using the libvorbis audio codec:

ffmpeg -i input.mp3 -c:a libvorbis output.ogg

And here's an example of converting an MP4 video to an MKV video using the vp9 video codec and the libvorbis audio codec:

ffmpeg -i input.mp4 -c:v vp9 -c:a libvorbis output.mkv

Using filters

You can apply filters to the input and output files using the -vf option. Here's an example of converting an MP4 video to an MKV video using the scale filter to resize the video:

ffmpeg -i input.mp4 -vf scale=640:480 output.mkv

Complex filters

You can apply complex filters to the input and output files using the -filter_complex option. Here's an example of converting an MP4 video to an MKV video using the scale filter to resize the video and the overlay filter to overlay an image on top of the video:

ffmpeg -i input.mp4 -filter_complex "[0:v]scale=640:480[v0];[v0][1:v]overlay=0:0" output.mkv

The above command uses the overlay filter to overlay the second video stream on top of the first video stream.

Changing a Single Stream

You can change a single stream in a media file using the -c option with a stream specifier. Here's an example of changing the audio stream in a WebM video to FLAC:

ffmpeg -i input.webm -c:v copy -c:a flac output.mkv

In this command, -c:v copy tells FFmpeg to copy the video stream as-is, and -c:a flac tells FFmpeg to encode the audio stream using the FLAC codec.

Influencing the Quality

You can influence the quality of the output file using various options. For example, you can change the bitrate of the video using the -b option:

ffmpeg -i input.webm -c:a copy -c:v vp9 -b:v 1M output.mkv

In this command, -b:v 1M sets the video bitrate to 1 megabit per second.

You can also adjust the frame rate of the video using the -r option:

ffmpeg -i input.webm -c:a copy -c:v vp9 -r 30 output.mkv

In this command, -r 30 sets the frame rate to 30 frames per second.

Modifying the Streams

FFmpeg allows you to modify the streams in a media file in various ways. For example, you can trim a video using the -ss and -t options:

ffmpeg -i input.mkv -c:av copy -ss 00:01:00 -t 10 output.mkv

In this command, -ss 00:01:00 sets the start time to 1 minute into the video, and -t 10 sets the duration to 10 seconds.

Extracting the Audio

You can extract the audio from a video file using the -vn option:

ffmpeg -i input.mkv -vn audio_only.ogg

In this command, -vn tells FFmpeg to ignore the video stream.

Making a GIF out of a Video

You can convert a video clip into an animated GIF using FFmpeg:

ffmpeg -i input.mkv output.gif

Conclusion

FFmpeg is a powerful and flexible tool for handling multimedia data. Its command syntax may seem daunting at first, but with a bit of practice, you can master it and handle even the most complex scenarios and edge-cases.