© 2021-2024, 2026 by Zack Smith. All rights reserved.
What is FFMPEG?
FFMPEG is the Swiss Army Knife of video conversion and alteration. It can't do everything, but it can do quite a lot.
It is very much a manually-operated utility and requires using various command line options to achieve the desired effects and the best video output quality.
FFMPEG is accompanied by ffplay which is a video player built on top of
the same underlying code as ffmpeg.
To preview what ffmpeg's video filters, you can typically use ffplay with the same
options.
Useful commands
Remove audio from a video
Use the -an switch:
ffmpeg -i input.mp4 -vcodec copy -an output.mp4
Remove right audio channel from a video, making it a mono video
ffmpeg -i input.mp4 -af 'pan=mono|c0=c0' -c:v copy output.mp4
Increase audio volume of a video
ffmpeg -i input.mp4 -filter:a "volume=1.6" output.mp4
Extract audio from a video
ffmpeg -i input.mp4 output.wav
Or convert directly to MP3, setting the bit-rate:
ffmpeg -i input.mp4 -b:a 160k output.mp3
Or you can also convert WAV to MP3 with:
lame -V0 output.wav
This generates output.mp3.
Add audio onto existing in a video, mixing with original audio
ffmpeg -i input_video.mp4 -i new_audio.mp3 -filter_complex \
"[0:a][1:a]amix=inputs=2" -c:v copy output.mp4
Replace audio track of a video file with another one
Don't specify an audio codec.
ffmpeg -i video.mp4 -i audio.wav -vcodec copy -map 0:v -map 1:a output.mp4
Extract a segment of a video
The following extracts a segment starting at the 5 minute mark with a length of 1 minute:
ffmpeg -i video.mp4 -ss 5:00 -t 1:00 -c copy output.mp4
Drop/skip every other frame (speed up video)
ffmpeg -i video.mp4 -vf "select='not(mod(n,2))',setpts=N/FRAME_RATE/TB" output.mp4
Speed up a video by 2X
This works by changing timestamps:
ffmpeg -i video.mp4 -vf 'setpts=0.5*PTS' output.mp4
Crop a video
Specify the rectangle to crop to.
ffmpeg -i input.mp4 -filter:v "crop=width:height:x:y" output.mp4
Strip out metadata
ffmpeg -i input.mp4 -map_metadata -1 -c copy output.mp4
Stabilize a shaky video
This is an alternative to using the Windows program Virtualdub's deshaker function.
ffmpeg -i input.mp4 -vf deshake output.mp4
Sharpen a video
ffmpeg -i input.mp4 -vf unsharp=3:3:1.5 output.mp4
Increase video brightness
ffmpeg -i input.mp4 -vf eq=brightness=0.1 output.mp4
or
ffmpeg -i input.mp4 -vf lutyuv=y=val*2 output.mp4
To take a nighttime video and increase the brightness to where you can see people, use the video filter eq=brightness=0.5.
Note! To find the perfect brightness level, you can use this filter with ffplay thus:
ffplay -vf eq=brightness=0.5 input.mp4
Rotate a video by a multiple of 90 degrees
ffmpeg -i input.mp4 -vf "transpose=1" output.mp4
Where:
- 1 = 90 degrees clockwise
- 2 = 90 degrees counter-clockwise
Rotate a video by an arbitrary angle
ffmpeg -i input.mp4 -vf "rotate=angle*(PI/180)" output.mp4
As you can see, ffmpeg expects the angle to be in radians.
If you want to provide it in degrees, multiply by the PI/180 expression.
Prevent loss of video orientation info
Sometimes ffmpeg will automatically rotate a portrait video
to landscape without warning, because it removes
the video orientation data. To prevent that, use the -noautorotate flag
but put it before the input file.
ffmepg -noautorotate -i input ... output.mp4
Horizontally flip a video
ffmpeg -i input.mp4 -vf "hflip" output.mp4
Vertically flip a video
ffmpeg -i input.mp4 -vf "vflip" output.mp4
Concatenate videos with reencode
ffmpeg -i 1.mp4 -i 2.mp4 \
-filter_complex "[0:v] [0:a] [1:v] [1:a] concat=n=2:v=1:a=1 [v] [a]" \
-map "[v]" -map "[a]" -b 9M output.mp4
Concatenate videos without reencode
echo 'file 1.mp4' > files.txt
echo 'file 2.mp4' >> files.txt
ffmpeg -f concat -safe 0 -i files.txt -c copy output.mp4
Extact all frames from a video
ffmpeg -i input.mp4 '%05d.bmp'
Concatenate images (frames) to make a video
Let's say your images are IMG_1000.jpg with increasing numbers. This is useful for creating a time lapse or stop motion movie.
ffmpeg -i IMG_%04d.png -qscale:v 1 output.mov
In order to set the frame rate in this case, you need to specify it before the -i:
ffmpeg -framerate 60 -i img_%03d.png -qscale:v 1 output.mov
Play a video
FFMPEG includes the movie player ffplay. It's roughly similar
in quality to MPlayer.
ffplay myfile.mp4
Play a video with decreased brightness
ffplay -vf eq=brightness=-0.5:saturation=2 myfile.mp4
or
ffplay -vf lutyuv=y=val*0.5 myfile.mp4
Multiple commands
FFMPEG lets you combine multiple commands in series e.g.
-vf crop=100:100:0:0,rotate=45*PI/180,...
About h264 Video quality
Many computers and mobile devices now have built-in h264 decoding hardware, so it's a good idea to encode in that format.
The encoding quality is represented mainly as the h264 profile
.
The default video encoding quality for h264 that ffmpeg uses however is the Low profile, which allows for rapid encoding
but will often result in sub-par videos.
Even if you set the bitrate to a high value, if the profile is Low, the quality will still be poor.
This is due to the fact that the profile
tells ffmpeg how much algorithmic effort should go into producing a quality encoding,
or essentially how much thinking the encoder should do.
To achieve a smaller, higher quality output the h264 profile must be set to High which results in more careful processing.
There is also a level
setting that corresponds roughly to the resolution of your video, with 6 being appropriate
up to 4K video.
Finally you can specify the amount of time to let ffmpeg think over how to
best encode your video. This is the preset
, with veryslow being the second-to-last
choice, the last being very time consuming.
Thus I recommend these settings:
-vcodec h264 -profile:v high -level:v 6.0 -preset veryslow
An example usage might be:
ffmpeg input.mp4 -vcodec h264 -profile:v high -level:v 6.0 \
-preset veryslow -acodec mp2 -b:a 160k output.mp4
Links
- Using FFMPEG as a benchmark
- FFMPEG Website
- MPlayer
- MainConcept makers of codecs commonly used to create online videos.