forwarder polish
This commit is contained in:
1 parent
e11d712ba0
commit
5e69d316db
1 file changed
+36
-6
+36
-6
@@ -105,27 +105,57 @@ class TelegramForwarder:
|
|||||||
|
|
||||||
# --- START OF ENHANCED GIF HANDLING BLOCK ---
|
# --- START OF ENHANCED GIF HANDLING BLOCK ---
|
||||||
# Telegram may send a GIF as an `animation` (the normal case) *or*
|
# Telegram may send a GIF as an `animation` (the normal case) *or*
|
||||||
# as a generic `document`/`video` with MIME type `video/mp4`. We want to
|
# as a muted `video` / `document` with a video MIME type.
|
||||||
# treat any of those as a potential GIF and try to convert it to a true
|
# Videos with an audio track are real videos → sent as-is.
|
||||||
# GIF for Discord auto‑looping.
|
# Silent videos are treated as GIFs → converted for Discord auto-loop.
|
||||||
elif getattr(message, "animation", None) or (
|
elif getattr(message, "animation", None) or (
|
||||||
getattr(message, "document", None) and message.document.mime_type.startswith("video")
|
getattr(message, "document", None) and message.document.mime_type.startswith("video")
|
||||||
) or message.video:
|
) or message.video:
|
||||||
# Identify the file source
|
|
||||||
if message.animation:
|
if message.animation:
|
||||||
file_id = message.animation.file_id
|
file_id = message.animation.file_id
|
||||||
|
is_animation = True
|
||||||
elif message.video:
|
elif message.video:
|
||||||
file_id = message.video.file_id
|
file_id = message.video.file_id
|
||||||
|
is_animation = False
|
||||||
else:
|
else:
|
||||||
file_id = message.document.file_id
|
file_id = message.document.file_id
|
||||||
|
is_animation = False
|
||||||
|
|
||||||
print(f"[GIF-HANDLER] Detected potential media (file_id={file_id})", flush=True)
|
print(f"[GIF-HANDLER] Detected potential media (file_id={file_id})", flush=True)
|
||||||
file_data, filename = await self._download_file(file_id)
|
file_data, filename = await self._download_file(file_id)
|
||||||
|
|
||||||
# Attempt conversion if it's a video/animation
|
# Probe for audio track to decide: real video or GIF?
|
||||||
|
convert_to_gif = is_animation # animations never have audio
|
||||||
|
|
||||||
|
if not is_animation and shutil.which('ffprobe'):
|
||||||
|
try:
|
||||||
|
with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as tmp:
|
||||||
|
tmp.write(file_data)
|
||||||
|
tmp_path = tmp.name
|
||||||
|
|
||||||
|
probe = await asyncio.create_subprocess_exec(
|
||||||
|
'ffprobe', '-v', 'error',
|
||||||
|
'-select_streams', 'a',
|
||||||
|
'-show_entries', 'stream=codec_type',
|
||||||
|
'-of', 'csv=p=0',
|
||||||
|
tmp_path,
|
||||||
|
stdout=asyncio.subprocess.PIPE,
|
||||||
|
stderr=asyncio.subprocess.PIPE
|
||||||
|
)
|
||||||
|
stdout, _ = await asyncio.wait_for(probe.communicate(), timeout=15.0)
|
||||||
|
os.remove(tmp_path)
|
||||||
|
|
||||||
|
# ffprobe outputs "audio" if an audio stream is present
|
||||||
|
convert_to_gif = b'audio' not in stdout
|
||||||
|
if not convert_to_gif:
|
||||||
|
print("[GIF-HANDLER] Audio track detected — sending as video", flush=True)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[GIF-HANDLER] ffprobe failed, defaulting to convert: {e}", flush=True)
|
||||||
|
convert_to_gif = True
|
||||||
|
|
||||||
|
if convert_to_gif:
|
||||||
# >>> FFMPEG GIF CONVERSION START >>>
|
# >>> FFMPEG GIF CONVERSION START >>>
|
||||||
try:
|
try:
|
||||||
|
|
||||||
if not shutil.which('ffmpeg'):
|
if not shutil.which('ffmpeg'):
|
||||||
raise RuntimeError("ffmpeg binary not found in PATH")
|
raise RuntimeError("ffmpeg binary not found in PATH")
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user