ffmpeg basic commands
These are the basics of the video processing tool “ffmpeg”.
Check FFmpeg documentation for all the info.
Table of Contents
- Encode a video (using h264/libx264 codec) quickly
- Encode several videos (Bash loop)
- Overwrite output file if exists
- Extract audio from video
- Remove audio
- Cut a video
- Speed up / slow down a video
- Change the resolution
- Capture frames
- Join several videos (using a list file)
- Change brightness, contrast or gamma (‘eq’ video filter)
- Stabilize a video
Encode a video (using h264/libx264 codec) quickly
ffmpeg -i input.mp4 -c:v libx264 -preset veryfast -crf 22 output.mp4
-preset
has several possible values (depends on codec used), faster encoding means less CPU load and less compression.-crf
stands for “Constant Rate Factor” and this is the recommended rate control mode for most uses. A lower value generally leads to higher quality, and a subjectively sane range is 17–28 (23 is the default).- You can add
-f <format>
to force a format for the output file (e.g.:-f mp4
).
Encode several videos (Bash loop)
for i in *.mp4;do ffmpeg -i $i -preset ultrafast ${i/.mp4/_new.mp4};done
for i in *.mp4;do ffmpeg -i $i -preset ultrafast output/$i;done
Overwrite output file if exists
ffmpeg -y -i input.mp4 output.mp4
Extract audio from video
ffmpeg -i video.mp4 -vn sound.mp3
Remove audio
ffmpeg -i video.mp4 -an output.mp4
Cut a video
# from mm:ss to mm2:ss2
# e.g.: from 00:12 to 00:15 (3 seconds)
ffmpeg -ss mm:ss -to mm2:ss2 -i video.mp4 output.mp4
# from mm:ss, end when passes mm2:ss2
# e.g.: from 00:12, ends after 00:03 (3 seconds)
ffmpeg -ss mm:ss -i video.mp4 -to mm2:ss2 output.mp4
- If you use
-codec
(e.g.:-codec copy
), add it after-i video.mp4
.
Speed up / slow down a video
# speed up to 2x
ffmpeg -i video.mp4 -vf setpts=0.5*PTS fast.mp4
# slow down to 0.5x
ffmpeg -i video.mp4 -vf setpts=2*PTS slow.mp4
Change the resolution
ffmpeg -i video.mp4 -s 1280x720 output.mp4
Capture frames
Capture a video frame (at mm:ss) and change its resolution.
ffmpeg -ss mm:ss -i video.mp4 -frames 1 -s 1280x720 -f image2 image.png
Capture one frame per second of the video.
ffmpeg -i video.mp4 -r 1 -f image2 image%d.png
Capture a frame from a video device (webcam, etc.).
ffmpeg -i /dev/video0 -frames 1 -f image2 image.png
Join several videos (using a list file)
-
Create a text file with the filenames.
# list.txt file video1.mp4 file video2.mp4
-
Run
ffmpeg
:ffmpeg -f concat -safe 0 -i list.txt output.mp4
- Using
-codec copy
will probably not work.
- Using
Change brightness, contrast or gamma (‘eq’ video filter)
ffmpeg -i video.mp4 -vf "eq=contrast=1.5" output.mp4
- Check FFmpeg documentation for more information.
Stabilize a video
See How to stabilize a video using ffmpeg.
Test with this online terminal:
If you have any suggestion, feel free to contact me via social media or email.
Latest tutorials and articles:
Featured content: