Commit 88e1af20 by wangzhiyang

feat(会议纪要脚本): 新增远程媒体时长探测的临时文件降级方案

新增URL文件后缀提取、远程文件下载至临时目录的工具函数,新增时长探测降级逻辑:先直接探测远程媒体时长,失败则下载临时文件后再次探测;更新媒体限制检查逻辑使用新的降级探测函数并正确清理临时文件,解决直接探测远程媒体文件时长可能失败的问题。
1 parent 9ee5f46b
...@@ -15,6 +15,7 @@ import re ...@@ -15,6 +15,7 @@ import re
import shutil import shutil
import subprocess import subprocess
import sys import sys
import tempfile
from http import HTTPStatus from http import HTTPStatus
from typing import Optional from typing import Optional
from urllib import request from urllib import request
...@@ -104,6 +105,57 @@ def _summarize_ffmpeg_output(stdout: str, stderr: str, *, max_lines: int = 30, m ...@@ -104,6 +105,57 @@ def _summarize_ffmpeg_output(stdout: str, stderr: str, *, max_lines: int = 30, m
summary = " | ".join(lines[:max_lines]) summary = " | ".join(lines[:max_lines])
return summary[:max_chars] return summary[:max_chars]
def _url_ext(url: str) -> str:
u = (url or "").split("?", 1)[0].split("#", 1)[0]
if "." not in u:
return ""
ext = u.rsplit(".", 1)[-1].lower()
if 0 < len(ext) <= 8 and re.fullmatch(r"[a-z0-9]+", ext or ""):
return ext
return ""
def _download_to_temp_file(url: str, *, expected_size_bytes: Optional[int]) -> Optional[str]:
suffix = ""
ext = _url_ext(url)
if ext:
suffix = f".{ext}"
try:
tmp = tempfile.NamedTemporaryFile(prefix="meeting_media_", suffix=suffix, delete=False)
tmp_path = tmp.name
tmp.close()
except Exception as e:
logger.info("创建临时文件失败: %s", str(e))
return None
try:
with _http_open(url, method="GET") as resp, open(tmp_path, "wb") as f:
downloaded = 0
while True:
chunk = resp.read(1024 * 1024)
if not chunk:
break
f.write(chunk)
downloaded += len(chunk)
if downloaded > _MAX_MEDIA_BYTES:
return None
if expected_size_bytes is not None and expected_size_bytes > 0:
if os.path.exists(tmp_path) and os.path.getsize(tmp_path) != expected_size_bytes:
logger.info("临时下载文件大小与预期不一致: expected=%s actual=%s", expected_size_bytes, os.path.getsize(tmp_path))
return tmp_path
except Exception as e:
logger.info("下载媒体到临时文件失败: %s", str(e))
return None
finally:
if expected_size_bytes is None and os.path.exists(tmp_path):
try:
size = os.path.getsize(tmp_path)
if size > _MAX_MEDIA_BYTES:
os.remove(tmp_path)
except Exception:
pass
def _probe_duration_seconds(url: str) -> Optional[float]: def _probe_duration_seconds(url: str) -> Optional[float]:
ffmpeg = _get_ffmpeg_exe() ffmpeg = _get_ffmpeg_exe()
...@@ -148,6 +200,27 @@ def _probe_duration_seconds(url: str) -> Optional[float]: ...@@ -148,6 +200,27 @@ def _probe_duration_seconds(url: str) -> Optional[float]:
return None return None
return duration_seconds return duration_seconds
def _probe_duration_seconds_with_fallback(url: str, *, size_bytes: Optional[int]) -> Optional[float]:
duration = _probe_duration_seconds(url)
if duration is not None:
return duration
if size_bytes is not None and size_bytes > _MAX_MEDIA_BYTES:
return None
logger.info("尝试下载媒体到本地后再次探测时长")
tmp_path = _download_to_temp_file(url, expected_size_bytes=size_bytes)
if not tmp_path:
return None
try:
return _probe_duration_seconds(tmp_path)
finally:
try:
os.remove(tmp_path)
except Exception:
pass
def _probe_media_limits(url: str) -> dict: def _probe_media_limits(url: str) -> dict:
size_bytes = _get_remote_size_bytes(url) size_bytes = _get_remote_size_bytes(url)
...@@ -157,7 +230,7 @@ def _probe_media_limits(url: str) -> dict: ...@@ -157,7 +230,7 @@ def _probe_media_limits(url: str) -> dict:
"error": f"音视频文件过大:{round(size_bytes / 1024 / 1024 / 1024, 2)} GB,超过 2 GB", "error": f"音视频文件过大:{round(size_bytes / 1024 / 1024 / 1024, 2)} GB,超过 2 GB",
} }
duration_seconds = _probe_duration_seconds(url) duration_seconds = _probe_duration_seconds_with_fallback(url, size_bytes=size_bytes)
if duration_seconds is None: if duration_seconds is None:
return { return {
"success": False, "success": False,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!