Added parameter in config for selecting convert type
This commit is contained in:
@@ -163,6 +163,12 @@ bridge:
|
|||||||
image_as_file_size: 10
|
image_as_file_size: 10
|
||||||
# Maximum size of Telegram documents in megabytes to bridge.
|
# Maximum size of Telegram documents in megabytes to bridge.
|
||||||
max_document_size: 100
|
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.
|
# Whether to bridge Telegram bot messages as m.notices or m.texts.
|
||||||
bot_messages_as_notices: true
|
bot_messages_as_notices: true
|
||||||
|
|||||||
@@ -101,6 +101,7 @@ class Config(BaseBridgeConfig):
|
|||||||
copy("bridge.inline_images")
|
copy("bridge.inline_images")
|
||||||
copy("bridge.image_as_file_size")
|
copy("bridge.image_as_file_size")
|
||||||
copy("bridge.max_document_size")
|
copy("bridge.max_document_size")
|
||||||
|
copy("bridge.animated_sticker_target_type")
|
||||||
|
|
||||||
copy("bridge.bot_messages_as_notices")
|
copy("bridge.bot_messages_as_notices")
|
||||||
if isinstance(self["bridge.bridge_notices"], bool):
|
if isinstance(self["bridge.bridge_notices"], bool):
|
||||||
|
|||||||
@@ -182,7 +182,9 @@ class PortalTelegram(BasePortal, ABC):
|
|||||||
thumb_loc = None
|
thumb_loc = None
|
||||||
thumb_size = None
|
thumb_size = None
|
||||||
file = await util.transfer_file_to_matrix(source.client, intent, document, thumb_loc,
|
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:
|
if not file:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|||||||
@@ -161,7 +161,8 @@ TypeThumbnail = Optional[Union[TypeLocation, TypePhotoSize]]
|
|||||||
|
|
||||||
async def transfer_file_to_matrix(client: MautrixTelegramClient, intent: IntentAPI,
|
async def transfer_file_to_matrix(client: MautrixTelegramClient, intent: IntentAPI,
|
||||||
location: TypeLocation, thumbnail: TypeThumbnail = None,
|
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)
|
location_id = _location_to_id(location)
|
||||||
if not location_id:
|
if not location_id:
|
||||||
return None
|
return None
|
||||||
@@ -177,12 +178,13 @@ async def transfer_file_to_matrix(client: MautrixTelegramClient, intent: IntentA
|
|||||||
transfer_locks[location_id] = lock
|
transfer_locks[location_id] = lock
|
||||||
async with lock:
|
async with lock:
|
||||||
return await _unlocked_transfer_file_to_matrix(client, intent, location_id, location,
|
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,
|
async def _unlocked_transfer_file_to_matrix(client: MautrixTelegramClient, intent: IntentAPI,
|
||||||
loc_id: str, location: TypeLocation,
|
loc_id: str, location: TypeLocation,
|
||||||
thumbnail: TypeThumbnail, is_sticker: bool
|
thumbnail: TypeThumbnail, is_sticker: bool,
|
||||||
|
tgs_convert_type: str
|
||||||
) -> Optional[DBTelegramFile]:
|
) -> Optional[DBTelegramFile]:
|
||||||
db_file = DBTelegramFile.get(loc_id)
|
db_file = DBTelegramFile.get(loc_id)
|
||||||
if db_file:
|
if db_file:
|
||||||
@@ -201,7 +203,7 @@ async def _unlocked_transfer_file_to_matrix(client: MautrixTelegramClient, inten
|
|||||||
|
|
||||||
image_converted = False
|
image_converted = False
|
||||||
if mime_type == "application/gzip" and is_sticker:
|
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
|
image_converted = width is not None
|
||||||
|
|
||||||
if mime_type == "image/webp":
|
if mime_type == "image/webp":
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ try:
|
|||||||
def _tgs_to_png(animation: Animation, width: int = None,
|
def _tgs_to_png(animation: Animation, width: int = None,
|
||||||
height: int = None, frame: int = None) -> Tuple[bytes, Optional[bytes]]:
|
height: int = None, frame: int = None) -> Tuple[bytes, Optional[bytes]]:
|
||||||
if not frame:
|
if not frame:
|
||||||
frame = int(animation.out_point * 0.3)
|
frame = int(animation.out_point * 0.9)
|
||||||
if not (width and height):
|
if not (width and height):
|
||||||
width = animation.width
|
width = animation.width
|
||||||
height = animation.height
|
height = animation.height
|
||||||
@@ -82,13 +82,9 @@ try:
|
|||||||
import numpy
|
import numpy
|
||||||
import tempfile
|
import tempfile
|
||||||
import os
|
import os
|
||||||
from PIL import Image
|
|
||||||
|
|
||||||
def _tgs_to_video(animation: Animation, width: int = None, height: int = None) \
|
def _tgs_to_video(animation: Animation, width: int = None, height: int = None) \
|
||||||
-> Tuple[bytes, Optional[bytes]]:
|
-> Tuple[bytes, Optional[bytes]]:
|
||||||
"""
|
|
||||||
FIXME: copy-pasted from tgs.exporters.video, because it's method don't resize images
|
|
||||||
"""
|
|
||||||
start = int(animation.in_point)
|
start = int(animation.in_point)
|
||||||
end = int(animation.out_point)
|
end = int(animation.out_point)
|
||||||
with tempfile.NamedTemporaryFile(mode="r+b", suffix=".mp4") as tmp:
|
with tempfile.NamedTemporaryFile(mode="r+b", suffix=".mp4") as tmp:
|
||||||
|
|||||||
Reference in New Issue
Block a user