Add support for converting animated stickers to webp

This commit is contained in:
Tulir Asokan
2022-08-12 22:07:52 +03:00
parent 76eafbf48c
commit a5fe05cff2
4 changed files with 35 additions and 3 deletions
+6 -1
View File
@@ -228,13 +228,18 @@ async def transfer_custom_emojis_to_matrix(
GetCustomEmojiDocumentsRequest(document_id=not_existing_ids)
)
tgs_args = source.config["bridge.animated_sticker"]
if tgs_args["target"] == "webm":
# Inline images can't be videos, let's hope animated webp is supported
tgs_args = {**tgs_args, "target": "webp"}
async def transfer(document: Document) -> None:
file_map[document.id] = await transfer_file_to_matrix(
source.client,
source.bridge.az.intent,
document,
is_sticker=True,
tgs_convert={"target": "png", "args": {"width": 256, "height": 256}},
tgs_convert=tgs_args,
filename=f"emoji-{document.id}",
# Emojis are used as inline images and can't be encrypted
encrypt=False,
+26
View File
@@ -126,7 +126,33 @@ if lottieconverter and ffmpeg:
log.error(str(e))
return ConvertedSticker("application/gzip", file)
async def tgs_to_webp(
file: bytes, width: int, height: int, fps: int = 30, **_: Any
) -> ConvertedSticker:
with tempfile.TemporaryDirectory(prefix="tgs_") as tmpdir:
file_template = tmpdir + "/out_"
try:
await _run_lottieconverter(
args=("-", file_template, "pngs", f"{width}x{height}", str(fps)),
input_data=file,
)
first_frame_name = min(os.listdir(tmpdir))
with open(f"{tmpdir}/{first_frame_name}", "rb") as first_frame_file:
first_frame_data = first_frame_file.read()
webp_data = await ffmpeg.convert_path(
input_args=("-framerate", str(fps), "-pattern_type", "glob"),
input_file=f"{file_template}*.png",
output_args=("-c:v", "libwebp_anim", "-pix_fmt", "yuva420p", "-f", "webp"),
output_path_override="-",
output_extension=None,
)
return ConvertedSticker("image/webp", webp_data, "image/png", first_frame_data)
except ffmpeg.ConverterError as e:
log.error(str(e))
return ConvertedSticker("application/gzip", file)
converters["webm"] = tgs_to_webm
converters["webp"] = tgs_to_webp
async def convert_tgs_to(