Various commands and shortcuts for working with ffmpeg

Turn folder of frames into a timelapse video

ffmpeg -r 20 -i pic%03d.png -vcodec libx264 -pix_fmt yuv420p -crf 25

-r specifies the frame rate %03d says that the images have three digits \crf sets video quality (lower number is higher quality)

To invert the colors of each image (or apply any other image filter), add -filter negate anywhere before the output video name

Convert a folder of images to an exact size

We will actually use ImageMagick for this, but this is a common preprocessing step before ffmeg timelapses

mogrify -resize 100x100 *png

Watermark a video

Follow the instructions here

ffmpeg -i -i watermark_small.png -filter_complex "overlay=(.7*main_w):(.94*main_h)"

Places watermark in bottom right corner

Extract audio from video

For an MP4 file this is easy

ffmpeg -i input.mp4 output.mp3

Encode a video for Twitter

Use FFMPEG, for mp4 the file shape has to be even in both dimensions; can omit the -vf crop below if this is already true

ffmpeg -i video2_small.mp4 -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -vcodec libx264 -acodec aac output.mp4

As a fallback, manually crop the file

ffmpeg -i video2_small.mp4 -filter:v "crop=3008:3000:0:0" -vcodec libx264 -acodec aac output.mp4

Deshake a video

ffmpeg -i input.mp4 -vf deshake="rx=64:ry=64"  -an

The flags -rx and -ry are optional flags taken from this list here

Convert a directory of FLAC to ALAC for iTunes

Using ffmpeg

for i in *.flac ; do
    ffmpeg -i "$i" -acodec alac "$(basename "${i/.flac}").m4a"
    sleep 2
    # may need to adjust sleep time for processor speed                               

I saved this as a bash script, so just cd into the directory containing the FLAC files and then run

bash ~/codebits/utility_scripts/

Merge three videos in xy as a mosiac using FFMPEG

The height and widht parameters have to be calculated very carefully—any extra space will be filled with black thanks to the “color” option below

A structured example

ffmpeg \
	-i -i -i \
	-filter_complex "\
		nullsrc=size=2712x904 [base]; \
		[0:v] setpts=PTS-STARTPTS, scale=904x904 [left]; \
		[1:v] setpts=PTS-STARTPTS, scale=904x904 [middle]; \
		[2:v] setpts=PTS-STARTPTS, scale=904x904 [right]; \
		[base][left] overlay=shortest=1 [tmp1]; \
		[tmp1][middle] overlay=shortest=1:x=904 [tmp2]; \
		[tmp2][right] overlay=shortest=1:x=1808 \
	" \
	-c:v libx264

Another example that cropsmosaic length to the shortest video length

ffmpeg -i -i -i -filter_complex "color=s=4912x1502:c=black [base];[0:v] setpts=PTS-STARTPTS, scale=1504x1502 [left];[1:v] setpts=PTS-STARTPTS, scale=1504x1502 [middle];[2:v] setpts=PTS-STARTPTS, scale=1504x1502 [right];[base][left] overlay=shortest=1 [tmp1];[tmp1][middle] overlay=shortest=1:x=1704 [tmp2];[tmp2][right] overlay=shortest=1:x=3408" -c:v libx264

Remove the shortest flag to set the mosaic length to length of longest video (pad with ending frames of other videos) 1520x1507

ffmpeg -i -i -filter_complex "color=s=3340x1508:c=black [base];[0:v] setpts=PTS-STARTPTS, scale=1520x1508 [left];[1:v] setpts=PTS-STARTPTS, scale=1520x1508 [right];[base][left] overlay=shortest=1 [tmp1];[tmp1][right] overlay=shortest=1:x=1820" -c:v libx264

Note that this throws an error if the video heights or widths are not even numbers of pixels

Source 1Source 2


Turn an image stack into a video, and crop to have even dimensions

ffmpeg -r 30 -i frame_%03d.png -vcodec libx264 -pix_fmt yuv420p -crf 25 -filter:v "crop=1210:1320:0:0"

Make a mosaic of three movies with equal height and length

ffmpeg -i -i -i -filter_complex hstack=inputs=3 -vcodec libx264 -acodec aac output.mp4