▶ Video & streaming

FFmpeg for transcoding: the essential commands explained

How an FFmpeg command is structured, choosing codec and quality, scaling, trimming, extracting audio and mapping tracks, with ready-to-use examples.

FFmpeg is the command-line tool behind a huge share of existing video software: many converters, players and online services use it internally. Learning the basics lets you do in seconds what a graphical interface would take many steps to achieve, and above all lets you know exactly what happens to the file. There is no need to memorise hundreds of options: a clear structure and a dozen recurring commands are enough.

The structure of a command

An FFmpeg command always follows the same pattern:

ffmpeg [input options] -i input [output options] output

The most important rule is that options apply to the file that follows them. An option written before -i concerns the input; written after, it concerns the output. Many mistakes come from options in the wrong place.

The most common options have a suffix indicating the stream type: :v for video, :a for audio, :s for subtitles. For example, -c:v selects the video codec and -b:a the audio bitrate.

First of all: look at what is in the file

Before converting it is worth knowing what you are dealing with:

ffprobe -hide_banner video.mp4

The output lists each stream with codec, resolution, frame rate and language. From there you decide whether a remux is enough or a re-encode is needed.

Standard conversion for maximum compatibility

ffmpeg -i input.mov -c:v libx264 -preset slow -crf 21 -pix_fmt yuv420p -c:a aac -b:a 160k -movflags +faststart output.mp4

What each part does:

  • -c:v libx264: encodes the video as H.264;
  • -preset slow: takes more time to compress better; presets range from ultrafast to veryslow;
  • -crf 21: sets the quality level;
  • -pix_fmt yuv420p: uses the most compatible pixel format;
  • -c:a aac -b:a 160k: AAC audio at 160 kbps;
  • -movflags +faststart: moves the index to the start of the file, so online playback begins before the whole file has downloaded.

Copying without re-encoding

If the codec is fine and you only need to change container:

ffmpeg -i input.mkv -c copy output.mp4

You can also copy one stream and re-encode another, for example keep the video and convert only the audio:

ffmpeg -i input.mkv -c:v copy -c:a aac -b:a 192k output.mp4

Scaling

The scale filter changes resolution. By giving -2 for one dimension, FFmpeg calculates the other while keeping the aspect ratio and rounding to an even number, which many codecs require:

ffmpeg -i input.mp4 -vf "scale=1280:-2" -c:v libx264 -crf 22 -c:a copy output_720p.mp4

Trimming an excerpt

ffmpeg -ss 00:01:30 -i input.mp4 -t 00:00:45 -c:v libx264 -crf 20 -c:a aac excerpt.mp4

-ss before -i starts reading quickly from the given point, -t sets the duration. When re-encoding, the cut is frame-accurate. With -c copy it is much faster but the start point shifts to the nearest keyframe.

Extracting or replacing audio

Extracting audio without re-encoding it, if the codec is AAC:

ffmpeg -i video.mp4 -vn -c:a copy audio.m4a

Replacing a video's audio with another file:

ffmpeg -i video.mp4 -i new_audio.wav -map 0:v -map 1:a -c:v copy -c:a aac -shortest output.mp4

Mapping tracks

By default FFmpeg automatically picks only one video stream and one audio stream, usually those with the highest quality. If a file contains several languages or subtitles, they must be selected explicitly with -map:

ffmpeg -i film.mkv -map 0:v:0 -map 0:a:0 -map 0:a:1 -map 0:s:0 -c copy output.mkv

The syntax 0:a:1 means «from the first input file, the second audio stream» (numbering starts at zero). With -map 0 all streams are copied.

Re-encoding a whole folder

On Linux and macOS a shell loop converts every file in a folder:

for f in *.mov; do ffmpeg -i "$f" -c:v libx264 -crf 21 -c:a aac "${f%.mov}.mp4"; done

The quotes around the names avoid problems with spaces.

Habits that prevent mistakes

  1. Never overwrite the original: always write to a new file.
  2. Test on an excerpt of 20-30 seconds before converting a long file.
  3. Read the output: FFmpeg flags warnings and errors in yellow and red, often with precise hints.
  4. Check the result with ffprobe, not just by watching it.

In short

  • Options apply to the file that follows them: before -i the input, after it the output.
  • Use ffprobe to understand what a file contains before converting it.
  • -c copy changes container losslessly; re-encoding is needed to change codec or quality.
  • -map explicitly selects the tracks to include.
  • Always test on an excerpt and never overwrite the original.
MM
Marco Marzaduri

Marco Marzaduri works in cybersecurity, digital forensics and web development. A computer science graduate, he combines rigorous technical analysis with attention to the people who use technology.

Read also