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:
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.
ffmpeg -i input.mkv -map 0:v:0 -map 0:a:0 -c copy output.mkvStream 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:
ffmpeg -i input.mkv -c copy output.mp4This is the fastest path when the target container accepts the existing codecs.
Transcode #
Re-encode when the output format requires a different codec:
ffmpeg -i input.avi -c:v libx264 -c:a aac output.mp4Trim #
Use -ss and -t or -to to cut a clip:
ffmpeg -ss 30 -i input.mp4 -t 60 -c copy clip.mp4When accuracy matters, place -ss after -i; when speed matters, place it before -i.
Extract Audio #
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:
ffmpeg -i input.mp4 -vf "scale=-2:720,format=yuv420p" output.mp4Complex filtergraphs combine multiple inputs:
ffmpeg -i main.mp4 -i overlay.png -filter_complex "[0:v][1:v]overlay=10:10" output.mp4The 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:
ffprobe -hide_banner -show_streams -show_format input.mkvThis is the fastest way to check stream indices, codec names, durations, and metadata before writing the ffmpeg command.
Practical Flags #
-shorteststops when the shortest stream ends;-movflags +faststartmoves MP4 metadata to the front;-yoverwrites output files;-hide_bannerkeeps script output short;-loglevel errorsuppresses informational noise.
Comments
Read or comment on GitHub (sign-in required to comment)