Last active
June 19, 2022 14:30
-
-
Save DavideGalilei/e3fc850e01069aadb51d6a54809da47d to your computer and use it in GitHub Desktop.
Combine Telegram animated stickers (.tgs) in a single one
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
import sys | |
import gzip | |
import json | |
from collections import defaultdict | |
if len(sys.argv) < 3: | |
print("Wrong args. Usage: python combine.py file1.tgs file2.tgs ...") | |
files = map(lambda f: open(f, "rb"), sys.argv[1:]) | |
stickers = [json.loads(gzip.decompress(file.read())) for file in files] | |
for file in files: | |
file.close() | |
final = stickers[0] | |
id_counter = 0 | |
ref_ids = defaultdict(lambda: [None, None]) | |
for layer in final["layers"]: | |
if ref_id := layer.get("refId"): | |
ref_ids[ref_id][0] = layer | |
for asset in final["assets"]: | |
ref_ids[asset["id"]][1] = asset | |
for sticker in stickers[1:]: | |
for layer in sticker["layers"]: | |
if ref_id := layer.get("refId"): | |
ref_ids[ref_id][0] = layer | |
for asset in sticker["assets"]: | |
ref_ids[asset["id"]][1] = asset | |
final["assets"].extend(sticker["assets"]) | |
final["layers"].extend(sticker["layers"]) | |
for layer, asset in ref_ids.values(): | |
if layer and asset and (ref_id := layer.get("refId")): | |
layer["refId"] = asset["id"] = f"id{id_counter}" | |
id_counter += 1 | |
with open("output.tgs", "wb") as output_tgs, open("output.json", "w+") as output_json: | |
json.dump(final, output_json, indent=4) | |
jsoned = json.dumps(final, 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