ffmpeg Notes
Scale
Scale To Width:
-vf "scale=960:-1"
Scale To New Aspect Ratio
-vf scale=800:600 -aspect 800:600
Crop and scale at the same time!
# From: https://stackoverflow.com/a/52675535
crop=iw-100:ih-200,scale=960:576
Blank Space Padding
-vf "pad=width=1800:height=1200:x=100:y=100:color=black"
Frame rate / time
Force frame rate
frame rate: -r 30
frame rate: -r 60
If the framerate is wrong, you can correct it with a specific frame rate and a filter to adjust the playback rate:
-r 60 -vf: setpts=2*PTS
Time Crop:
start: -ss 2.3
end: -to 4.3
example: -ss 0.5 -to 1.5
Video quality
Video Codec + Quality
h264: -c:v libx264 -crf 1 not this: -q:v 0
mpeg4: -c:v mpeg4 -crf 1
webm: -c:v libvpx -c:a libvorbis
# https://thethemefoundry.com/blog/convert-mp4-to-webm/
Copy Codec (could result in non-mp4 output)
-vcodec copy
Video Playback
# faster decoding:
-tune fastdecode
Audio
Remove Audio
-an
Convert audio to mono
-af "pan=mono|c0=c1" -map 0:0 -c:v
ffmpeg -i video.mp4 -af "pan=mono|c0=c1" video.mp4
Audio Codec
-c:a aac -b:a 128k -ac 2
Loop a video 3 times via concat
# Create a file _concat.txt dynamically
"file 'input.mp4'`nfile 'input.mp4'`nfile 'input.mp4'" | Set-Content -Path "_concat.txt"
ffmpeg -f concat -safe 0 -i _concat.txt -c copy output.mp4
Concat two videos (with the same compression)
ffmpeg -i "concat:video-1.mp4|video-2.mp4" -c copy video-output.mp4
Merge Audio To Video File
%ffmpeg% -f concat -i _concat.txt -i %audioFile% -c:a aac -b:a 128k -ac 2 final-render\%sessionId%.mp4
Concat Video Files & Add Audio
%ffmpeg% -f concat -i _concat.txt -i %audioFile% -c:a aac -b:a 128k -ac 2 final-render\%sessionId%.mp4
Images to video
Image Sequence To Video (Window)
%ffmpeg% -r 30 -f image2 -i %%04d.tga -c:v libx264 -crf 16 -pix_fmt yuv420p -f mp4 ..\_frames-rendered.mp4
Single image to video
ffmpeg -loop 1 -i image.png -c:v libx264 -t 20 -pix_fmt yuv420p -crf 8 output.mp4
Keyframes
Keyframes On Every Frame
-vcodec libx264 -x264-params keyint=1:scenecut=0
# or for non-mp4:
-keyint_min 1 -g 1
Filters
Rotate Video
0 = 90CounterCLockwise and Vertical Flip (default)
1 = 90Clockwise
2 = 90CounterClockwise
3 = 90Clockwise and Vertical Flip
Use -vf "transpose=2,transpose=2" for 180 degrees.
# example: -vf "transpose=1"
Effects
# Brightness/saturation/contrast
-vf eq=brightness=0.08:saturation=1.2:contrast=1.15
# Grayscale:
-vf format=gray
# Invert:
-vf lutrgb="r=negval:g=negval:b=negval"
# Frame interpolation
-vf minterpolate='fps=60:mi_mode=mci:mc_mode=aobmc:me_mode=bidir:vsbmc=1'
# https://github.com/dthpham/butterflow
# Boomerang
-filter_complex "[0]reverse[r];[0][r]concat=n=2"
Special Effects
Another Boomerang solution
# create reverse video
ffmpeg -i input.mp4 -vf reverse -crf 12 input-reverse.mp4
# concat original and reversed video
ffmpeg -i input.mp4 -i input-reverse.mp4 -filter_complex "[0][1]concat=n=2:v=1:a=0[out]" -map "[out]" -crf 16 input-boomerang.mp4
Generate A Test Pattern
ffmpeg -f lavfi -i testsrc=duration=10:size=3840x2160:rate=60 -c:v libx264 testsrc1.mp4
Alpha Channel video to webm
# (2) commands to convert from Prores to webm w/alpha channel
ffmpeg -i input.mov -c:v libvpx-vp9 -b:v 1000k -pass 1 -an -f null -
ffmpeg -i input.mov -c:v libvpx-vp9 -b:v 1000k -pass 2 output.webm
Alpha channel video to image sequence
ffmpeg -i input.mov -pix_fmt rgba -qscale:v 1 -vf scale=640:-1 output_%04d.png
ProRes
# Info: https://ottverse.com/ffmpeg-convert-to-apple-prores-422-4444-hq/
Specific recommendations for Twitter
# From: https://gist.github.com/marcduiker/abe8e4b7353b4c6430d556b727666620
# - Convert pngs to mp4
ffmpeg -framerate 10 -i frame_%04d.png -c:v h264_qsv -b:v 5M video.mp4
# - Convert and scale an existing mp4 to 1080:
ffmpeg -i input.mp4 -c:v h264_qsv -vf: scale=1080:-1 -b:v 5M output.mp4
# - Convert infinite looping gif to limited looping mp4 (no scaling)
ffmpeg -i input.gif -c:v h264_qsv -filter_complex loop=loop=<NrOfLoops>:size=<TotalFrames>:start=<FramesToSkip> -b:v 5M output.mp4