sticker to gif conversion

This commit is contained in:
esef committed 2026-06-23 19:47:38 +03:00
1 parent 92b004c668
commit 0358baa522
1 file changed
+35 -44
+35 -44
View File
@@ -193,55 +193,46 @@ 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): file_data, filename = await self._download_file(message.sticker.file_id)
# Download .tgs sticker file try:
file_data, filename = await self._download_file(message.sticker.file_id) # Determine original file suffix for temporary file
try: suffix = os.path.splitext(filename)[1] or '.dat'
# Write to a temporary .tgs file with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as temp_in:
with tempfile.NamedTemporaryFile(suffix='.tgs', delete=False) as temp_tgs: temp_in.write(file_data)
temp_tgs.write(file_data) temp_in_path = temp_in.name
temp_tgs_path = temp_tgs.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,
stderr=asyncio.subprocess.PIPE stderr=asyncio.subprocess.PIPE
) )
stdout, stderr = await asyncio.wait_for(process.communicate(), timeout=30.0) stdout, stderr = await asyncio.wait_for(process.communicate(), timeout=30.0)
if process.returncode == 0 and os.path.exists(temp_gif_path): if process.returncode == 0 and os.path.exists(temp_gif_path):
gif_size = os.path.getsize(temp_gif_path) gif_size = os.path.getsize(temp_gif_path)
if gif_size < 10 * 1024 * 1024: if gif_size < 10 * 1024 * 1024:
with open(temp_gif_path, 'rb') as f: with open(temp_gif_path, 'rb') as f:
file_data = f.read() file_data = f.read()
filename = filename.rsplit('.', 1)[0] + '.gif' filename = filename.rsplit('.', 1)[0] + '.gif'
else:
print('[STICKER-HANDLER] Converted GIF exceeds size limit; sending original .tgs', flush=True)
else: else:
print(f"[STICKER-HANDLER] ffmpeg error: {stderr.decode()}", flush=True) print('[STICKER-HANDLER] Converted GIF exceeds size limit; sending original file', flush=True)
except Exception as e: else:
print(f'Sticker conversion skipped/failed: {e}', flush=True) print(f"[STICKER-HANDLER] ffmpeg error: {stderr.decode()}", flush=True)
finally: except Exception as e:
# Clean up temporary files print(f'Sticker conversion skipped/failed: {e}', flush=True)
if os.path.exists(temp_tgs_path): finally:
os.remove(temp_tgs_path) if os.path.exists(temp_in_path):
if os.path.exists(temp_gif_path): os.remove(temp_in_path)
os.remove(temp_gif_path) if os.path.exists(temp_gif_path):
await self._send_to_webhook(webhook_url, sender, text, file_data, filename) os.remove(temp_gif_path)
else: await self._send_to_webhook(webhook_url, sender, text, file_data, filename)
# Static sticker – just forward the file # <<< STICKER TO GIF CONVERSION END <<<
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}'