telegram gif fix #4

This commit is contained in:
esef committed 2026-06-23 19:30:57 +03:00
1 parent 206ddf893e
commit 4a3e1b7029
1 file changed
+8 -7
+8 -7
View File
@@ -146,7 +146,7 @@ class TelegramForwarder:
# Discord webhooks reject files > 25MB (sometimes > 8MB), and GIFs are massively unoptimized compared to MP4s.
process = await asyncio.create_subprocess_exec(
'ffmpeg', '-y', '-i', temp_mp4_path,
'-vf', '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,
stdout=asyncio.subprocess.DEVNULL,
stderr=asyncio.subprocess.PIPE
@@ -158,19 +158,20 @@ class TelegramForwarder:
except asyncio.TimeoutError:
process.kill()
_, stderr_data = await process.communicate()
print("ffmpeg gif conversion timed out after 30 seconds.")
print("ffmpeg gif conversion timed out after 30 seconds.", flush=True)
if process.returncode == 0 and os.path.exists(temp_gif_path):
gif_size = os.path.getsize(temp_gif_path)
# Fallback to MP4 if GIF is > 8MB, to prevent Discord from rejecting the webhook post
if gif_size < 8 * 1024 * 1024:
# Fallback to MP4 if GIF is > 10 MiB, to prevent Discord from rejecting the webhook post
if gif_size < 10 * 1024 * 1024:
with open(temp_gif_path, 'rb') as f:
file_data = f.read()
filename = filename.rsplit('.', 1)[0] + '.gif'
print(f"ffmpeg conversion succeeded: GIF size {gif_size / 1024 / 1024:.2f} MiB", flush=True)
else:
print(f"GIF was too large for Discord webhook ({gif_size / 1024 / 1024:.2f}MB). Falling back to MP4.")
print(f"GIF was too large for Discord webhook ({gif_size / 1024 / 1024:.2f} MiB). Falling back to MP4.", flush=True)
else:
print(f"ffmpeg failed with code {process.returncode}. Stderr: {stderr_data.decode('utf-8', errors='ignore')}")
print(f"ffmpeg failed with code {process.returncode}. Stderr: {stderr_data.decode('utf-8', errors='ignore')}", flush=True)
try:
if os.path.exists(temp_mp4_path): os.remove(temp_mp4_path)
@@ -178,7 +179,7 @@ class TelegramForwarder:
except Exception:
pass
except Exception as e:
print(f'ffmpeg gif conversion threw an exception: {e}')
print(f'ffmpeg gif conversion threw an exception: {e}', flush=True)
text = f'{prefix}{caption}'.strip()
await self._send_to_webhook(webhook_url, sender, text, file_data, filename)