Skip to content

Instantly share code, notes, and snippets.

@aliceklipper
Last active July 30, 2017 12:08
Show Gist options
  • Save aliceklipper/5b3c9c2b259ee3d87842362906760ac7 to your computer and use it in GitHub Desktop.
Save aliceklipper/5b3c9c2b259ee3d87842362906760ac7 to your computer and use it in GitHub Desktop.
Encode GIFs using mpv's A-B loop and ffmpeg.
#!/usr/bin/zsh
start="$1"
end="$2"
input="$3"
outdir="/home/aliceklipper/Pictures/Screenshots/MpvGifs/"
basename="${input##*/}"
output="${outdir}${basename%.*} [${start} - ${end}].gif"
cut="/tmp/cut.mp4"
shakal="/tmp/shakal.mp4"
palette="/tmp/palette.png"
filters="fps=12,scale=w=min(480\,iw):-1:flags=lanczos"
echo "[encode-gif] Start: ${start}."
echo "[encode-gif] End: ${end}."
echo "[encode-gif] Input: ${input}."
echo "[encode-gif] Output: ${output}."
echo
/usr/bin/mkdir -p "${outdir}"
echo "Cutting..."
/usr/bin/ffmpeg -v warning -i "${input}" -ss "${start}" -to "${end}" -an -c:v libx264 -preset ultrafast -crf 0 -threads 4 -y "${cut}"
echo "[encode-gif] Shakaling..."
/usr/bin/ffmpeg -v warning -i "${cut}" -vf "${filters}" -c:v libx264 -preset ultrafast -crf 0 -threads 4 -y "${shakal}"
echo "Generating the palette..."
/usr/bin/ffmpeg -v warning -i "${shakal}" -vf "palettegen" -y "${palette}"
echo "[encode-gif] Encoding..."
/usr/bin/ffmpeg -v warning -i "${shakal}" -i "${palette}" -lavfi "paletteuse=dither=bayer:bayer_scale=3" -y "${output}"
echo "[encode-gif] GIF encoding completed."
#!/usr/bin/zsh
start="$1"
end="$2"
path="$3"
/usr/local/bin/encode-gif "${start}" "${end}" "${path}"
g run "/usr/local/bin/encode-gif-mpv-wrapper" "${=ab-loop-a}" "${=ab-loop-b}" "${path}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment