Skip to content

Commit

Permalink
Fixes #1 and #2
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanh committed Oct 13, 2016
1 parent fceba04 commit ef84128
Showing 1 changed file with 42 additions and 14 deletions.
56 changes: 42 additions & 14 deletions ffmpeg2gif.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

VERSION=0.2.0
VERSION=0.3.0
SUBJECT=ffmpeg2gif
USAGE="Usage: ffmpeg2gif.sh -hv -f <framerate> -y <yscale> input.mov output.gif"
USAGE="Usage: ffmpeg2gif.sh [-hv -f <framerate> -y <yscale> -s <skip_sec> -t <duration_sec>] input.mov output.gif"

# --- Options processing -------------------------------------------
if [ $# == 0 ] ; then
Expand All @@ -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")
Expand All @@ -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;
Expand All @@ -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
# -----------------------------------------------------------------

0 comments on commit ef84128

Please sign in to comment.