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
- Convert mkv to mp4
- 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
- Create a video from a sequence of images
- Join several videos (using a list file)
- Change brightness, contrast or gamma (‘eq’ video filter)
- Adjust exposure
- Stack two videos horizontally
- Flip a video horizontally
- More audio, video filters
- Stabilize a video
- Add a timestamp
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
). - Ffmpeg cannot overwrite the input file, always use a different name for the output file.
Convert mkv to mp4
ffmpeg -i input.mkv -codec copy output.mp4
-codec copy
makes ffmpeg omit the decoding and encoding step for the specified stream, so it does only demuxing and muxing. It’s the same as-c:v copy -c:a copy
.
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
When cutting a video using -codec copy
(add it after -i input.mp4
), the video is not trimmed in the exact specified timeframe.
# from mm:ss to end
ffmpeg -i video.mp4 -preset superfast -ss mm:ss output.mp4
# 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 -preset superfast 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 -preset superfast output.mp4
Speed up / slow down a video
# speed up to 2x
ffmpeg -i video.mp4 -vf setpts=0.5*PTS fast.mp4
- Audio is not changed. To speed up the audio, add
-af atempo=2
(for 2x video).
# slow down to 0.5x
ffmpeg -i video.mp4 -vf setpts=2*PTS slow.mp4
- Audio is not changed. To slow down the audio, add
af tempo=0.5
(for 0.5x video).
Change the resolution
-s <width>x<height>
ffmpeg -i video.mp4 -s 1280x720 output.mp4
-vf scale=<width|-1>:<height|-1>
: use-1
to resize keeping original ratio.ffmpeg -i video.mp4 -vf scale=1280:-1 output.mp4
ffmpeg -i video.mp4 -vf scale=-1:720 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
Create a video from a sequence of images
$ ls
anim_1.jpg anim_2.jpg anim_3.jpg anim_4.jpg anim_5.jpg
anim_6.jpg ...
$ ffmpeg -i anim_%d.jpg -framerate 25 -c:v h264 output.mp4
If filenames have another naming scheme:
$ ls
anim_0001.jpg anim_0014.jpg anim_0027.jpg anim_0040.jpg anim_0053.jpg
anim_0002.jpg anim_0015.jpg anim_0028.jpg anim_0041.jpg anim_0054.jpg
...
$ ffmpeg -i anim_%4d.jpg -framerate 25 -c:v h264 output.mp4
$ ls
anim_100.jpg anim_101.jpg anim_102.jpg anim_103.jpg anim_104.jpg
...
$ ffmpeg -start_number 100 -i anim_%d.jpg -framerate 30 -c:v h264 output.mp4
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
ffmpeg -i video.mp4 -vf eq=gamma=1.2 output.mp4
- Check FFmpeg documentation for more information.
- Quotes around filter are optional, but recommended.
Adjust exposure
ffmpeg -i video.mp4 -vf exposure=0.8 output.mp4
Stack two videos horizontally
ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex hstack output.mp4
- The horizontal resolution is changed.
Flip a video horizontally
ffmpeg -i input.mp4 -vf 'transpose=0,transpose=1' output.mp4
More audio, video filters
- Check FFmpeg filters.
- When applying filters, use this schema:
-vf <filter1 name>=<prop1>=<value>:<prop2>=<value>,<filter2 name>=<prop1>=<value>
- change
-vf
to-af
for audio filters.
- change
- When using more than one inputs or outputs, use
-filter_complex
instead of-vf
or-af
.
Stabilize a video
See How to stabilize a video using ffmpeg.
Add a timestamp
ffmpeg -i input.mp4 -vf drawtext="fontfile=arial.ttf:text='%{pts\:hms}':fontcolor=white:fontsize=50:box=1:boxcolor=black:y=h-50:x=(w-text_w)/2" output.mp4
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: