An FFmpeg command is written in this order: global options, then inputs, then outputs. Where you put an option decides which file it applies to.
Overview of the Basic Syntax
The official documentation gives the syntax as:
ffmpeg [global options] {[input options] -i input_url} ... {[output options] output_url} ...
Here is a real command:
ffmpeg -y -i input.mp4 -c:v libx264 -c:a aac output.mkv
| Part | Description |
|---|---|
ffmpeg |
The command itself |
-y |
Global option (overwrite output file without confirmation) |
-i input.mp4 |
Input file specification |
-c:v libx264 |
Output option (specify video codec as libx264) |
-c:a aac |
Output option (specify audio codec as AAC) |
output.mkv |
Output file (container is automatically determined from the extension) |
Global Options
Global options apply to the whole command. Write them before the inputs and outputs.
| Option | Description |
|---|---|
-y |
Overwrite the output file without confirmation if it already exists |
-n |
Never overwrite; exit immediately if the output file already exists |
-loglevel quiet |
Minimize log output |
-hide_banner |
Hide the version and other banner information |
Specifying Input (-i)
Put -i in front of each input file (official docs).
ffmpeg -i input.mp4 output.avi
For more than one input, use -i once for each:
ffmpeg -i input.mp4 -i audio.wav output.mkv
Write an input option (an option for an input file) right before that file’s
-i. If you write it after-i, FFmpeg treats it as an output option.
Specifying Output
Write the output file at the end of the command. Anything that FFmpeg cannot read as an option is treated as an output URL.
ffmpeg -i input.mkv output.mp4
Automatic Output Format Detection
FFmpeg normally picks the output format from the file extension. The official documentation says:
The format is normally auto detected for input files and guessed from the file extension for output files.
| Extension | Auto-selected container |
|---|---|
.mp4 |
MP4 (MPEG-4 Part 14) |
.mkv |
Matroska |
.avi |
AVI |
.mov |
QuickTime |
.webm |
WebM |
.mp3 |
MP3 |
.wav |
WAV |
If FFmpeg cannot tell the format from the extension, set it with -f:
ffmpeg -i input.mp4 -f matroska output
Option Ordering Rules (Most Important)
The official documentation says this about order:
As a general rule, options are applied to the next specified file. Therefore, order is important, and you can have the same option on the command line multiple times.
So an option applies to the file that comes right after it.
The Difference Between Input and Output Options
For example, -ss (start position) means different things before and after -i:
# -ss specified on the input side (fast; frame-accurate when re-encoding, keyframe-based only with -c copy)
ffmpeg -ss 10 -i input.mp4 output.mp4
# -ss specified on the output side (specifies the start position of the output; precise, but decodes and discards everything before it)
ffmpeg -i input.mp4 -ss 10 output.mp4
Do Not Mix Input and Output
# Not recommended: Input and output are interleaved
ffmpeg -i input.mp4 output1.mp4 -i input2.mp4
# Recommended: All inputs first, then all outputs
ffmpeg -i input.mp4 -i input2.mp4 output.mp4
Mixing them does not cause an error. But streams (the video and audio tracks) from input2.mp4 can still end up in output1.mp4, even though input2.mp4 comes after it. That makes the command easy to misread.
Specifying Codecs (-c, -c:v, -c:a)
-c picks the codec: the encoder for an output, or the decoder for an input.
| Option | Target | Examples |
|---|---|---|
-c:v |
Video codec | -c:v libx264, -c:v libvpx-vp9, -c:v copy |
-c:a |
Audio codec | -c:a aac, -c:a libmp3lame, -c:a copy |
-c |
All streams | -c copy |
copy is used a lot:
ffmpeg -i input.mkv -c copy output.mp4
-c copy copies the streams as they are, without re-encoding. Because nothing is decoded or encoded, it is very fast and there is no quality loss (official documentation).
Stream Selection (-map)
Without -map, FFmpeg picks one stream of each type by itself (official docs):
- Video: The stream with the highest resolution (within one file, a video stream marked as default is preferred)
- Audio: The stream with the most channels. Within one file, however, an audio stream marked as default is preferred.
To choose the streams yourself, use -map:
# Output video stream 0 and audio stream 0 from the first input
ffmpeg -i input.mp4 -map 0:v:0 -map 0:a:0 output.mp4
Common Syntax Mistakes
Mistake 1: Writing Global Options After Everything Else
Global options apply to the whole command wherever you write them, so this still works. But the official documentation says to put them first.
# Works, but not recommended: -y was written at the end
ffmpeg -i input.mp4 output.avi -y
# Recommended: Global options go at the beginning
ffmpeg -y -i input.mp4 output.avi
Mistake 2: Writing Output Options Before the Input
Wrong (FFmpeg reads -c:v libx264 as an input option and stops with an error):
ffmpeg -c:v libx264 -i input.mp4 output.mp4
Right (the codec goes after -i):
ffmpeg -i input.mp4 -c:v libx264 output.mp4
Mistake 3: Forgetting to Specify an Output File
Wrong (there is no output file, so FFmpeg stops with an error):
ffmpeg -i input.mp4 -c:v libx264
Right:
ffmpeg -i input.mp4 -c:v libx264 output.mp4
Practice: Syntax Verification Commands
These short commands all work. From top to bottom: convert to AVI; convert to H.264 and AAC in an MKV file, overwriting without asking; and copy the streams into MKV without re-encoding.
ffmpeg -i input.mp4 output.avi
ffmpeg -y -i input.mp4 -c:v libx264 -c:a aac output.mkv
ffmpeg -i input.mp4 -c copy output.mkv
For codec details and conversion recipes, see Convert Video to MP4 (H.264/AAC) with FFmpeg.