FFmpeg Master
Comprehensive guide for professional video and audio manipulation using FFmpeg and FFprobe.
Core Concepts
FFmpeg is the leading multimedia framework, able to decode, encode, transcode, mux, demux, stream, filter and play almost anything that humans and machines have created. It is a command-line tool that processes streams through a complex pipeline of demuxers, decoders, filters, encoders, and muxers.
Common Operations
# Basic Transcoding (MP4 to MKV)
ffmpeg -i input.mp4 output.mkv
# Change Video Codec (to H.265/HEVC)
ffmpeg -i input.mp4 -c:v libx265 -crf 28 -c:a copy output.mp4
# Extract Audio (No Video)
ffmpeg -i input.mp4 -vn -c:a libmp3lame -q:a 2 output.mp3
# Resize/Scale Video
ffmpeg -i input.mp4 -vf "scale=1280:720" output.mp4
# Cut Video (Start at 10s, Duration 30s)
ffmpeg -i input.mp4 -ss 00:00:10 -t 00:00:30 -c copy output.mp4
# Fast Precise Cut (Re-encoding only the cut points is complex, so standard re-encoding is safer for precision)
ffmpeg -ss 00:00:10 -i input.mp4 -to 00:00:40 -c:v libx264 -crf 23 -c:a aac output.mp4
# Concatenate Files (using demuxer)
# Create filelist.txt: file 'part1.mp4' \n file 'part2.mp4'
ffmpeg -f concat -safe 0 -i filelist.txt -c copy output.mp4
# Speed Up/Slow Down Video (2x speed)
ffmpeg -i input.mp4 -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" output.mp4
Processing Categories & When to Use
Codecs & Quality
| Option |
Use When |
-c:v libx264 |
Standard H.264 encoding (best compatibility) |
-c:v libx265 |
H.265/HEVC encoding (best compression/quality) |
-crf [0-51] |
Constant Rate Factor (lower is higher quality, 18-28 recommended) |
-preset |
Encoding speed vs compression (ultrafast, medium, veryslow) |
-c:a copy |
Pass-through audio without re-encoding (saves time/quality) |
Filters & Manipulation
| Filter |
Use When |
scale |
Changing resolution (e.g., scale=1920:-1 for 1080p width) |
crop |
Removing edges (e.g., crop=w:h:x:y) |
transpose |
Rotating video (1=90deg CW, 2=90deg CCW) |
fps |
Changing frame rate (e.g., fps=30) |
drawtext |
Adding text overlays/watermarks |
overlay |
Picture-in-picture or adding image watermarks |
fade |
Adding fade-in/out effects (e.g., fade=in:0:30 for first 30 frames) |
volume |
Adjusting audio levels (e.g., volume=1.5 for 150% volume) |
setpts |
Changing video speed (e.g., setpts=0.5*PTS for double speed) |
atempo |
Changing audio speed without pitch shift (0.5 to 2.0) |
Inspection & Metadata
| Tool/Option |
Use When |
ffprobe -v error -show_format -show_streams |
Getting detailed technical info of a file |
-metadata title="Name" |
Setting global metadata tags |
-map |
Selecting specific streams (e.g., -map 0:v:0 -map 0:a:1) |
Advanced: Complex Filtergraphs
Use filter_complex when you need to process multiple inputs or create non-linear filter chains.
# Example: Adding a watermark at the bottom right
ffmpeg -i input.mp4 -i watermark.png -filter_complex "overlay=main_w-overlay_w-10:main_h-overlay_h-10" output.mp4
# Example: Vertical Stack (2 videos)
ffmpeg -i top.mp4 -i bottom.mp4 -filter_complex "vstack=inputs=2" output.mp4
# Example: Side-by-Side (2 videos)
ffmpeg -i left.mp4 -i right.mp4 -filter_complex "hstack=inputs=2" output.mp4
# Example: Grid (4 videos 2x2)
ffmpeg -i v1.mp4 -i v2.mp4 -i v3.mp4 -i v4.mp4 -filter_complex "[0:v][1:v]hstack=inputs=2[top];[2:v][3:v]hstack=inputs=2[bottom];[top][bottom]vstack=inputs=2" output.mp4
# Example: Fade Transition (Simple crossfade between two clips)
# Requires manual offset calculation, using xfade is better
ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "xfade=transition=fade:duration=1:offset=9" output.mp4
Hardware Acceleration
| Platform |
Codec |
Command |
| NVIDIA (NVENC) |
H.264 |
-c:v h264_nvenc |
| Intel (QSV) |
H.264 |
-c:v h264_qsv |
| Apple (VideoToolbox) |
H.265 |
-c:v hevc_videotoolbox |
Constraints & Error Handling
- Stream Mapping: Always use
-map for complex files to ensure you get the right audio/subtitle tracks.
- Seeking: Put
-ss before -i for fast seeking (input seeking), or after -i for accurate seeking (output seeking).
- Format Support: Ensure the output container (extension) supports the codecs you've chosen.
1---
2name: ffmpeg-master
3version: 1.0.0
4description: Use when performing video/audio processing tasks including transcoding, filtering, streaming, metadata manipulation, or complex filtergraph operations with FFmpeg.
5triggers:
6 - ffmpeg
7 - ffprobe
8 - video processing
9 - audio conversion
10 - codec
11 - transcoding
12 - filter_complex
13 - h264
14 - h265
15 - mp4
16 - mkv
17 - hardware acceleration
18role: specialist
19scope: implementation
20output-format: shell-command
21metadata:
22 source: https://skillmd.com/skills/modbender/ffmpeg-master
23---
24
25# FFmpeg Master
26
27Comprehensive guide for professional video and audio manipulation using FFmpeg and FFprobe.
28
29## Core Concepts
30
31FFmpeg is the leading multimedia framework, able to **decode, encode, transcode, mux, demux, stream, filter and play** almost anything that humans and machines have created. It is a command-line tool that processes streams through a complex pipeline of demuxers, decoders, filters, encoders, and muxers.
32
33## Common Operations
34
35```bash
36# Basic Transcoding (MP4 to MKV)
37ffmpeg -i input.mp4 output.mkv
38
39# Change Video Codec (to H.265/HEVC)
40ffmpeg -i input.mp4 -c:v libx265 -crf 28 -c:a copy output.mp4
41
42# Extract Audio (No Video)
43ffmpeg -i input.mp4 -vn -c:a libmp3lame -q:a 2 output.mp3
44
45# Resize/Scale Video
46ffmpeg -i input.mp4 -vf "scale=1280:720" output.mp4
47
48# Cut Video (Start at 10s, Duration 30s)
49ffmpeg -i input.mp4 -ss 00:00:10 -t 00:00:30 -c copy output.mp4
50
51# Fast Precise Cut (Re-encoding only the cut points is complex, so standard re-encoding is safer for precision)
52ffmpeg -ss 00:00:10 -i input.mp4 -to 00:00:40 -c:v libx264 -crf 23 -c:a aac output.mp4
53
54# Concatenate Files (using demuxer)
55# Create filelist.txt: file 'part1.mp4' \n file 'part2.mp4'
56ffmpeg -f concat -safe 0 -i filelist.txt -c copy output.mp4
57
58# Speed Up/Slow Down Video (2x speed)
59ffmpeg -i input.mp4 -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" output.mp4
60```
61
62---
63
64## Processing Categories & When to Use
65
66### Codecs & Quality
67| Option | Use When |
68|-----------|----------|
69| `-c:v libx264` | Standard H.264 encoding (best compatibility) |
70| `-c:v libx265` | H.265/HEVC encoding (best compression/quality) |
71| `-crf [0-51]` | Constant Rate Factor (lower is higher quality, 18-28 recommended) |
72| `-preset` | Encoding speed vs compression (ultrafast, medium, veryslow) |
73| `-c:a copy` | Pass-through audio without re-encoding (saves time/quality) |
74
75### Filters & Manipulation
76| Filter | Use When |
77|-----------|----------|
78| `scale` | Changing resolution (e.g., `scale=1920:-1` for 1080p width) |
79| `crop` | Removing edges (e.g., `crop=w:h:x:y`) |
80| `transpose` | Rotating video (1=90deg CW, 2=90deg CCW) |
81| `fps` | Changing frame rate (e.g., `fps=30`) |
82| `drawtext` | Adding text overlays/watermarks |
83| `overlay` | Picture-in-picture or adding image watermarks |
84| `fade` | Adding fade-in/out effects (e.g., `fade=in:0:30` for first 30 frames) |
85| `volume` | Adjusting audio levels (e.g., `volume=1.5` for 150% volume) |
86| `setpts` | Changing video speed (e.g., `setpts=0.5*PTS` for double speed) |
87| `atempo` | Changing audio speed without pitch shift (0.5 to 2.0) |
88
89### Inspection & Metadata
90| Tool/Option | Use When |
91|-----------|----------|
92| `ffprobe -v error -show_format -show_streams` | Getting detailed technical info of a file |
93| `-metadata title="Name"` | Setting global metadata tags |
94| `-map` | Selecting specific streams (e.g., `-map 0:v:0 -map 0:a:1`) |
95
96---
97
98## Advanced: Complex Filtergraphs
99
100Use `filter_complex` when you need to process multiple inputs or create non-linear filter chains.
101
102```bash
103# Example: Adding a watermark at the bottom right
104ffmpeg -i input.mp4 -i watermark.png -filter_complex "overlay=main_w-overlay_w-10:main_h-overlay_h-10" output.mp4
105
106# Example: Vertical Stack (2 videos)
107ffmpeg -i top.mp4 -i bottom.mp4 -filter_complex "vstack=inputs=2" output.mp4
108
109# Example: Side-by-Side (2 videos)
110ffmpeg -i left.mp4 -i right.mp4 -filter_complex "hstack=inputs=2" output.mp4
111
112# Example: Grid (4 videos 2x2)
113ffmpeg -i v1.mp4 -i v2.mp4 -i v3.mp4 -i v4.mp4 -filter_complex "[0:v][1:v]hstack=inputs=2[top];[2:v][3:v]hstack=inputs=2[bottom];[top][bottom]vstack=inputs=2" output.mp4
114
115# Example: Fade Transition (Simple crossfade between two clips)
116# Requires manual offset calculation, using xfade is better
117ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "xfade=transition=fade:duration=1:offset=9" output.mp4
118```
119
120## Hardware Acceleration
121
122| Platform | Codec | Command |
123|----------|-------|---------|
124| NVIDIA (NVENC) | H.264 | `-c:v h264_nvenc` |
125| Intel (QSV) | H.264 | `-c:v h264_qsv` |
126| Apple (VideoToolbox) | H.265 | `-c:v hevc_videotoolbox` |
127
128## Constraints & Error Handling
129
130- **Stream Mapping**: Always use `-map` for complex files to ensure you get the right audio/subtitle tracks.
131- **Seeking**: Put `-ss` *before* `-i` for fast seeking (input seeking), or *after* `-i` for accurate seeking (output seeking).
132- **Format Support**: Ensure the output container (extension) supports the codecs you've chosen.