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
+2 -1
View File
@@ -228,12 +228,13 @@ bridge:
# png - converts to non-animated png (fastest),
# gif - converts to animated gif
# webm - converts to webm video, requires ffmpeg executable with vp9 codec and webm container support
# webp - converts to animated webp, requires ffmpeg executable with webp codec/container support
target: gif
# Arguments for converter. All converters take width and height.
args:
width: 256
height: 256
fps: 25 # only for webm and gif (2, 5, 10, 20 or 25 recommended)
fps: 25 # only for webm, webp and gif (2, 5, 10, 20 or 25 recommended)
# End-to-bridge encryption support options.
#
# See https://docs.mau.fi/bridges/general/end-to-bridge-encryption.html for more info.
+1 -1
View File
@@ -280,7 +280,7 @@ async def _telegram_entities_to_matrix(
html.append(entity_text)
elif entity_type == ReuploadedCustomEmoji:
html.append(
f'<img data-mx-emoticon src="{escape(entity.file.mxc)}" height="32" '
f'<img data-mx-emoticon src="{escape(entity.file.mxc)}" height="32" width="32" '
f'alt="{entity_text}" title="{entity_text}"/>'
)
elif entity_type in (
+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(