87 lines · 1.9 KB · executable
#!/bin/bash

# If ffmpeg is not installed, exit with an error message
if ! command -v ffmpeg &>/dev/null; then
    printf "Can't find ffmpeg. Please install it (or check your path).\n"
    exit 1
fi

pngSeq='pngs/frame-%5d.png'

function usage {
    cmd=$(basename "$0")
    local cmd
    printf "Create a video from a PNG sequence.\n\n"
    printf "Usage:\n"
    printf "    %s [gif|mp4|webm] framerate width basename [max-gif-colors]\n\n" "$cmd"
    printf "The input is expected to be a PNG sequence relative to the current\ndirectory with the format 'pngs/frame-%%5d.png'\n"
}

function makeGIF {
    local framerate=$1
    local width=$2
    local basename=$3
    local maxColors=$5

    # GIFs of 60fps videos have to be 50fps
    if [[ $framerate == "60" ]]; then
        framerate=50
    fi

    if [[ $maxColors == "" ]]; then
        maxColors=256
    fi

    ffmpeg \
        -framerate "$framerate" \
        -i $pngSeq \
        -vf "fps=$framerate,scale=$width:-1:flags=lanczos,split[s0][s1];[s0]palettegen=max_colors=${maxColors}[p];[s1][p]paletteuse" \
        "$basename".gif
}