Musings of a Fondue

FFmpeg Snippets

Scripts for useful FFmpeg actions. Posting for future reference. Perhaps others will also get use from them.

Crop a video and generate a gif
Useful when want to generate a gif from only a portion of the video.

@echo off


set src=yourVideo
set dest=yourOutput.gif

set width=
set height=
set x=
set y=

set palette=palette.png
set intermediate=temp.avi

rem Crop source
ffmpeg -i %src% -filter:v "crop=%width%:%height%:%x%:%y%" -c:v rawvideo %intermediate%

rem Gen gif
ffmpeg -i %intermediate% -vf palettegen -y %palette%
ffmpeg -i %intermediate% -i %palette% -lavfi paletteuse -y %dest%

Pad a video
Useful when want to achieve a given aspect ratio without rescaling.

@echo off

set src=yourVideo
set dest=

set padColor=black
set targetWidth=
set targetHeight=
set curWidth=
set curHeight=

rem Should probably check that difference between target and current is even
set /a "x=(%targetWidth%-%curWidth%)/2"
set /a "y=(%targetHeight%-%curHeight%)/2"

ffmpeg -i %src% -vf "pad=width=%targetWidth%:height=%targetHeight%:x=%x%:y=%y%:color=%padColor%" -c:v rawvideo %dest%

AVI to MP4
Useful when want minimal loss during compression.

ffmpeg -i src.avi -c:v libx264 -crf 0 -preset veryslow dest.mp4

At the moment, Firefox does not support the yuv444 videos generated by the above snippet. The following works in the meantime:

ffmpeg -i src.avi -c:v libx264 -crf 1 -preset veryslow -pix_fmt yuv420p dest.mp4

Convert all
Useful when want to apply an action to all the files in a directory. The snippet below generates an mp3 file for each wma file in the current directory.

@echo off

for %%A in (*.wma) do ffmpeg -i "%%A" -acodec libmp3lame -ab 192k "%%~nA.mp3"