Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- function runffmpeg() {
- echo --------------------------------------------
- echo "ffmpeg" "$@"
- echo --------------------------------------------
- ffmpeg -hide_banner "$@"
- }
- # color curves
- function curves() { echo -n "colorbalance=$1:$1:$1:$2:$2:$2:$3:$3:$3"; }
- # scale videos with sar≠1 to have given width and pixel size 1x1
- function SARscale() { echo -n "scale=trunc($1/2)*2:trunc((ih*$1)/(iw*sar*2))*2,setsar=1"; }
- # usage: scale <width> [<height>]
- function scale() { [ -n "$2" ] && echo -n "scale=$1:$2" || scale $1 -1; }
- function fps() { [ -n "$1" ] && echo -n "fps=$1,"; }
- function print_vars() {
- echo "w = $w"
- echo "cropw = $cropw"
- echo "croph = $croph"
- echo "cropx = $cropx"
- echo "cropy = $cropy"
- echo "colors = $colors"
- echo "fps = $fps"
- echo "ss = $ss"
- echo "d = $d"
- echo "dither = $dither"
- echo "bayer_scale = $bayer_scale"
- echo "out = $out"
- echo "curves = $curves"
- echo "vidstab = $vidstab"
- echo "vidstab_shakiness = $vidstab_shakiness"
- echo "vidstab_stepsize = $vidstab_stepsize"
- }
- function print_help() {
- echo "converts video to gif"
- echo "usage: ${0##*/} [-y|-v|-h] [<file>]"
- echo ""
- echo "Options:"
- echo " -y - automatically override the output file"
- echo " -v - print user-set variables, final variables and then exit"
- echo " -h - print help, user-set variables and exit"
- echo ""
- echo "The following enviroment variables can be set:"
- echo ""
- echo " file - the file to convert, has to be defined or passed as an argument to the script"
- echo " w - width, height is set to keep the aspect ratio, default: iw"
- echo " cropx, cropy, cropw, croph - crop x/y (top left corner of crop) and with/height"
- echo " defaults are 0/0, iw/ih"
- echo " colors - the number of colors in gif, default: 50"
- echo " fps - fps, default is empty string - keep fps of the source"
- echo " ss - seek position, default: 0:00"
- echo " d - duration, default: 5.0"
- echo " out - filename of output (without extension), default: out"
- echo " vidstab - 1 => use vidstab, not set by default"
- echo " vidstab_shakiness - shakiness for vidstab, default: 8"
- echo " vidstab_stepsize - step size for vidstab, default: 1"
- echo " dither, bayer_scale - dithering method, and bayer scale (if dither == bayer)"
- echo " default: bayer, 2"
- echo " curves - applies colorbalance filter, is defined as 'a:b:c' where 0:0:0"
- echo " is standard color balance with range -1 to 1 (the filter separates r/g/b,"
- echo " this script doesn't)"
- echo ""
- echo "vars: "
- print_vars
- [ -z "$1" ] || exit "$1"
- }
- set -e
- case "$1" in
- (-h*) print_help 0;;
- (-v) check_vars=1; shift;;
- (-y) y="-y"; shift;;
- esac
- [ $# -eq 0 ] || file="$1"
- [ -n "$file" ] || print_help 1
- echo "user set vars:"
- print_vars
- [ -n "$w" ] || w=iw
- [ -n "$cropw" ] || cropw=iw
- [ -n "$croph" ] || croph=ih
- [ -n "$cropx" ] || cropx=0
- [ -n "$cropy" ] || cropy=0
- [ -n "$colors" ] || colors=50
- [ -n "$fps" ] || fps=''
- [ -n "$ss" ] || ss=0:00
- [ -n "$d" ] || d=5.0
- [ -n "$vidstab" ] || vidstab=''
- [ -n "$dither" ] || dither=bayer
- [ -n "$bayer_scale" ] || bayer_scale=2
- [ -n "$out" ] || out=out
- [ -n "$vidstab_shakiness" ] || vidstab_shakiness=8
- [ -n "$vidstab_stepsize" ] || vidstab_stepsize=1
- if [ -n "$curves" ]; then
- if [[ ! ( "$curves" =~ [0-9.]+:[0-9.]+:[0-9.]+ ) ]];then
- echo "Bad curves definition \'$curves' on line $LINENO"
- exit 1
- fi
- c1=${curves%%:*}
- c3=${curves##*:}
- ctmp=${curves#*:}
- c2=${ctmp%:*}
- curves_filter="$(curves $c1 $c2 $c3),"
- fi
- echo "final vars:"
- print_vars
- [ "$check_vars" != 1 ] || exit 0
- crop="crop=$cropw:$croph:$cropx:$cropy"
- filters="trim=duration=$d,$(fps $fps)${curves_filter}setpts=PTS-STARTPTS,$(SARscale iw),
- $(scale $w $h),$crop,format=yuv420p"
- palette=/tmp/ffgif-$$-p.png
- vidstab1="vidstabdetect=shakiness=$vidstab_shakiness:stepsize=$vidstab_stepsize"
- vidstab2="vidstabtransform"
- palettegen="palettegen=$colors:0:diff"
- paletteuse="paletteuse=$dither:$bayer_scale"
- if [ "$vidstab" != 1 ]; then
- runffmpeg -ss $ss -i "$file" -vf "$filters,$palettegen" $y "$palette"
- runffmpeg -ss $ss -i "$file" -i "$palette" \
- -lavfi "[0:v]$filters[x],[x][1:v]$paletteuse[o]" \
- -map [o] $y "${out}.gif"
- rm "$palette"
- else
- tmpfile=/tmp/ffgif-$$-clip.mp4
- runffmpeg -ss $ss -i "$file" -t $d -b:v 2000k -vf "$filters" "$tmpfile"
- runffmpeg -i "$tmpfile" -vf "$palettegen" $y "$palette"
- runffmpeg -i "$tmpfile" -pass 1 -vf "$vidstab1" -f null -
- runffmpeg -i "$tmpfile" -i "$palette" -pass 2 \
- -lavfi "[0:v]$vidstab2[x],[x][1:v]$paletteuse[o]" \
- -map [o] $y "${out}.gif"
- rm "$tmpfile"
- rm "$palette"
- fi
Advertisement
Add Comment
Please, Sign In to add comment