Guest User

Untitled

a guest
Apr 29th, 2018
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.77 KB | None | 0 0
  1. #!/bin/bash
  2. function runffmpeg() {
  3.     echo --------------------------------------------
  4.     echo "ffmpeg" "$@"
  5.     echo --------------------------------------------
  6.     ffmpeg -hide_banner "$@"
  7. }
  8.  
  9. # color curves
  10. function curves() { echo -n "colorbalance=$1:$1:$1:$2:$2:$2:$3:$3:$3"; }
  11. # scale videos with sar≠1 to have given width and pixel size 1x1
  12. function SARscale() { echo -n "scale=trunc($1/2)*2:trunc((ih*$1)/(iw*sar*2))*2,setsar=1"; }
  13. # usage: scale <width> [<height>]
  14. function scale() { [ -n "$2" ] && echo -n "scale=$1:$2" || scale $1 -1; }
  15. function fps() { [ -n "$1" ] && echo -n "fps=$1,"; }
  16.  
  17. function print_vars() {
  18.     echo "w            = $w"
  19.     echo "cropw        = $cropw"
  20.     echo "croph        = $croph"
  21.     echo "cropx        = $cropx"
  22.     echo "cropy        = $cropy"
  23.     echo "colors       = $colors"
  24.     echo "fps          = $fps"
  25.     echo "ss           = $ss"
  26.     echo "d            = $d"
  27.     echo "dither       = $dither"
  28.     echo "bayer_scale  = $bayer_scale"
  29.     echo "out          = $out"
  30.     echo "curves       = $curves"
  31.     echo "vidstab      = $vidstab"
  32.     echo "vidstab_shakiness = $vidstab_shakiness"
  33.     echo "vidstab_stepsize  = $vidstab_stepsize"
  34. }
  35.  
  36. function print_help() {
  37.     echo "converts video to gif"
  38.     echo "usage: ${0##*/} [-y|-v|-h] [<file>]"
  39.     echo ""
  40.     echo "Options:"
  41.     echo "  -y - automatically override the output file"
  42.     echo "  -v - print user-set variables, final variables and then exit"
  43.     echo "  -h - print help, user-set variables and exit"
  44.     echo ""
  45.     echo "The following enviroment variables can be set:"
  46.     echo ""
  47.     echo "  file - the file to convert, has to be defined or passed as an argument to the script"
  48.     echo "  w - width, height is set to keep the aspect ratio, default: iw"
  49.     echo "  cropx, cropy, cropw, croph - crop x/y (top left corner of crop) and with/height"
  50.     echo "    defaults are 0/0, iw/ih"
  51.     echo "  colors - the number of colors in gif, default: 50"
  52.     echo "  fps - fps, default is empty string - keep fps of the source"
  53.     echo "  ss - seek position, default: 0:00"
  54.     echo "  d - duration, default: 5.0"
  55.     echo "  out - filename of output (without extension), default: out"
  56.     echo "  vidstab - 1 => use vidstab, not set by default"
  57.     echo "  vidstab_shakiness - shakiness for vidstab, default: 8"
  58.     echo "  vidstab_stepsize - step size for vidstab, default: 1"
  59.     echo "  dither, bayer_scale - dithering method, and bayer scale (if dither == bayer)"
  60.     echo "    default: bayer, 2"
  61.     echo "  curves - applies colorbalance filter, is defined as 'a:b:c' where 0:0:0"
  62.     echo "    is standard color balance with range -1 to 1 (the filter separates r/g/b,"
  63.     echo "    this script doesn't)"
  64.     echo ""
  65.     echo "vars: "
  66.     print_vars
  67.     [ -z "$1" ] || exit "$1"
  68. }
  69.  
  70. set -e
  71. case "$1" in
  72.     (-h*) print_help 0;;
  73.     (-v) check_vars=1; shift;;
  74.     (-y) y="-y"; shift;;
  75. esac
  76. [ $# -eq 0 ] || file="$1"
  77. [ -n "$file" ] || print_help 1
  78.  
  79. echo "user set vars:"
  80. print_vars
  81.  
  82. [ -n "$w"            ] || w=iw
  83. [ -n "$cropw"        ] || cropw=iw
  84. [ -n "$croph"        ] || croph=ih
  85. [ -n "$cropx"        ] || cropx=0
  86. [ -n "$cropy"        ] || cropy=0
  87. [ -n "$colors"       ] || colors=50
  88. [ -n "$fps"          ] || fps=''
  89. [ -n "$ss"           ] || ss=0:00
  90. [ -n "$d"            ] || d=5.0
  91. [ -n "$vidstab"      ] || vidstab=''
  92. [ -n "$dither"       ] || dither=bayer
  93. [ -n "$bayer_scale"  ] || bayer_scale=2
  94. [ -n "$out"          ] || out=out
  95. [ -n "$vidstab_shakiness" ] || vidstab_shakiness=8
  96. [ -n "$vidstab_stepsize"  ] || vidstab_stepsize=1
  97. if [ -n "$curves" ]; then
  98.     if [[ ! ( "$curves" =~ [0-9.]+:[0-9.]+:[0-9.]+ ) ]];then
  99.         echo "Bad curves definition \'$curves' on line $LINENO"
  100.         exit 1
  101.     fi
  102.     c1=${curves%%:*}
  103.     c3=${curves##*:}
  104.     ctmp=${curves#*:}
  105.     c2=${ctmp%:*}
  106.     curves_filter="$(curves $c1 $c2 $c3),"
  107. fi
  108.  
  109. echo "final vars:"
  110. print_vars
  111. [ "$check_vars" != 1 ] || exit 0
  112.  
  113. crop="crop=$cropw:$croph:$cropx:$cropy"
  114. filters="trim=duration=$d,$(fps $fps)${curves_filter}setpts=PTS-STARTPTS,$(SARscale iw),
  115.     $(scale $w $h),$crop,format=yuv420p"
  116. palette=/tmp/ffgif-$$-p.png
  117.  
  118. vidstab1="vidstabdetect=shakiness=$vidstab_shakiness:stepsize=$vidstab_stepsize"
  119. vidstab2="vidstabtransform"
  120.  
  121. palettegen="palettegen=$colors:0:diff"
  122. paletteuse="paletteuse=$dither:$bayer_scale"
  123.  
  124. if [ "$vidstab" != 1 ]; then
  125.     runffmpeg -ss $ss -i "$file" -vf "$filters,$palettegen" $y "$palette"
  126.     runffmpeg -ss $ss -i "$file" -i "$palette" \
  127.         -lavfi "[0:v]$filters[x],[x][1:v]$paletteuse[o]" \
  128.         -map [o] $y "${out}.gif"
  129.     rm "$palette"
  130. else
  131.     tmpfile=/tmp/ffgif-$$-clip.mp4
  132.     runffmpeg -ss $ss -i "$file" -t $d -b:v 2000k -vf "$filters" "$tmpfile"
  133.  
  134.     runffmpeg -i "$tmpfile" -vf "$palettegen" $y "$palette"
  135.     runffmpeg -i "$tmpfile" -pass 1 -vf "$vidstab1" -f null -
  136.     runffmpeg -i "$tmpfile" -i "$palette" -pass 2 \
  137.         -lavfi "[0:v]$vidstab2[x],[x][1:v]$paletteuse[o]" \
  138.         -map [o] $y "${out}.gif"
  139.     rm "$tmpfile"
  140.     rm "$palette"
  141. fi
Advertisement
Add Comment
Please, Sign In to add comment