Last active
October 18, 2024 07:25
-
-
Save WoozyMasta/b70ba64cb59cedab8661a261695e004a to your computer and use it in GitHub Desktop.
Download telegram stickers and convert to PNG/GIF with optimizations
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
#!/usr/bin/env bash | |
# Download telegram stickers and convert to PNG/GIF | |
# | |
# Dependecies: | |
# - jq, curl | |
# - https://www.ffmpeg.org/ | |
# - https://pngquant.org/ | |
# - http://www.lcdf.org/gifsicle/ | |
# - https://github.com/ed-asriyan/lottie-converter | |
# | |
# TOKEN=123456 ./this https://t.me/addstickers/HANGSEED_Pepe Pepetopanim spinningfood notxxx | |
set -euo pipefail | |
: "${TOKEN:?Get token from @BotFather adn set to var}" | |
: "${name:-${1:?Specify the stickerpack name or url}}" | |
# WebP -> PNG | |
convert::webp () { | |
< /dev/null ffmpeg -y -hide_banner -loglevel error -i "$1" "${1%.*}.png" | |
rm -f "$1" | |
pngquant --force --skip-if-larger --strip "${1%.*}.png" | |
[ -f "${1%.*}-fs8.png" ] && \ | |
mv "${1%.*}-fs8.png" "${1%.*}.png" | |
} | |
# WebM -> GIF | |
convert::webm () { | |
< /dev/null ffmpeg -y -hide_banner -loglevel error -i "$1" \ | |
-lavfi fps=10,palettegen "${1%.*}.palette.png" | |
# TODO transparency not work | |
< /dev/null ffmpeg -y -hide_banner -loglevel error -i "$1" \ | |
-i "${1%.*}.palette.png" -lavfi "fps=10,paletteuse=alpha_threshold=0" \ | |
-gifflags -offsetting "${1%.*}.gif" | |
rm -f "$1" "${1%.*}.palette.png" | |
gifsicle --batch -O3 -i -f "${1%.*}.gif" | |
} | |
# TGS -> GIF | |
convert::tgs () { | |
local tmp="${1%.*}_tmp" | |
mkdir -p "$tmp" | |
gunzip -c "$1" > "$tmp/anim.json" | |
lottie_to_png --output "$tmp" "$tmp/anim.json" | |
< /dev/null ffmpeg -y -hide_banner -loglevel error -i "$tmp/%03d.png" \ | |
-vf palettegen=reserve_transparent=1 "$tmp/palette.png" | |
< /dev/null ffmpeg -y -hide_banner -loglevel error -i "$tmp/%03d.png" \ | |
-i "$tmp/palette.png" -lavfi paletteuse=alpha_threshold=128 \ | |
-gifflags -offsetting "${1%.*}.gif" | |
rm -r "$1" "$tmp" | |
gifsicle --batch -O3 -i -f "${1%.*}.gif" | |
} | |
mkdir -p "stickers" | |
for pack in "$@"; do | |
pack="${pack##*/}" | |
mkdir -p "stickers/$pack" | |
while IFS=$'\t' read -r file_id width height emoji animated video id _; do | |
echo "Proces: $id $emoji ${width}x$height in pack $pack" | |
file_path=$( | |
curl -sSfL "https://api.telegram.org/bot$TOKEN/getFile" \ | |
-F file_id="$file_id" | \ | |
jq -er '.result.file_path' | |
) | |
result_file="stickers/$pack/${file_path##*/}" | |
curl -sSfL "https://api.telegram.org/file/bot$TOKEN/$file_path" -o \ | |
"$result_file" | |
if [ "$video" == true ]; then | |
convert::webm "$result_file" | |
elif [ "$animated" == true ] ; then | |
convert::tgs "$result_file" | |
else | |
convert::webp "$result_file" | |
fi | |
done < <( | |
curl -sSfL "https://api.telegram.org/bot$TOKEN/getStickerset" \ | |
-F "name=$pack" | \ | |
jq -er ' | |
.result.stickers[] | [ | |
.file_id, .width, .height, .emoji, | |
.is_animated, .is_video, .file_unique_id | |
] | @tsv' | \ | |
tr -d '\r' | |
) | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment