Advertisement
Guest User

mkgif.sh

a guest
Jun 28th, 2020
76
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. # https://engineering.giphy.com/how-to-make-gifs-with-ffmpeg/
  4.  
  5.  
  6. INFILE="$1"
  7. OUT="${INFILE%.*}.gif"
  8.  
  9.  
  10. if [[ ! -f $INFILE ]]; then
  11. echo "'$INFILE' is not a File."
  12. exit 1
  13. fi
  14.  
  15.  
  16. INTER=$(mktemp --suffix=".mkv")
  17. PALETTE=$(mktemp --suffix=".png")
  18.  
  19.  
  20. function cleanup() {
  21. rm "$PALETTE" "$INTER"
  22. }
  23.  
  24. function panic() {
  25. echo "Operation cancelling." >&2
  26. cleanup
  27. exit 1
  28. }
  29.  
  30.  
  31. [[ -z "$HEIGHT" ]] && HEIGHT=270
  32. [[ -z "$WIDTH" ]] && WIDTH=-2
  33.  
  34. [[ -z "$FILTER" ]] && FILTER="scale=w=$WIDTH:h=$HEIGHT"
  35.  
  36.  
  37. echo "1/3. Creating scaled intermediate..." >&2
  38. ffmpeg -y -hide_banner -loglevel warning -stats -i "$@" \
  39. -filter_complex "[0:v] $FILTER" -map 0:v "$INTER" || panic
  40. #       -filter_complex "[0:v] crop=1024:576:100:504" -map 0:v "$INTER" || panic
  41.  
  42. echo "2/3. Generating palette..." >&2
  43. ffmpeg -y -hide_banner -loglevel warning -i "$INTER" \
  44. -filter_complex "[0:v] palettegen" "$PALETTE" || panic
  45.  
  46. echo "3/3. Encoding Gif..." >&2
  47. ffmpeg -hide_banner -loglevel warning -stats -i "$INTER" -i "$PALETTE" \
  48. -filter_complex "[0:v][1:v] paletteuse" -vsync 0 -map 0:v "$OUT" || panic
  49.  
  50.  
  51. cleanup
  52. echo -n "Done. Size: " >&2
  53. numfmt --to=iec-i --suffix=B --format="%.1f" $(stat -c '%s' "$OUT") >&2
  54. readlink -f "$OUT"
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement