ffmpeg is a commandline tool to convert media (audio, video) into different formats, manipulate or compress them, put filters on, etc. .
This is a list of ffmpeg flags and commands I commonly use.
This is neither a complete list nor a guide on how to use ffmpeg.
If the code doesn’t start with ffmpeg
then it’s only the
middle part.
This middle part needs to be inserted
ffmpeg -i input.mp4 <here> output.mp4
.
Should be between 20 and 28 for good quality.
The lower the bigger the filesize.
51 is max compression and looks very bad.
A good value is 25 for compression/filesize balance.
The default value is 23.
ffmpeg -i input.mp4 -crf 25 output.mp4
Using the -movflags +faststart
parameters puts specific
metadata infront of the file, which allows for faster starting when
received via e.g. a browser.
This does not raise the filesize.
ffmpeg -i input.mp4 -movflags +faststart output.mp4
-an
-vn
ffmpeg -i input_video.mp4 -vn output_audio.mp3
vcodec changes the video’s codec and acodec the audio one.
copy means it takes the input straight to the output. This makes it way
faster.
Do not copy the codec if you cut a video length. This will create errors because it HAS TO recode the beginning of the cut’ video or else it will create an error-like video.
Examples for video and audio codecs:
-vcodec h264
-vcodec libx265
-vcodec copy
-acodec aac
-acodec mp3
-acodec copy
-b:a 96k
Speed and Quality are always opposite of each other.
Longer compression makes smaller filesizes, faster makes worse
compression.
The default is medium. slow takes 40% longer, slower 100% longer than
medium.
On my test, with a 1min clip, the filesize difference between medium and
slow was ~1MB (18.7 -> 17.5)
-preset slow
With h264 video codec (which is mostly used in mp4) you can set a custom tuning to use for your videos. Example tuning options:
- film , for high quality
- animation , for cartoons
- stillimage , for slideshows
- grain , to preserve film grain
- fastdecode , to view faster on slower devices
Example usage:
ffmpeg -i input.mp4 -vcodec h264 -tune film output.mp4
-aspect 16:9
-vf "setpts=0.5*PTS"
-filter:a "atempo=2.0"
-b:v 1M
-b:a 96k
ffmpeg -i input.mp3 -af 'volume=1.5' output.mp3
iw = width, ih = height, -1 means auto
The first example command halfs the width (and height
automatically)
The second one sets the width and height (and thus the aspect ratio)
explicitly
-vf scale="iw/2:-1"
-vf scale="1280:720"
# what also works for scaling to HD is:
-vf scale="1280:-1"
w = width of output video
x = left top startpoint for the rectangle
ffmpeg -i input.mp4 -filter:v "crop=w:h:x:y" output.mp4
Names are image-001.png, image-002.png, …
-r = framerete in which screenshots are taken, default=25
%3d = name, 001 - 999
ffmpeg -i input.mp4 -r 1 -f image2 image-%3d.png
-vf drawtext="text='MyTextHere': fontcolor=white: fontsize=24: x=(w-text_w)/2: y=(h-text_h)/2"
ffmpeg -i in_one.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts _1.ts
ffmpeg -i in_two.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts _2.ts
ffmpeg -i "concat:_1.ts|_2.ts" -c copy -bsf:a aac_adtstoasc together.mp4
-filter:v "minterpolate='fps=60'"
-r 60
ffmpeg -i 30.mp4 -i 60.mp4 -filter_complex hstack comparison.mp4
The time either in 00:01:23.000 or in seconds format, here seconds
are used
This example would cut the video, so that only seconds 3 to 8
plays
aka. 5 seconds long output video -t = duration, -ss = starttime
ffmpeg -i input.mp4 -ss 3 -t 5 output.mp4
ffmpeg -i in.mkv -vf subtitles=in.mkv out.mp4
for i in *.mp4; do ffmpeg -i "$i" "$(basename "$i" .mp4)".mp3 ; done
This creates a picture from the first frame of the video.
There are more complex thumbnail generation commands out there.
ffmpeg -i "input.mp4" -vf "thumbnail" -frames:v 1 input_thumbnail.jpg
You can use ffmpeg to convert between image formats too
ffmpeg -i "input.jpg" output.png
This can remove noise (e.g. flickering of pixels in the background) and “soften” videos, reducing filesize but also removing a few fine-grained details
ffmpeg -i "input.mp4" -vf "atadenoise" output.mp4
Source: https://web.dev/replace-gifs-with-videos/
ffmpeg -i my-animation.gif -vf "crop=trunc(iw/2)*2:trunc(ih/2)*2" -b:v 0 -crf 25 -f mp4 -vcodec libx264 -pix_fmt yuv420p my-animation.mp4
ffmpeg -i inputfile.mp4 -filter_complex \
"[0:v]pad=iw:ih+100:0:(oh-ih)/2+50:color=white, \
drawtext=text='Me coming to my wife's funeral':fontsize=42:x=(w-tw)/2:y=(100-th)/2" \
outputfile.mp4
ffmpeg -i input.mp4 -filter_complex \
"[0:v]pad=iw:ih+100:0:(oh-ih)/2:color=white, \
drawtext=text='ONE DOES NOT SIMPLY':fontsize=24:x=(w-tw)/2:y=(50-th)/2, \
drawtext=text='STOP ME FROM FILTERING':fontsize=24:x=(w-tw)/2:y=h-25-(th/2)" \
output.mp4
The input file is a 15fps and full HD .mkv video with external
subtitles and unoptimized, weird encodings.
This command makes it into a 30fps normal HD .mp4 video with the
subtitles burned into the video and encodings that are optimized and
widely acknowledged (h264/aac).
ffmpeg -i input.mkv -vf "subtitles=input.mkv,scale=1280:720" -crf 25 -vcodec h264 -acodec aac -r 30 output.mp4