Created
June 14, 2022 11:30
-
-
Save DavideGalilei/469062af2a37b13624a04d2d18ddd1ab to your computer and use it in GitHub Desktop.
Speed up Telegram .tgs stickers, using a lottie precomposition layer
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
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