Advertisement
Guest User

Untitled

a guest
Feb 10th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.07 KB | None | 0 0
  1. #!/bin/bash
  2. #default values
  3. #based on http://superuser.com/questions/556029/how-do-i-convert-a-video-to-gif-using-ffmpeg-with-reasonable-quality
  4. fps=15
  5. width=320
  6.  
  7. filename() {
  8.     if [ ! -f "$1" ]; then return 1; fi
  9.     filename1=$(basename "$1")
  10.     filename1="${filename1%.*}"
  11.     echo "$filename1"
  12. }
  13.  
  14. numcomp() {
  15.     #compare two floating point numbers
  16.     #example: numcomp 2 -gt 3   - return status 1
  17.     if [ "$#" -ne 3 ]; then echo "compare: wrong number of parameters" >&2; return 1; fi
  18.     case "$2" in
  19.     '-gt') op='>';;
  20.     '-ge') op='>=';;
  21.     '-lt') op='<';;
  22.     '-le') op='<=';;
  23.     *) echo "compare: wrong operator" >&2; exit 1 ;;
  24.     esac
  25.     output="$(echo "$1 $op $3" 2>&1 | bc -l 2>&1)"
  26.     case "$output" in
  27.     '0') return 1;;
  28.     '1') return 0;;
  29.     *) echo "compare error: $output"; exit 1;;
  30.     esac
  31. }
  32.  
  33. gengif() {
  34.     if [ ! -f "$1" ]; then
  35.     echo "$1 doesn't exists!" >&2
  36.     return 1
  37.     fi
  38.     f_width="$(mediainfo '--Inform=Video;%Width%' "$1")"
  39.     f_fps="$(mediainfo '--Inform=Video;%FrameRate%' "$1")"
  40.     if [ "$fps" = 'auto' ] || [ "$fps" = '0' ]; then
  41.     fps=$f_fps
  42.     echo "fps value set to $fps - from source video"
  43.     elif numcomp "$f_fps" -lt "$fps"; then
  44.     echo "File fps [$f_fps] is lower than target fps [$fps]. Setting target fps to $f_fps ."
  45.     fps="$f_fps"
  46.     fi
  47.  
  48.     if [ "$width" = 'auto' ] || [ "$width" = '0' ]; then
  49.     width=$f_width
  50.     echo "Width value set to $width - from source video"
  51.     elif [ "$f_width" -lt "$width" ]; then
  52.     echo "File width [$f_width] is lower than target width [$width]. Setting target width to $f_width ."
  53.     width="$f_width"
  54.     fi
  55.     output="$(filename "$1")-${width}p-${fps}fps.gif"
  56.     if [ -f "$output" ]; then
  57.     echo "$output already exists!" >&2
  58.     exit 1
  59.     fi
  60.     palette="$(mktemp ./palette.XXXXXX.png)"
  61.     echo "ffmpeg -y -i $1 -vf fps=$fps,scale=$width:-1:flags=lanczos,palettegen $palette"
  62.     ffmpeg -y -i "$1" -vf fps=$fps,scale=$width:-1:flags=lanczos,palettegen "$palette"
  63.     echo "ffmpeg -i $1 -i $palette -filter_complex fps=$fps,scale=$width:-1:flags=lanczos[x];[x][1:v]paletteuse $output"
  64.     ffmpeg -i "$1" -i "$palette" -filter_complex \
  65.        "fps=$fps,scale=$width:-1:flags=lanczos[x];[x][1:v]paletteuse" "$output"
  66.     rm "$palette"
  67. }
  68.  
  69. help() {
  70.     echo "
  71. VIDEO TO GIF GENERATOR
  72. USAGE: $0 [-f FPS] [-w WIDTH] [-h] MOVIE1 [MOVIE2 ...]
  73. OPTIONS:
  74.  -h       - print this help
  75.  -f FPS   - output fps. Can be floatpoint. (default is $fps)
  76.           '0' or 'auto' to set to source video fps
  77.  -w WIDTH - output width (default is $width)
  78.           '0' or 'auto' to set to sorce video width
  79. If provided fps or width is greater than in source video, then value from source video will be used.
  80. "
  81.     exit 0
  82. }
  83.  
  84. if [ $# -eq 0 ]; then
  85.     help
  86. fi
  87.  
  88. while [ $# -gt 0 ]; do
  89.     case "$1" in
  90.     -f)  fps="$2"; shift;;
  91.     -w)  width="$2"; shift;;
  92.     -h)  help;;
  93.     --)  shift; break;;
  94.     -*)  help      
  95.         exit 1;;
  96.     *)  break;;# terminate while loop
  97.     esac
  98.     shift
  99.     # all command line switches are processed,
  100.     # "$@" contains all file names
  101. done
  102.  
  103. for k in "$@"; do
  104.     gengif "$k"
  105. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement