1---2name: ffmpeg3description: Process video and audio with correct codec selection, filtering, and encoding settings.4---5
6## Input Seeking (Major Difference)
7
8- `-ss` BEFORE `-i`: fast seek, may be inaccurate—starts from nearest keyframe
9- `-ss` AFTER `-i`: frame-accurate but slow—decodes from start
10- Combine both: `-ss 00:30:00 -i input.mp4 -ss 00:00:05`—fast seek then accurate trim
11- For cutting, add `-avoid_negative_ts make_zero` to fix timestamp issues
12
13## Stream Selection
14
15- Default: first video + first audio—may not be what you want
16- Explicit selection: `-map 0:v:0 -map 0:a:1`—first video, second audio
17- All streams of type: `-map 0:a`—all audio streams
18- Copy specific: `-map 0 -c copy`—all streams, no re-encoding
19- Exclude: `-map 0 -map -0:s`—all except subtitles
20
21## Encoding Quality
22
23- CRF (Constant Rate Factor): lower = better quality, larger file—18-23 typical for H.264
24- `-preset`: ultrafast to veryslow—slower = smaller file at same quality
25- Two-pass for target bitrate: first pass analyzes, second pass encodes
26- `-crf` and `-b:v` mutually exclusive—use one or the other
27
28## Container vs Codec
29
30- Container (MP4, MKV, WebM): wrapper format holding streams
31- Codec (H.264, VP9, AAC): compression algorithm for stream
32- Not all codecs fit all containers—H.264 in MP4/MKV, not WebM; VP9 in WebM/MKV, not MP4
33- Copy codec to new container: `-c copy`—fast, no quality loss
34
35## Filter Syntax
36
37- Simple: `-vf "scale=1280:720"`—single filter chain
38- Complex: `-filter_complex "[0:v]scale=1280:720[scaled]"`—named outputs for routing
39- Chain filters: `-vf "scale=1280:720,fps=30"`—comma-separated
40- Filter order matters—scale before crop gives different result than crop before scale
41
42## Common Filters
43
44- Scale: `scale=1280:720` or `scale=-1:720` for auto-width maintaining aspect
45- Crop: `crop=640:480:100:50`—width:height:x:y from top-left
46- FPS: `fps=30`—change framerate
47- Trim: `trim=start=10:end=20,setpts=PTS-STARTPTS`—setpts resets timestamps
48- Overlay: `overlay=10:10`—position from top-left
49
50## Audio Processing
51
52- Sample rate: `-ar 48000`—standard for video
53- Channels: `-ac 2`—stereo
54- Audio codec: `-c:a aac -b:a 192k`—AAC at 192kbps
55- Normalize: `-filter:a loudnorm`—EBU R128 loudness normalization
56- Extract audio: `-vn -c:a copy output.m4a`—no video, copy audio
57
58## Concatenation
59
60- Same codec/params: concat demuxer—`-f concat -safe 0 -i list.txt -c copy`
61- Different formats: concat filter—`-filter_complex "[0:v][0:a][1:v][1:a]concat=n=2:v=1:a=1"`
62- list.txt format: `file 'video1.mp4'` per line—escape special characters
63- Different resolutions: scale/pad to match before concat filter
64
65## Subtitles
66
67- Burn-in (hardcode): `-vf "subtitles=subs.srt"`—cannot be turned off
68- Mux as stream: `-c:s mov_text` (MP4) or `-c:s srt` (MKV)—user toggleable
69- From input: `-map 0:s`—include subtitle streams
70- Extract: `-map 0:s:0 subs.srt`—first subtitle to file
71
72## Hardware Acceleration
73
74- Decode: `-hwaccel cuda` or `-hwaccel videotoolbox` (macOS)
75- Encode: `-c:v h264_nvenc` (NVIDIA), `-c:v h264_videotoolbox` (macOS)
76- Not always faster—setup overhead; benefits show on long videos
77- Quality may differ—software encoding often produces better quality
78
79## Common Mistakes
80
81- Forgetting `-c copy` when not re-encoding—defaults to re-encode, slow and lossy
82- `-ss` after `-i` for long videos—takes forever seeking
83- Audio desync after cutting—use `-async 1` or `-af aresample=async=1`
84- Filter on stream copy—filters require re-encoding; `-c copy` + `-vf` = error
85- Output extension doesn't set codec—`output.mp4` without `-c:v` uses default, may not be H.264