Skip to content

Instantly share code, notes, and snippets.

@DavideGalilei
Created June 14, 2022 11:30
Show Gist options
  • Save DavideGalilei/469062af2a37b13624a04d2d18ddd1ab to your computer and use it in GitHub Desktop.
Save DavideGalilei/469062af2a37b13624a04d2d18ddd1ab to your computer and use it in GitHub Desktop.
Speed up Telegram .tgs stickers, using a lottie precomposition layer
import sys
import gzip
import json
if len(sys.argv) < 3:
print("Wrong args. Usage: python speed.py file.tgs 150%")
sticker = json.load(gzip.open(sys.argv[1]))
stretch = 100 / float(sys.argv[2].strip(" %"))
# https://lottiefiles.github.io/lottie-docs/breakdown/precomps/
# https://lottiefiles.github.io/lottie-docs/layers/#precomposition-layer
sticker["assets"].append(
{
"id": "precomp_layer",
"layers": sticker["layers"]
}
)
sticker["layers"] = [
{
"ty": 0,
"ip": sticker["ip"] * stretch,
"op": sticker["op"] * stretch,
"w": sticker["w"],
"h": sticker["h"],
"refId": "precomp_layer",
"st": 0,
"sr": stretch,
"ind": 0,
"ks": {}
}
]
sticker["op"] *= stretch
with open(f"speed_output.tgs", "wb") as output_tgs, open("speed_output.json", "w") as output_json:
json.dump(sticker, output_json, indent=2)
jsoned = json.dumps(sticker, separators=(',', ':'))
output_tgs.write(gzip.compress(jsoned.encode()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment