Added parameter in config for selecting convert type

This commit is contained in:
Randall Lawrence
2019-09-25 13:09:21 +03:00
parent dc71f74c0c
commit f5c008c1a7
5 changed files with 17 additions and 10 deletions
+6
View File
@@ -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
+1
View File
@@ -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):
+3 -1
View File
@@ -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
+6 -4
View File
@@ -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":
+1 -5
View File
@@ -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: