Last active
June 16, 2022 16:18
-
-
Save tihasdop/0870fce96e7115d6db358a7c3b428c92 to your computer and use it in GitHub Desktop.
Converts gif image to webm (vp9) video
This file contains 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 | |
# Copyright (c) 2018 Alexander Tomilov | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining | |
# a copy of this software and associated documentation files (the | |
# "Software"), to deal in the Software without restriction, including | |
# without limitation the rights to use, copy, modify, merge, publish, | |
# distribute, sublicense, and/or sell copies of the Software, and to | |
# permit persons to whom the Software is furnished to do so, subject to | |
# the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included | |
# in all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
help() { | |
echo "Convert gif image to webm/vp9 video." | |
echo | |
echo "Usage: $(basename "$0") [-crf crf_value] GIF_FILE [GIF_FILE...]" | |
echo | |
echo "The CRF value must be from 0--63." | |
echo "If undefined, the value will be determined automatically." | |
} | |
define_crf() { | |
h=$(identify -format "%h" "$1[0]") | |
if [ "$?" -ne 0 ]; then | |
exit 1 | |
fi | |
# Quality values from: | |
# https://developers.google.com/media/vp9/settings/vod/#recommended_settings | |
if [ "$h" -lt 300 ]; then | |
return 34 # 480p (low) quality | |
elif [ "$h" -lt 800 ]; then | |
return 32 # 720p quality | |
elif [ "$h" -lt 1000 ]; then | |
return 31 # 1080p quality | |
elif [ "$h" -lt 1500 ]; then | |
return 24 # 1440p quality | |
else | |
return 15 # 2160p quality | |
fi | |
} | |
if [ \( $# -lt 1 \) -o \( "$1" = "--help" \) ]; then | |
help | |
exit 0 | |
fi | |
crf=-1 | |
if [ "$1" = "-crf" ]; then | |
if [ \( "$2" -lt 0 \) -o \( "$2" -gt 63 \) ]; then | |
echo "The CRF value must be from 0--63!" | |
exit 1 | |
fi | |
crf="$2" | |
shift 2 | |
fi | |
for gif in "$@"; do | |
if [ \( ! -f "$gif" \) -o \( "$(file --mime-type -b "$gif")" != "image/gif" \) ]; then | |
echo "$gif isn't a gif image!" | |
exit 1 | |
fi | |
done | |
for gif in "$@"; do | |
if [ "$crf" = -1 ]; then # auto CRF definition | |
define_crf "$gif" | |
cur_crf="$?" | |
else | |
cur_crf="$crf" | |
fi | |
echo -e "\n\e[1;31m### $gif" | |
echo -e "### CRF=${cur_crf}\e[0m" | |
ffmpeg -hide_banner -i "$gif" -c:v libvpx-vp9 -b:v 0 -crf "$cur_crf" "${gif}.webm" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment