The cheatsheet
Basic format conversion
ffmpeg -i input.mp4 output.webm
Extracting audio from a video.
ffmpeg -i input.mp4 -vn output.mp3
Trim video (starting at 10 seconds):
ffmpeg -i input.mp4 -ss 00:00:10 -c copy output.mp4
Extracting subvideo from a video (from 5 minutes, lasting 60 seconds):
ffmpeg -i input.mp4 -ss 00:05:00 -t 60 -c copy output.mp4
Converting video and audio codecs (to x264 for video and aac for audio):
ffmpeg -i input.mp4 -c:v libx264 -c:a aac output.mp4
The explanation
ffmpeg is an amazing tool for manipulating audio and video. It’s a fantastic workhorse of a project. But that can be overwhelming. Using ffmpeg can feel like using an enormous space alien hammer to hammer a tupperware lid on its container. But, as complicated and vast ffmpeg’s capabilities are, it can be used by every-day developers and video creators, too.
So what can you, as a mere mortal, use ffmpeg for?
- changing video container formats (such as mkv, mov, mp4) of a video
- changing the video and audio codecs of a video/audio file (x264, aac, stuff like that)
- trimming a video
- extracting an audio from a video file
- merging together a video and an audio file
Changing the container/format
This was one of the harder things for me to understand at first. A video format is the “container” for your video and audio. I had no idea things worked this way but an MP4 file may have video and audio data within it that’s compatible on one device and incompatible in another and it’s still an mp4!
This is very unlike images where the jpg
and png
extensions tell you not only the format of the file but the encoding of the file (a .jpg
uses a jpg
) – there’s no separation.
The syntax to keep the audio/video data as is (utilizing the same “codec”) but change the container/format is fairly straightforward:
ffmpeg -i input.mp4 output.mov
ffmpeg will copy over the audio/video data into the new format/container. It’s almost like just changing the extension on the video (but you know, you can’t just do that)
Changing the audio/video codecs
The “codec” for a video or audio (they’re separate) is the encoding and decoding algorithm for the data. Think of how JPEG and PNG can show just about the same image but they can be very different sizes and you need completely different parsers for those formats.
That’s what a codec is here. The codec is specified using the -c
flag followed by a :
and then a
or v
depending on if you want to specify the audio or video codec. You can find out what codecs you have available on your machine via ffmpeg --codecs
. The most common codecs you’ll encounter for video will be:
libx264
forh.264
libx265
forh.265
And for audio:
aac
for theaac
codec common to mp4 filesmp3
for themp3
codec
ffmpeg -i input.mp4 -c:v libx264 -c:a aac output.mp4
Copying the codec
Sometimes, you just want to copy the codec without making any changes. For example, maybe you want to re-encode the video but keep the audio the same.
You can do this using the copy
codec input.
ffmpeg -i input.mp4 -c:v libx264 -c:a copy output.mp4
You can also do something like -c copy
to copy both audio and video codec.
What happens if I just leave the
-c
option out? Is it the same as specifyingcopy
?
Nope. If you don’t specify copy
, ffmpeg will use whatever default there is for the output format/container and re-encode it this way no matter the input. For mp4, that would probably be h264 for video and aac for audio. However, keep in mind the audio/video stream will be reencoded. Specifying copy
will actually just use the encoded audio/video in the input file.
Extract audio from video
Sometimes, you might want to extract the audio from a video file. That’s very straightforward with ffmpeg. You can specify an mp3 file (or wav) as the output and pass in a -vn
flag – which basically means “video none”.
ffmpeg -i input.mp4 -vn output.mp3
As with any ffmpeg command, you can add a bunch of options and when you don’t, ffmpeg will use defaults.
Trimming video
Trimming videos requires using the -ss
flag to mark the start of the trim and using the -t
flag to identify how long the video should go on from the start mark. ffmpeg will assume that you want to keep the rest of the video if you leave the -t
flag out.
So to remove the first 20 seconds of the video, the command would be:
ffmpeg -i input.mp4 -ss 00:00:20 output.mp4
To start the video at 20 seconds and go on for another 60 seconds, pass in the -t
flag
ffmpeg -i input.mp4 -ss 00:00:20 -t 60 output.mp4