FFmpeg User Guide

Last update: 2021-05-09 05:38:32 +0800 +0800
tags: Ffmpeg | Media | Video
Available translations: 日本語 / 中文(简体) / 中文(繁體)

ffmpeg is a media pipeline tool. It reads one or more inputs, optionally filters the decoded frames, and writes one or more outputs.

Pipeline Model #

ffmpeg builds a pipeline from these components:

  • demuxers read container data and expose encoded packets;
  • decoders turn packets into raw frames;
  • filtergraphs transform raw audio or video frames;
  • encoders turn filtered frames back into packets;
  • muxers interleave packets into the output container.

That model explains why the command line is sensitive to option order and why stream selection matters.

Command Line Order #

The command line is positional:

sh
ffmpeg [global_options] [input_options] -i input ... [output_options] output ...

Rules that matter in practice:

  • input options go before the matching -i;
  • output options go before the matching output URL;
  • global options go first;
  • options are reset between files.

Stream Selection #

ffmpeg can choose streams automatically, but explicit mapping is safer for nontrivial jobs.

sh
ffmpeg -i input.mkv -map 0:v:0 -map 0:a:0 -c copy output.mkv

Stream specifiers such as 0:v:0 select the first video stream from the first input. Use them when the container has multiple audio tracks, subtitles, or attached pictures.

Common Operations #

Stream Copy #

Copy a stream without decoding or re-encoding it:

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

This is the fastest path when the target container accepts the existing codecs.

Transcode #

Re-encode when the output format requires a different codec:

sh
ffmpeg -i input.avi -c:v libx264 -c:a aac output.mp4

Trim #

Use -ss and -t or -to to cut a clip:

sh
ffmpeg -ss 30 -i input.mp4 -t 60 -c copy clip.mp4

When accuracy matters, place -ss after -i; when speed matters, place it before -i.

Extract Audio #

sh
ffmpeg -i input.mp4 -vn -c:a copy output.m4a

-vn drops video, and -c:a copy avoids re-encoding the audio stream.

Filtering #

Simple filters are attached to one stream:

sh
ffmpeg -i input.mp4 -vf "scale=-2:720,format=yuv420p" output.mp4

Complex filtergraphs combine multiple inputs:

sh
ffmpeg -i main.mp4 -i overlay.png -filter_complex "[0:v][1:v]overlay=10:10" output.mp4

The filtergraph syntax belongs to ffmpeg-filters(1). Commas separate filters in a chain, and semicolons separate chains.

Probing #

Use ffprobe when the input layout is unclear:

sh
ffprobe -hide_banner -show_streams -show_format input.mkv

This is the fastest way to check stream indices, codec names, durations, and metadata before writing the ffmpeg command.

Practical Flags #

  • -shortest stops when the shortest stream ends;
  • -movflags +faststart moves MP4 metadata to the front;
  • -y overwrites output files;
  • -hide_banner keeps script output short;
  • -loglevel error suppresses informational noise.

References #

Comments

Read or comment on GitHub (sign-in required to comment)