From f5c008c1a76b18abfe1813ca29dbb0c42b73099c Mon Sep 17 00:00:00 2001 From: Randall Lawrence Date: Wed, 25 Sep 2019 13:09:21 +0300 Subject: [PATCH] Added parameter in config for selecting convert type --- example-config.yaml | 6 ++++++ mautrix_telegram/config.py | 1 + mautrix_telegram/portal/telegram.py | 4 +++- mautrix_telegram/util/file_transfer.py | 10 ++++++---- mautrix_telegram/util/tgs_converter.py | 6 +----- 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/example-config.yaml b/example-config.yaml index e5e18d53..507d0d76 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -163,6 +163,12 @@ bridge: image_as_file_size: 10 # Maximum size of Telegram documents in megabytes to bridge. max_document_size: 100 + # Format, animated sticker convert to (unstable). + # Supported values: + # image - converts to png (preferred), + # gif - converts to gif animation (sometimes loses alpha), + # video - VP9 video in mp4 container + animated_sticker_target_type: image # Whether to bridge Telegram bot messages as m.notices or m.texts. bot_messages_as_notices: true diff --git a/mautrix_telegram/config.py b/mautrix_telegram/config.py index fb80a26f..3d4cf621 100644 --- a/mautrix_telegram/config.py +++ b/mautrix_telegram/config.py @@ -101,6 +101,7 @@ class Config(BaseBridgeConfig): copy("bridge.inline_images") copy("bridge.image_as_file_size") copy("bridge.max_document_size") + copy("bridge.animated_sticker_target_type") copy("bridge.bot_messages_as_notices") if isinstance(self["bridge.bridge_notices"], bool): diff --git a/mautrix_telegram/portal/telegram.py b/mautrix_telegram/portal/telegram.py index 6ccf62cd..fde07f24 100644 --- a/mautrix_telegram/portal/telegram.py +++ b/mautrix_telegram/portal/telegram.py @@ -182,7 +182,9 @@ class PortalTelegram(BasePortal, ABC): thumb_loc = None thumb_size = None file = await util.transfer_file_to_matrix(source.client, intent, document, thumb_loc, - is_sticker=attrs.is_sticker) + is_sticker=attrs.is_sticker, + tgs_convert_type= + self.get_config("animated_sticker_target_type")) if not file: return None diff --git a/mautrix_telegram/util/file_transfer.py b/mautrix_telegram/util/file_transfer.py index 94da7fc3..b7dfe002 100644 --- a/mautrix_telegram/util/file_transfer.py +++ b/mautrix_telegram/util/file_transfer.py @@ -161,7 +161,8 @@ TypeThumbnail = Optional[Union[TypeLocation, TypePhotoSize]] async def transfer_file_to_matrix(client: MautrixTelegramClient, intent: IntentAPI, location: TypeLocation, thumbnail: TypeThumbnail = None, - is_sticker: bool = False) -> Optional[DBTelegramFile]: + is_sticker: bool = False, tgs_convert_type: str = "image") \ + -> Optional[DBTelegramFile]: location_id = _location_to_id(location) if not location_id: return None @@ -177,12 +178,13 @@ async def transfer_file_to_matrix(client: MautrixTelegramClient, intent: IntentA transfer_locks[location_id] = lock async with lock: return await _unlocked_transfer_file_to_matrix(client, intent, location_id, location, - thumbnail, is_sticker) + thumbnail, is_sticker, tgs_convert_type) async def _unlocked_transfer_file_to_matrix(client: MautrixTelegramClient, intent: IntentAPI, loc_id: str, location: TypeLocation, - thumbnail: TypeThumbnail, is_sticker: bool + thumbnail: TypeThumbnail, is_sticker: bool, + tgs_convert_type: str ) -> Optional[DBTelegramFile]: db_file = DBTelegramFile.get(loc_id) if db_file: @@ -201,7 +203,7 @@ async def _unlocked_transfer_file_to_matrix(client: MautrixTelegramClient, inten image_converted = False if mime_type == "application/gzip" and is_sticker: - mime_type, file, width, height, thumbnail = convert_tgs(file, "image", 256, 256) + mime_type, file, width, height, thumbnail = convert_tgs(file, tgs_convert_type, 256, 256) image_converted = width is not None if mime_type == "image/webp": diff --git a/mautrix_telegram/util/tgs_converter.py b/mautrix_telegram/util/tgs_converter.py index 34d0299b..ad08ac3f 100644 --- a/mautrix_telegram/util/tgs_converter.py +++ b/mautrix_telegram/util/tgs_converter.py @@ -20,7 +20,7 @@ try: def _tgs_to_png(animation: Animation, width: int = None, height: int = None, frame: int = None) -> Tuple[bytes, Optional[bytes]]: if not frame: - frame = int(animation.out_point * 0.3) + frame = int(animation.out_point * 0.9) if not (width and height): width = animation.width height = animation.height @@ -82,13 +82,9 @@ try: import numpy import tempfile import os - from PIL import Image def _tgs_to_video(animation: Animation, width: int = None, height: int = None) \ -> Tuple[bytes, Optional[bytes]]: - """ - FIXME: copy-pasted from tgs.exporters.video, because it's method don't resize images - """ start = int(animation.in_point) end = int(animation.out_point) with tempfile.NamedTemporaryFile(mode="r+b", suffix=".mp4") as tmp: