Skip to content

Instantly share code, notes, and snippets.

@flackend
Created August 2, 2018 18:01
Show Gist options
  • Select an option

  • Save flackend/efac7230fa53511e0a6cec16a8d0a943 to your computer and use it in GitHub Desktop.

Select an option

Save flackend/efac7230fa53511e0a6cec16a8d0a943 to your computer and use it in GitHub Desktop.

Revisions

  1. flackend created this gist Aug 2, 2018.
    43 changes: 43 additions & 0 deletions gif.bash
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    #!/bin/bash
    #
    # _/_/_/ _/_/_/ _/_/_/_/
    # _/ _/ _/
    # _/ _/_/ _/ _/_/_/
    # _/ _/ _/ _/
    # _/_/_/ _/_/_/ _/
    #
    # Uses ffmpeg to generate
    # animated gifs from mov
    # files.

    # TODO
    # + delay/speed
    # + choose dither

    # Get the speed from the user (or default)
    read -p "Enter fps[10]: " FPS
    FPS=${FPS:-10}

    read -p "Enter max colors[255]: " MAX_COLORS
    MAX_COLORS=${MAX_COLORS:-255}

    # Get the video size
    GIF_ORIGINAL_SIZE=$(php -r '
    $json = `/usr/local/bin/ffprobe -v quiet -print_format json -show_streams ~/Desktop/in.mov`;
    $info = json_decode($json);
    foreach($info->streams as $stream) {
    if (isset($stream->width)) {
    break;
    }
    }
    $width = $stream->width;
    $height = $stream->height;
    echo "$width:$height";')

    # Get the size from the user (or match the original)
    read -p "Enter size[$GIF_ORIGINAL_SIZE]: " GIF_SIZE
    GIF_SIZE=${GIF_SIZE:-$GIF_ORIGINAL_SIZE}

    ffmpeg -y -i ~/Desktop/in.mov -vf fps=$FPS,scale=$GIF_SIZE:flags=lanczos,palettegen=max_colors=$MAX_COLORS /tmp/palette.png
    ffmpeg -i ~/Desktop/in.mov -i /tmp/palette.png -filter_complex "fps=$FPS,scale=$GIF_SIZE:flags=lanczos[x];[x][1:v]paletteuse=dither=none" ~/Desktop/out.gif
    rm /tmp/palette.png