Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # convert *.gif file (and *.sfw and *flv) to *.webm
- # resulting file is much smaller, though there will be more artifacts
- FFMPEG="/usr/local/bin/ffmpeg"
- let BITRATE=1024 # initial bitrate, may downgrade
- AUDIO="-an" # default is audio=null
- if [ ! -x $FFMPEG ]; then echo >&2 "missing $FFMPEG"; exit 1; fi
- F=$1
- if [ ! -e "$F" ]; then
- echo >&2 "missing file: $F";
- exit 1;
- fi
- if [[ "$F" =~ .gif$ ]]; then
- G="${F/.gif/.webm}"
- let BITRATE=1024
- AUDIO=-an
- elif [[ "$F" =~ .flv$ ]]; then
- G="${F/.flv/.webm}"
- let BITRATE=384
- AUDIO="-ac 1"
- elif [[ "$F" =~ .swf$ ]]; then
- G="${F/.swf/.webm}"
- let BITRATE=384
- AUDIO="-ac 1"
- else
- echo >&2 "not a a recognized format : $F";
- exit 1;
- fi
- OLDSIZE=$(stat -c%s "$F")
- RATIO=100
- while [[ RATIO -ge 100 ]]; do
- $FFMPEG -loglevel error -i "$F" $AUDIO -c:v libvpx -b:v ${BITRATE}K "$G"
- [ -e "$G" ] || break;
- NEWSIZE=$(stat -c%s "$G")
- RATIO=$(bc <<<"scale=0; $NEWSIZE * 100 / $OLDSIZE")
- if [[ RATIO -ge 100 ]]; then
- rm "$G"
- let BITRATE/=2
- if [[ BITRATE -lt 32 ]];then
- [ "$TERM" ] && echo "giving up; can't reasonably reduce file size";
- break;
- else
- [ "$TERM" ] && echo "bitrate ${BITRATE}K increased size to ${RATIO}%; trying again"
- fi
- fi
- done
- [[ -e "$G" ]] || exit 1;
- [ "$TERM" ] && echo "$G reduced to ${RATIO}%"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement