Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #default values
- #based on http://superuser.com/questions/556029/how-do-i-convert-a-video-to-gif-using-ffmpeg-with-reasonable-quality
- fps=15
- width=320
- filename() {
- if [ ! -f "$1" ]; then return 1; fi
- filename1=$(basename "$1")
- filename1="${filename1%.*}"
- echo "$filename1"
- }
- numcomp() {
- #compare two floating point numbers
- #example: numcomp 2 -gt 3 - return status 1
- if [ "$#" -ne 3 ]; then echo "compare: wrong number of parameters" >&2; return 1; fi
- case "$2" in
- '-gt') op='>';;
- '-ge') op='>=';;
- '-lt') op='<';;
- '-le') op='<=';;
- *) echo "compare: wrong operator" >&2; exit 1 ;;
- esac
- output="$(echo "$1 $op $3" 2>&1 | bc -l 2>&1)"
- case "$output" in
- '0') return 1;;
- '1') return 0;;
- *) echo "compare error: $output"; exit 1;;
- esac
- }
- gengif() {
- if [ ! -f "$1" ]; then
- echo "$1 doesn't exists!" >&2
- return 1
- fi
- f_width="$(mediainfo '--Inform=Video;%Width%' "$1")"
- f_fps="$(mediainfo '--Inform=Video;%FrameRate%' "$1")"
- if [ "$fps" = 'auto' ] || [ "$fps" = '0' ]; then
- fps=$f_fps
- echo "fps value set to $fps - from source video"
- elif numcomp "$f_fps" -lt "$fps"; then
- echo "File fps [$f_fps] is lower than target fps [$fps]. Setting target fps to $f_fps ."
- fps="$f_fps"
- fi
- if [ "$width" = 'auto' ] || [ "$width" = '0' ]; then
- width=$f_width
- echo "Width value set to $width - from source video"
- elif [ "$f_width" -lt "$width" ]; then
- echo "File width [$f_width] is lower than target width [$width]. Setting target width to $f_width ."
- width="$f_width"
- fi
- output="$(filename "$1")-${width}p-${fps}fps.gif"
- if [ -f "$output" ]; then
- echo "$output already exists!" >&2
- exit 1
- fi
- palette="$(mktemp ./palette.XXXXXX.png)"
- echo "ffmpeg -y -i $1 -vf fps=$fps,scale=$width:-1:flags=lanczos,palettegen $palette"
- ffmpeg -y -i "$1" -vf fps=$fps,scale=$width:-1:flags=lanczos,palettegen "$palette"
- echo "ffmpeg -i $1 -i $palette -filter_complex fps=$fps,scale=$width:-1:flags=lanczos[x];[x][1:v]paletteuse $output"
- ffmpeg -i "$1" -i "$palette" -filter_complex \
- "fps=$fps,scale=$width:-1:flags=lanczos[x];[x][1:v]paletteuse" "$output"
- rm "$palette"
- }
- help() {
- echo "
- VIDEO TO GIF GENERATOR
- USAGE: $0 [-f FPS] [-w WIDTH] [-h] MOVIE1 [MOVIE2 ...]
- OPTIONS:
- -h - print this help
- -f FPS - output fps. Can be floatpoint. (default is $fps)
- '0' or 'auto' to set to source video fps
- -w WIDTH - output width (default is $width)
- '0' or 'auto' to set to sorce video width
- If provided fps or width is greater than in source video, then value from source video will be used.
- "
- exit 0
- }
- if [ $# -eq 0 ]; then
- help
- fi
- while [ $# -gt 0 ]; do
- case "$1" in
- -f) fps="$2"; shift;;
- -w) width="$2"; shift;;
- -h) help;;
- --) shift; break;;
- -*) help
- exit 1;;
- *) break;;# terminate while loop
- esac
- shift
- # all command line switches are processed,
- # "$@" contains all file names
- done
- for k in "$@"; do
- gengif "$k"
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement