ffmpeg media
it converts literally anything to literally anything else.
The ffmpeg documentation is not a man page. It’s a novel. Multiple volumes. With appendices. It supports every media format that has ever existed, some that shouldn’t exist, and a few that were invented specifically because ffmpeg needed a challenge.
You wanted to convert a video file. So you Googled “convert MKV to MP4.” You clicked the first result. It was a website called something like “FreeVideoConverterOnline.io.” It had seventeen ads. Three fake download buttons. A popup asking for notification permissions. You uploaded your 2GB file over your internet connection. You waited. It gave you a 144p watermarked preview and asked for $9.99 to remove the watermark. You closed the tab. You downloaded HandBrake. It was 40MB. It had a queue system, presets for devices you don’t own, a “chapters” tab, and an “Audio” tab with a dropdown that said “AAC (avcodec)” and you didn’t know what any of that meant. You clicked “Start Encode” and waited 45 minutes.
Meanwhile, ffmpeg has been on your machine — or one apt install away — doing this same thing in one command since 2000.
Unless you’re running Windows — then what the hell are you doing here anyways?! If you want to join the party and understand what the hell we’re talking about, go install WSL2. We’ll wait. Impatiently.
Lazy sysadmins can skip straight to the cheat sheet.
Install it first
Unlike the other tools on this site, ffmpeg might not be pre-installed. Fix that:
# Debian/Ubuntu
sudo apt install ffmpeg
# Fedora
sudo dnf install ffmpeg
# macOS
brew install ffmpeg
Verify it’s there:
ffmpeg -version
If you see a version number and a wall of --enable flags, you’re good. If you see “command not found,” re-read the three lines above. Slowly.
Convert a video (the thing you Googled)
MKV to MP4. The reason 60% of you are here.
ffmpeg -i input.mkv output.mp4
That’s it. ffmpeg looks at the input, looks at the output extension, and figures out what you want. It’s smarter than every online converter you’ve ever used, and it didn’t ask for your email address first.
Convert and pick your codec
ffmpeg -i input.mkv -c:v libx264 -c:a aac output.mp4
-c:v is the video codec, -c:a is the audio codec. libx264 and aac will play on literally everything — your phone, your TV, your fridge if it has a screen. This is the safe combo.
Convert without re-encoding (fast)
If the codecs are already compatible and you just need a different container:
ffmpeg -i input.mkv -c copy output.mp4
-c copy means “don’t re-encode anything, just repackage it.” This takes seconds instead of minutes because it’s not actually processing the video. It’s just moving data from one box to another. Like putting your leftovers in a different container. Same food. Different tupperware.
Extract audio from a video
The thing you opened an entire audio editor for.
ffmpeg -i video.mp4 -vn audio.mp3
-vn means “no video.” That’s all. You just ripped the audio from a video. Audacity would have made you import the file, wait for the waveform to render, then File > Export > choose format > fill in metadata tags > click through two dialogs > save.
Extract audio without re-encoding
If the audio is already in the format you want:
ffmpeg -i video.mp4 -vn -c:a copy audio.aac
Instant. No quality loss. Just pull the audio stream out of the container.
Trim a video (cut a section)
The thing you were about to open iMovie for.
ffmpeg -i input.mp4 -ss 00:01:30 -to 00:03:45 -c copy output.mp4
-ss is the start time. -to is the end time. -c copy makes it instant because it’s not re-encoding. You just cut a two-minute clip from a video without installing a video editor. Your coworker is still waiting for Premiere Pro to load.
Using duration instead of end time
ffmpeg -i input.mp4 -ss 00:01:30 -t 60 output.mp4
-t 60 means “grab 60 seconds starting from the -ss point.” Use whichever makes more sense for what you’re doing.
Resize a video
ffmpeg -i input.mp4 -vf scale=1280:720 output.mp4
-vf means “video filter.” scale=1280:720 is the resolution. You just downscaled a 4K video to 720p. HandBrake has a “Dimensions” tab with width fields, height fields, anamorphic dropdowns, cropping options, and a “Preview” button. ffmpeg has scale=W:H.
Keep the aspect ratio
ffmpeg -i input.mp4 -vf scale=1280:-1 output.mp4
-1 tells ffmpeg to calculate the height automatically to maintain the aspect ratio. Because you shouldn’t have to do math to resize a video.
Compress a video (reduce file size)
Your video is 4GB. It shouldn’t be.
ffmpeg -i input.mp4 -c:v libx264 -crf 23 output.mp4
-crf is the magic flag. It stands for “Constant Rate Factor” and controls quality vs. size:
- 18 — visually lossless. Large files. For perfectionists.
- 23 — default. Good quality. Reasonable size. For normal people.
- 28 — smaller files. Noticeable quality loss. For “I need to email this.”
- 32+ — you hate your audience.
Lower number = better quality = bigger file. Higher number = worse quality = smaller file. That’s it. HandBrake has this exact same slider buried under three tabs. You’re welcome.
Two-pass encoding (when quality matters)
ffmpeg -i input.mp4 -c:v libx264 -b:v 2M -pass 1 -f null /dev/null
ffmpeg -i input.mp4 -c:v libx264 -b:v 2M -pass 2 output.mp4
First pass analyzes the video. Second pass encodes it. This gives you a specific bitrate (2M = 2 megabits/sec) with better quality distribution. Use this for final output when you care.
Convert audio formats
Because apparently iTunes still can’t figure out FLAC.
ffmpeg -i song.flac song.mp3
FLAC to MP3. One command. No media player. No “import library.” No iTunes.
Specify quality
ffmpeg -i song.flac -b:a 320k song.mp3
-b:a 320k sets the audio bitrate to 320kbps. The highest quality MP3 you can make. Audiophiles will still complain.
WAV to FLAC, because you hoard lossless audio
ffmpeg -i recording.wav recording.flac
Lossless compression. Same quality, half the size. Your NAS drive just sighed with relief.
Make a GIF from a video
The thing you used some sketchy online tool for.
ffmpeg -i input.mp4 -ss 00:00:05 -t 3 -vf "fps=15,scale=480:-1" output.gif
That grabs 3 seconds starting at the 5-second mark, at 15fps, scaled to 480px wide. It’s a GIF. From a video. One command. The online tool would have watermarked it, limited it to 5 seconds, and asked you to create an account for “HD quality.”
Extract a single frame (thumbnail)
ffmpeg -i input.mp4 -ss 00:00:30 -frames:v 1 thumbnail.jpg
One frame. At the 30-second mark. Saved as a JPEG. You were about to open the video in VLC, pause it, and take a screenshot, weren’t you? Don’t lie.
Combine multiple files
Concatenate videos
Create a text file called list.txt:
file 'part1.mp4'
file 'part2.mp4'
file 'part3.mp4'
Then:
ffmpeg -f concat -safe 0 -i list.txt -c copy output.mp4
Three videos are now one video. No video editor. No timeline. No dragging clips around.
Add audio to a video
ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -c:a aac -shortest output.mp4
-shortest stops when the shorter input ends. You just did a voiceover without opening a video editor.
Change the framerate
ffmpeg -i input.mp4 -r 30 output.mp4
-r 30 sets 30fps. For when your 60fps screen recording doesn’t need to be 60fps and you’d rather have a smaller file.
Strip audio from a video
The opposite of extracting audio.
ffmpeg -i input.mp4 -an output.mp4
-an means “no audio.” For when the video has background noise, confidential conversation, or your coworker’s Spotify playlist.
The flags that actually matter
| Flag | What it does |
|---|---|
-i FILE |
Input file. Always required. |
-c:v CODEC |
Video codec. libx264 for H.264, libx265 for H.265, copy to skip encoding. |
-c:a CODEC |
Audio codec. aac, libmp3lame, copy to skip encoding. |
-c copy |
Copy everything without re-encoding. Fast. |
-crf N |
Quality control (18-28 range). Lower = better quality, bigger file. |
-b:v RATE |
Video bitrate. 2M = 2 megabits/sec. |
-b:a RATE |
Audio bitrate. 320k = 320 kilobits/sec. |
-vf FILTER |
Video filter. scale=W:H, fps=N, etc. |
-ss TIME |
Start time. 00:01:30 or 90 (seconds). |
-to TIME |
End time. |
-t DURATION |
Duration in seconds. |
-vn |
No video (audio only). |
-an |
No audio (video only). |
-r FPS |
Set framerate. |
-frames:v N |
Capture N frames. |
-y |
Overwrite output without asking. For when you’re sure. |
“But HandBrake—”
Sure.
“HandBrake is free and open source.” It is. It’s also a GUI wrapper around ffmpeg’s libraries. You’re installing a 40MB application with tabs, presets, and a queue system to access the same encoding engine that ffmpeg gives you from the command line. You put a middle manager between yourself and the thing that does the work.
“I need presets for my device.” ffmpeg with -c:v libx264 -c:a aac plays on everything. Phones, tablets, smart TVs, game consoles, that screen on your fridge. The “Apple TV 4K” preset in HandBrake outputs… H.264 with AAC. The exact same thing.
“Adobe Media Encoder integrates with Premiere.” Adobe Media Encoder costs $22.99/month. Per month. To encode video. ffmpeg is free. It was free yesterday. It’ll be free tomorrow. It doesn’t need your Creative Cloud login. It doesn’t need to “check for updates” every time you open it. It doesn’t need to phone home to verify your subscription is active before it lets you convert an MP4.
“Format Factory supports everything.” Format Factory is a Windows-only application that bundles adware in its installer and has been flagged by multiple antivirus tools. You downloaded a program that your antivirus doesn’t trust to convert a video file. Think about that for a second.
“Online converters are convenient.” You uploaded a 2GB file to a server you don’t control, run by a company you’ve never heard of, in a country you can’t identify, and then downloaded the result. For a video file. That might contain personal content. That server still has your file. Probably. Convenient.
“VLC can convert files too.” VLC can do a lot of things. It can also crash doing a lot of things. Its conversion interface is buried under Media > Convert/Save, then a dialog with an “Add” button, then a “Profile” dropdown with names like “Video - H.264 + MP3 (MP4),” and then an output file selector. ffmpeg is one line. Typed. Done.
Cheat sheet
You made it. Or you skipped straight here. Either way, no judgment. Copy and paste these. Pin them. Tattoo them on your forearm. Whatever works.
| What you’re doing | Command |
|---|---|
| Convert format (simple) | ffmpeg -i input.mkv output.mp4 |
| Convert with codec | ffmpeg -i input.mkv -c:v libx264 -c:a aac output.mp4 |
| Remux without re-encoding | ffmpeg -i input.mkv -c copy output.mp4 |
| Extract audio | ffmpeg -i video.mp4 -vn audio.mp3 |
| Trim/cut video | ffmpeg -i input.mp4 -ss 00:01:30 -to 00:03:45 -c copy output.mp4 |
| Resize video | ffmpeg -i input.mp4 -vf scale=1280:720 output.mp4 |
| Compress video | ffmpeg -i input.mp4 -c:v libx264 -crf 23 output.mp4 |
| Convert audio format | ffmpeg -i song.flac song.mp3 |
| High quality MP3 | ffmpeg -i song.flac -b:a 320k song.mp3 |
| Make a GIF | ffmpeg -i input.mp4 -ss 5 -t 3 -vf "fps=15,scale=480:-1" output.gif |
| Extract thumbnail | ffmpeg -i input.mp4 -ss 30 -frames:v 1 thumb.jpg |
| Strip audio | ffmpeg -i input.mp4 -an output.mp4 |
| Change framerate | ffmpeg -i input.mp4 -r 30 output.mp4 |
| Concatenate videos | ffmpeg -f concat -safe 0 -i list.txt -c copy output.mp4 |
The pattern:
ffmpeg -i input [options] output. Input first, options in the middle, output last. ffmpeg figures out the format from the file extension. It’s smarter than you think.