Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # install ffmpeg and gifsicle if not exists
  4. which ffmpeg > /dev/null || brew install ffmpeg
  5. which gifsicle > /dev/null || brew install gifsicle
  6.  
  7. usage () {
  8. echo "Usage:" 1>&2
  9. echo " mov2gif [-d delay] [-o 1|2|3] [-r frame_rate] [-w width] input_file" 1>&2
  10. echo "" 1>&2
  11. echo "Example:" 1>&2
  12. echo " mov2gif -w 640 -r 24 ~/Desktop/example.mov" 1>&2
  13. echo "" 1>&2
  14. echo "Options:" 1>&2
  15. echo " -d delay Set the delay between frames to time in hundredths of a second." 1>&2
  16. echo " -o level Optimize output GIF animations for space. Level determines how much optimization is done; higher levels take longer, but may have better results." 1>&2
  17. echo " -r rate Set frame rate." 1>&2
  18. echo " -w width Set width of output GIF." 1>&2
  19. exit 1
  20. }
  21.  
  22. while getopts "d:o:r:w:h" opt
  23. do
  24. case $opt in
  25. d) DELAY=$OPTARG ;;
  26. o) OPTIMIZE=$OPTARG ;;
  27. r) RATE=$OPTARG ;;
  28. w) WIDTH=$OPTARG ;;
  29. h) usage ;;
  30. esac
  31. done
  32.  
  33. shift $((OPTIND - 1))
  34. FILE=$1
  35.  
  36. if [ -z "$DELAY" ]; then
  37. DELAY=3
  38. fi
  39.  
  40. if [ -z "$OPTIMIZE" ]; then
  41. OPTIMIZE=3
  42. fi
  43.  
  44. if [ -z "$RATE" ]; then
  45. RATE=24
  46. fi
  47.  
  48. if [ -z "$WIDTH" ]; then
  49. WIDTH=640
  50. fi
  51.  
  52. if [ -z "$FILE" ]; then
  53. echo "Please specify input file path." 1>&2
  54. echo "" 1>&2
  55. usage
  56. fi
  57.  
  58. # Split frames into gifs with `ffmpeg` and then combine them into an animated gif with `gifsicle`
  59. ffmpeg -i "$FILE" -vf scale=$WIDTH:-1 -r $RATE -f gif - | gifsicle --optimize=$OPTIMIZE --delay=$DELAY -o "$FILE.gif"
  60.  
  61. echo "" 1>&2
  62. echo "generated $(cd $(dirname "$FILE.gif"); pwd)/$(basename "$FILE.gif")" 1>&2
  63.  
  64. # I like to check generated gif in a browser (with Chrome)
  65. open -a 'Google Chrome.app' "$FILE.gif"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement