From ef8412899b99c5ac8d84cc1d1d3116ac1298eac7 Mon Sep 17 00:00:00 2001 From: Jordan Husney Date: Thu, 13 Oct 2016 11:50:03 -0400 Subject: [PATCH] Fixes #1 and #2 --- ffmpeg2gif.sh | 56 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/ffmpeg2gif.sh b/ffmpeg2gif.sh index 4d77287..8a84d1b 100755 --- a/ffmpeg2gif.sh +++ b/ffmpeg2gif.sh @@ -20,9 +20,9 @@ # along with this program. If not, see . # -VERSION=0.2.0 +VERSION=0.3.0 SUBJECT=ffmpeg2gif -USAGE="Usage: ffmpeg2gif.sh -hv -f -y input.mov output.gif" +USAGE="Usage: ffmpeg2gif.sh [-hv -f -y -s -t ] input.mov output.gif" # --- Options processing ------------------------------------------- if [ $# == 0 ] ; then @@ -32,8 +32,10 @@ fi framerate=30 yscale=480 +skip_sec=-1 +duration_sec=-1 -while getopts ":f:y:vh" optname +while getopts ":f:y:s:t:vh" optname do case "$optname" in "v") @@ -56,6 +58,22 @@ while getopts ":f:y:vh" optname echo "invalid yscale \"$OPTARG\"" && exit 1 fi ;; + "s") + if [[ "$OPTARG" =~ ^[0-9]+$ ]] && [ "$OPTARG" -ge 1 ]; then + echo "skip_sec: $OPTARG" + skip_sec=$OPTARG + else + echo "invalid skip_sec \"$OPTARG\"" && exit 1 + fi + ;; + "t") + if [[ "$OPTARG" =~ ^[0-9]+$ ]] && [ "$OPTARG" -ge 1 ]; then + echo "duration_sec: $OPTARG" + duration_sec=$OPTARG + else + echo "invalid duration_sec \"$OPTARG\"" && exit 1 + fi + ;; "h") echo $USAGE exit 0; @@ -80,24 +98,34 @@ shift $(($OPTIND - 1)) inputfile=$1 outputfile=$2 -[[ -z $inputfile ]] && echo "no input file specified" && exit -1 -[[ -z $outputfile ]] && echo "no output file specified" && exit -1 +[[ -z "$inputfile" ]] && echo "no input file specified" && exit -1 +[[ -z "$outputfile" ]] && echo "no output file specified" && exit -1 # --- Body -------------------------------------------------------- # SCRIPT LOGIC GOES HERE -echo "input file: $inputfile" -echo "output file: $outputfile" +echo "input file: \"$inputfile\"" +echo "output file: \"$outputfile\"" palettefile=$(mktemp /tmp/$SUBJECT.XXXXXX).png filters="fps=$framerate,scale=$yscale:-1:flags=lanczos" -ffmpeg -v warning -i $1 -vf "$filters,palettegen" -y $palettefile && - ffmpeg -v warning -i $inputfile -i $palettefile \ - -lavfi "$filters [x]; [x][1:v] paletteuse" -y $outputfile +optional_params="" +if [ "$skip_sec" -ge 1 ]; then + optional_params="-ss $skip_sec" +fi +if [ "$duration_sec" -ge 1 ]; then + optional_params="$optional_params -t $duration_sec" +fi + +ffmpeg $optional_params -v warning \ + -i "$inputfile" -vf "$filters,palettegen" -y "$palettefile" && + ffmpeg $optional_params -v warning -i "$inputfile" -i "$palettefile" \ + -lavfi "$filters [x]; [x][1:v] paletteuse" -y "$outputfile" -if [ -f $outputfile ]; then - outputsize=`du -h $outputfile | cut -f1` - echo "$outputfile is $outputsize" +if [ -f "$outputfile" ]; then + outputsize=`du -h "$outputfile" | cut -f1` + echo "\"$outputfile\" is $outputsize" fi -rm "$palettefile" +rm "$palettefile" 2> /dev/null +exit 0 # -----------------------------------------------------------------