tracks: hash the carried payload with -f streamhash, never through a PIPE pair
stream_signature() ran ffmpeg -f data - with stdout=PIPE, stderr=PIPE and drained stdout first, reading stderr only after EOF. A source that floods stderr (a Matroska with 1,612 'exceeds containing master element' errors = 219,020 bytes) fills the 64 KB stderr buffer, ffmpeg blocks writing it, stdout never closes and the read loop blocks for ever - 42 min at 2.1 s of CPU, and the supervisor's gate watchdog killed the same item three times at 60 min each. p.wait(timeout=1800) cannot help: it is unreachable until that loop ends. -f streamhash -hash md5 - lets ffmpeg do the md5 itself (byte-identical: 5cb7483040... both ways on that file, 2.3 s instead of a 48.9 MB pipe) and one subprocess.run(capture_output=True, timeout=...) drains both fds on threads, so the stderr flood cannot wedge and a wedged demuxer costs a bounded wait that returns None (the gate then says 'could not hash payload', never 'payload differs'). Return shape (nb_packets, md5) and every caller unchanged. test_tracks 55/55, including 'e2e mkv/mp4: copied streams are byte-identical' on the real fixtures.
This commit is contained in:
1 parent
7c1a243aad
commit
b1b7dbdff1
3 files changed
+73
-16
No files matched your search
+30
-16
@@ -38,7 +38,6 @@ same plan (`verify`).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
@@ -478,6 +477,17 @@ def stream_signature(path, sel, timeout=1800):
|
||||
`-bsf:a aac_adtstoasc` agree (955905a0 / 955905a0), and the decoded PCM agrees too. So for
|
||||
AAC the dump goes through that bitstream filter on BOTH sides - it strips ADTS where it exists
|
||||
and is a pass-through where it does not.
|
||||
|
||||
One hard-won constraint on HOW it is computed (measured 2026-11-15, IMOG-159.mkv): the
|
||||
payload must NOT be piped through this process. The previous version ran
|
||||
`ffmpeg ... -f data -` with `stdout=PIPE, stderr=PIPE` and drained stdout in a read loop
|
||||
while stderr was read only after EOF. A source that floods stderr - that Matroska emitted
|
||||
219,020 bytes / 1,630 container-error lines - fills the ~64 KB stderr pipe, ffmpeg blocks
|
||||
writing it, the stdout pipe never closes and the loop blocks for ever: 42 minutes at 2.1 s
|
||||
of CPU, and the supervisor's 60 min stall watchdog had already killed the same item twice.
|
||||
`-f streamhash` lets FFMPEG do the md5 itself (same 32 hex, byte-identical to the pipe
|
||||
dump: 5cb7483040621df964779a9e0fb21d45 both ways on that file, 2.3 s instead of a 48.9 MB
|
||||
pipe), and `capture_output=True` drains both pipes concurrently, so nothing can wedge.
|
||||
"""
|
||||
bsf = []
|
||||
r = subprocess.run([FFPROBE, "-v", "error", "-select_streams", sel, "-count_packets",
|
||||
@@ -495,24 +505,28 @@ def stream_signature(path, sel, timeout=1800):
|
||||
# 「copy」这个动作,不是流的真实编码;而这一条 bsf 必须按**真实编码**决定。
|
||||
if codec_name.lower() in ("aac", "aac_latm"):
|
||||
bsf = ["-bsf:a", "aac_adtstoasc"]
|
||||
p = subprocess.Popen([FFMPEG, "-v", "error", "-i", path, "-map", "0:" + sel, "-c", "copy"] + bsf +
|
||||
["-f", "data", "-"], stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
||||
creationflags=NOWIN)
|
||||
h = hashlib.md5()
|
||||
# `-f streamhash` = ffmpeg's own md5 of the packet payloads (no container bytes), so there is
|
||||
# no payload in flight between us and it. ONE capture_output call drains stdout AND stderr on
|
||||
# separate threads (`communicate`), which is what makes the stderr flood harmless - and on
|
||||
# `timeout` the child is killed, so a wedged demuxer costs one bounded wait instead of a lane.
|
||||
try:
|
||||
while True:
|
||||
chunk = p.stdout.read(1 << 20)
|
||||
if not chunk:
|
||||
break
|
||||
h.update(chunk)
|
||||
finally:
|
||||
p.stdout.close()
|
||||
err = p.stderr.read() if p.stderr else b""
|
||||
p.stderr.close()
|
||||
p.wait(timeout=timeout)
|
||||
p = subprocess.run([FFMPEG, "-v", "error", "-i", path, "-map", "0:" + sel, "-c", "copy"] + bsf +
|
||||
["-f", "streamhash", "-hash", "md5", "-"],
|
||||
capture_output=True, text=True, encoding="utf-8", errors="replace",
|
||||
timeout=timeout, creationflags=NOWIN)
|
||||
except subprocess.TimeoutExpired:
|
||||
return None
|
||||
if p.returncode != 0:
|
||||
return None
|
||||
return nb, h.hexdigest()
|
||||
md5 = ""
|
||||
for line in (p.stdout or "").splitlines():
|
||||
# `0,a,MD5=<hex>` - the leading index is the output stream, `a` its type
|
||||
if "MD5=" in line:
|
||||
md5 = line.rsplit("MD5=", 1)[1].strip().lower()
|
||||
break
|
||||
if len(md5) != 32:
|
||||
return None
|
||||
return nb, md5
|
||||
|
||||
|
||||
def copy_streams_intact(plan_, src_path, out_path):
|
||||
|
||||
Reference in new issue
Block a user