sticker to gif conversion
This commit is contained in:
1 parent
92b004c668
commit
0358baa522
1 file changed
+12
-21
+12
-21
@@ -193,20 +193,19 @@ class TelegramForwarder:
|
|||||||
elif message.sticker:
|
elif message.sticker:
|
||||||
sticker_text = message.sticker.emoji or '(sticker)'
|
sticker_text = message.sticker.emoji or '(sticker)'
|
||||||
text = f'{prefix}{sticker_text}'
|
text = f'{prefix}{sticker_text}'
|
||||||
# >>> ANIMATED STICKER CONVERSION START >>>
|
# >>> STICKER TO GIF CONVERSION START >>>
|
||||||
if getattr(message.sticker, "is_animated", False):
|
|
||||||
# Download .tgs sticker file
|
|
||||||
file_data, filename = await self._download_file(message.sticker.file_id)
|
file_data, filename = await self._download_file(message.sticker.file_id)
|
||||||
try:
|
try:
|
||||||
# Write to a temporary .tgs file
|
# Determine original file suffix for temporary file
|
||||||
with tempfile.NamedTemporaryFile(suffix='.tgs', delete=False) as temp_tgs:
|
suffix = os.path.splitext(filename)[1] or '.dat'
|
||||||
temp_tgs.write(file_data)
|
with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as temp_in:
|
||||||
temp_tgs_path = temp_tgs.name
|
temp_in.write(file_data)
|
||||||
|
temp_in_path = temp_in.name
|
||||||
|
|
||||||
temp_gif_path = temp_tgs_path + '.gif'
|
temp_gif_path = temp_in_path + '.gif'
|
||||||
|
|
||||||
process = await asyncio.create_subprocess_exec(
|
process = await asyncio.create_subprocess_exec(
|
||||||
'ffmpeg', '-y', '-i', temp_tgs_path,
|
'ffmpeg', '-y', '-i', temp_in_path,
|
||||||
'-filter_complex', 'fps=15,scale=320:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse',
|
'-filter_complex', 'fps=15,scale=320:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse',
|
||||||
'-loop', '0', temp_gif_path,
|
'-loop', '0', temp_gif_path,
|
||||||
stdout=asyncio.subprocess.DEVNULL,
|
stdout=asyncio.subprocess.DEVNULL,
|
||||||
@@ -222,26 +221,18 @@ class TelegramForwarder:
|
|||||||
file_data = f.read()
|
file_data = f.read()
|
||||||
filename = filename.rsplit('.', 1)[0] + '.gif'
|
filename = filename.rsplit('.', 1)[0] + '.gif'
|
||||||
else:
|
else:
|
||||||
print('[STICKER-HANDLER] Converted GIF exceeds size limit; sending original .tgs', flush=True)
|
print('[STICKER-HANDLER] Converted GIF exceeds size limit; sending original file', flush=True)
|
||||||
else:
|
else:
|
||||||
print(f"[STICKER-HANDLER] ffmpeg error: {stderr.decode()}", flush=True)
|
print(f"[STICKER-HANDLER] ffmpeg error: {stderr.decode()}", flush=True)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f'Sticker conversion skipped/failed: {e}', flush=True)
|
print(f'Sticker conversion skipped/failed: {e}', flush=True)
|
||||||
finally:
|
finally:
|
||||||
# Clean up temporary files
|
if os.path.exists(temp_in_path):
|
||||||
if os.path.exists(temp_tgs_path):
|
os.remove(temp_in_path)
|
||||||
os.remove(temp_tgs_path)
|
|
||||||
if os.path.exists(temp_gif_path):
|
if os.path.exists(temp_gif_path):
|
||||||
os.remove(temp_gif_path)
|
os.remove(temp_gif_path)
|
||||||
await self._send_to_webhook(webhook_url, sender, text, file_data, filename)
|
await self._send_to_webhook(webhook_url, sender, text, file_data, filename)
|
||||||
else:
|
# <<< STICKER TO GIF CONVERSION END <<<
|
||||||
# Static sticker – just forward the file
|
|
||||||
try:
|
|
||||||
file_data, filename = await self._download_file(message.sticker.file_id)
|
|
||||||
await self._send_to_webhook(webhook_url, sender, text, file_data, filename)
|
|
||||||
except Exception:
|
|
||||||
await self._send_to_webhook(webhook_url, sender, text)
|
|
||||||
# <<< ANIMATED STICKER CONVERSION END <<<
|
|
||||||
|
|
||||||
elif message.text:
|
elif message.text:
|
||||||
text = f'{prefix}{message.text}'
|
text = f'{prefix}{message.text}'
|
||||||
|
|||||||
Reference in new issue
Block a user