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

source "$HOME/.local/share/meowgorithm/bash_funcs"

if [[ $(command_exists ffmpeg) == 0 ]]; 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