FFmpeg
Quick Reference
Section titled “Quick Reference”ffmpeg [global options] -i <input> [output options] <output>FFmpeg figures out formats from file extensions. Most of the work is specifying codecs, quality, and filters.
Common Conversions
Section titled “Common Conversions”Video format conversion
Section titled “Video format conversion”# MKV to MP4 (re-encode)ffmpeg -i input.mkv output.mp4
# MKV to MP4 (copy streams — fast, no quality loss)ffmpeg -i input.mkv -c copy output.mp4
# MOV to MP4ffmpeg -i input.mov -c:v libx264 -c:a aac output.mp4-c copy copies streams without re-encoding. Use this when you just need a container change — it’s instant and lossless. It fails if the target container doesn’t support the source codecs.
Audio extraction
Section titled “Audio extraction”# Extract audio as-isffmpeg -i video.mp4 -vn -c:a copy audio.aac
# Extract and convert to MP3ffmpeg -i video.mp4 -vn -q:a 2 audio.mp3
# Extract and convert to FLACffmpeg -i video.mp4 -vn audio.flac-vn = no video. -q:a 2 = variable bitrate quality (0=best, 9=worst for MP3).
Audio conversion
Section titled “Audio conversion”# WAV to MP3ffmpeg -i input.wav -b:a 320k output.mp3
# FLAC to MP3ffmpeg -i input.flac -b:a 320k output.mp3
# MP3 to OGGffmpeg -i input.mp3 -c:a libvorbis -q:a 6 output.oggTrimming and Cutting
Section titled “Trimming and Cutting”# Cut from 00:01:30 to 00:04:00ffmpeg -i input.mp4 -ss 00:01:30 -to 00:04:00 -c copy output.mp4
# Cut 90 seconds starting at 00:01:30ffmpeg -i input.mp4 -ss 00:01:30 -t 90 -c copy output.mp4-ss = start time. -to = end time. -t = duration. Placing -ss before -i seeks faster (input seeking) but can be less accurate at keyframes.
Resizing and Scaling
Section titled “Resizing and Scaling”# Scale to 1280x720ffmpeg -i input.mp4 -vf scale=1280:720 output.mp4
# Scale width to 1280, auto-calculate height (keep aspect ratio)ffmpeg -i input.mp4 -vf scale=1280:-2 output.mp4
# Scale to 50%ffmpeg -i input.mp4 -vf scale=iw/2:ih/2 output.mp4Use -2 instead of -1 for auto-dimension — it rounds to even numbers, which many codecs require.
Quality and Encoding
Section titled “Quality and Encoding”H.264 (libx264)
Section titled “H.264 (libx264)”# CRF mode (constant quality) — lower = better, 18-28 is sane rangeffmpeg -i input.mp4 -c:v libx264 -crf 23 -preset medium output.mp4
# Presets: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow# Slower presets = better compression at same quality = smaller files = longer encodeH.265/HEVC (libx265)
Section titled “H.265/HEVC (libx265)”ffmpeg -i input.mp4 -c:v libx265 -crf 28 output.mp4~50% smaller files than H.264 at similar quality. Slower to encode. CRF scale differs — 28 in H.265 is roughly equivalent to 23 in H.264.
Useful Options
Section titled “Useful Options”| Flag | Purpose |
|---|---|
-c:v | Video codec (libx264, libx265, copy) |
-c:a | Audio codec (aac, libmp3lame, copy) |
-c copy | Copy all streams (no re-encoding) |
-b:v 5M | Video bitrate |
-b:a 320k | Audio bitrate |
-r 30 | Output frame rate |
-an | Strip audio |
-vn | Strip video |
-sn | Strip subtitles |
-y | Overwrite output without asking |
-n | Never overwrite |
-map 0 | Include all streams from first input |
-metadata title="Name" | Set metadata |
Recipes
Section titled “Recipes”Create GIF from video
Section titled “Create GIF from video”ffmpeg -i input.mp4 -ss 5 -t 3 -vf "fps=15,scale=480:-2" -loop 0 output.gifAdd subtitles (burn in)
Section titled “Add subtitles (burn in)”ffmpeg -i input.mp4 -vf subtitles=subs.srt output.mp4Concatenate files
Section titled “Concatenate files”Create a text file list.txt:
file 'part1.mp4'file 'part2.mp4'file 'part3.mp4'ffmpeg -f concat -safe 0 -i list.txt -c copy output.mp4Extract frames
Section titled “Extract frames”# One frame per secondffmpeg -i input.mp4 -vf fps=1 frame_%04d.png
# Single frame at timestampffmpeg -i input.mp4 -ss 00:00:30 -frames:v 1 thumbnail.pngCompress for web
Section titled “Compress for web”ffmpeg -i input.mp4 -c:v libx264 -crf 28 -preset slow -c:a aac -b:a 128k -movflags +faststart output.mp4-movflags +faststart moves the metadata to the beginning of the file so browsers can start playing before the full download.
Probe / Inspect
Section titled “Probe / Inspect”# Show file infoffprobe input.mp4
# JSON output (detailed)ffprobe -v quiet -print_format json -show_format -show_streams input.mp4
# Just durationffprobe -v quiet -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 input.mp4