Created
March 24, 2018 01:20
-
-
Save lbland94/9f3197a53a2b5f8fea5a251e3719ee3a to your computer and use it in GitHub Desktop.
A program to wrap ffmpeg specifically for usage with gifs. Allows speed manipulation and time clipping.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| POSITIONAL=() | |
| while [[ $# -gt 0 ]] | |
| do | |
| key="$1" | |
| case $key in | |
| -b|--begin|-ss) | |
| START="$2" | |
| shift # past argument | |
| shift # past value | |
| ;; | |
| -l|--length) | |
| LENGTH="true" | |
| shift # past argument | |
| ;; | |
| -t|--time) | |
| TIME="$2" | |
| shift # past argument | |
| shift # past value | |
| ;; | |
| -s|--speed) | |
| SPEED="$2" | |
| shift # past argument | |
| shift # past value | |
| ;; | |
| -o|--outfile) | |
| OUTFILE="$2" | |
| shift # past argument | |
| shift # past value | |
| ;; | |
| -h|--help) | |
| echo " | |
| Usage: giftool <OPTIONS> filename | |
| -b|--begin|-ss: The starting point to use in the video in seconds | |
| -t|--time: The length of the video in seconds after the start point | |
| -l|--length: Print the length of the file in seconds and exit | |
| -s|--speed: The factor by which to speed up or slow down the video | |
| -o|--outfile: The file name to use for the resultant file | |
| -h|--help: Show this message and exit | |
| " | |
| exit 0 | |
| ;; | |
| *) # unknown option | |
| POSITIONAL+=("$1") # save it in an array for later | |
| shift # past argument | |
| ;; | |
| esac | |
| done | |
| set -- "${POSITIONAL[@]}" # restore positional parameters | |
| if [[ -n $1 ]]; then | |
| FILE="$1" | |
| if [ ! -f "$FILE" ]; then | |
| echo "File does not exist; exiting" | |
| exit -1 | |
| fi | |
| else | |
| echo "No filename provided; exiting" | |
| exit 0 | |
| fi | |
| if [[ -n $LENGTH ]]; then | |
| if hash exiftool 2>/dev/null; then | |
| exiftool -Duration $FILE | grep -Eo '[0-9]+\.*[0-9]+' | |
| else | |
| echo "exiftool is required for this functionality" | |
| fi | |
| exit 0 | |
| fi | |
| if ! hash ffmpeg 2>/dev/null; then | |
| echo "ffmpeg is required for this program to work properly" | |
| exit -1 | |
| fi | |
| ## Get random tmp file for palette | |
| paletteFile=/tmp/$RANDOM-palette.png | |
| ## Trap exit to make sure we clean up our tmp file | |
| function finish { | |
| rm "$paletteFile" | |
| } | |
| trap finish EXIT | |
| filterCmd="-i '$1' -vf \ scale=iw:ih:flags=lanczos,palettegen $paletteFile &>/dev/null" | |
| cmd="-i $1 -i '$paletteFile' -filter_complex 'scale=iw:ih:flags=lanczos[x];[x][1:v]paletteuse" | |
| outfile="${1%%.*}" | |
| if [[ -n $SPEED ]]; then | |
| cmd=$cmd',setpts='$(bc -l <<< "1/"$SPEED | sed -e 's/[0]*$//g')'*PTS'"'" | |
| outfile="$outfile-x$SPEED" | |
| else | |
| cmd=$cmd"'" | |
| fi | |
| if [[ -n $TIME ]]; then | |
| filterCmd="-t $TIME $filterCmd" | |
| cmd="-t $TIME $cmd" | |
| if [[ -z ${START+x} ]]; then | |
| outfile="clipped-$outfile-0-$TIME" | |
| fi | |
| fi | |
| if [[ -n $START ]]; then | |
| filterCmd="-ss $START $filterCmd" | |
| cmd="-ss $START $cmd" | |
| if [[ -n $TIME ]]; then | |
| outfile="clipped-$outfile-$START-$(bc -l <<< "$START+$TIME")" | |
| else | |
| outfile="clipped-$outfile-$START-end" | |
| fi | |
| fi | |
| if [[ -n $OUTFILE ]]; then | |
| outfile=$OUTFILE | |
| else | |
| outfile="$outfile.gif" | |
| fi | |
| cmd="$cmd $outfile -v quiet -progress /dev/stdout 2>&1 > /dev/null" | |
| eval "ffmpeg $filterCmd" | |
| eval "ffmpeg $cmd" | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment