feat: household recording destinations and verified Baidu segment archive

This commit is contained in:
Codex
2026-10-16 13:02:12 +08:00
parent 292321ea20
commit 38201492af
42 changed files with 1787 additions and 112 deletions
+9 -6
View File
@@ -20,7 +20,7 @@ class PlaybackError(Exception):
@contextmanager
def cached_source(source, authorization, reserve_bytes):
def cached_source(source, authorization, reserve_bytes, response_factory=None):
# A private, bounded scratch file decouples MediaMTX's HTTP write timeout
# from browser buffering and pause. Use the data disk, not a small /tmp RAM disk.
base = Path(os.environ.get('VISION_DATA', tempfile.gettempdir())) / 'playback-tmp'
@@ -38,9 +38,9 @@ def cached_source(source, authorization, reserve_bytes):
raise PlaybackError('磁盘剩余空间不足以缓冲回放,请先释放空间')
request = urllib.request.Request(source, headers={'Authorization': authorization})
target = Path(folder) / 'source.mp4'
deadline = time.monotonic() + 30
deadline = time.monotonic() + (180 if response_factory else 30)
size = 0
with urllib.request.urlopen(request, timeout=10) as response, target.open('wb') as output:
with (response_factory() if response_factory else urllib.request.urlopen(request, timeout=10)) as response, target.open('wb') as output:
while chunk := response.read(256 * 1024):
size += len(chunk)
if size > MAX_SOURCE_BYTES:
@@ -87,7 +87,7 @@ def command(source, authorization, duration):
@contextmanager
def stream(source, authorization, duration, reserve_bytes=8 * 1024**3):
def stream(source, authorization, duration, reserve_bytes=8 * 1024**3, response_factory=None, offset=0):
if not 0 < duration <= MAX_SESSION_SECONDS:
raise PlaybackError('请刷新页面,使用每段最多 5 分钟的自动连续回放')
if not SLOTS.acquire(blocking=False):
@@ -96,9 +96,12 @@ def stream(source, authorization, duration, reserve_bytes=8 * 1024**3):
try:
if not shutil.which('ffmpeg'):
raise PlaybackError('回放转换需要服务器安装 FFmpeg')
with cached_source(source, authorization, reserve_bytes) as local_source:
with cached_source(source, authorization, reserve_bytes, response_factory=response_factory) as local_source:
# Credentials never appear in the converter's process arguments.
process = subprocess.Popen(command(local_source, '', duration),
args = command(local_source, '', duration)
if offset:
at = args.index('-t'); args[at:at] = ['-ss',str(offset)]
process = subprocess.Popen(args,
stdin=subprocess.DEVNULL, stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL)
timer = threading.Timer(duration + 45, process.kill)