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.
ffmpeg -i input.mp4 -crf 25 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.
-vcodec h264
-vcodec libvpx
-vcodec copy
-acodec aac
-acodec mp3
-acodec copy
With this you take the input video and audio and copy it to output.
This makes cutting videos (e.g. by time) very quick as no converting takes place.
ffmpeg -i input.mp4 -c copy output.mp4
-map 0:a:0 -b:a 96k
-aspect 16:9
-vf "setpts=0.5*PTS"
ffmpeg -i input.mp4 -filter:a "atempo=2.0" -vn output.mp3
-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"
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
ffmpeg -i Input.mp4 -filter:v "minterpolate='fps=60'" Out60.mp4
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
for i in *.mp4; do ffmpeg -i "$i" "_$(basename "$i" .mp4)".mp4 ; done
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