diff --git a/scripts/tracks.py b/scripts/tracks.py index eb06dab..26db767 100644 --- a/scripts/tracks.py +++ b/scripts/tracks.py @@ -468,19 +468,35 @@ def stream_signature(path, sel, timeout=1800): differs" on streams that were byte-identical (a false failure on a good file is the one outcome this gate must never produce). Cost is one linear read of the stream's payload - ~2 s per 10 min of audio - and no giant JSON in memory like `-show_packets`. + + ONE exception to "container-independent", and it is an ADTS one: an AAC stream in MPEG-TS is + carried as ADTS frames (7-byte header per frame), while MP4 stores the same frames raw with a + single `AudioSpecificConfig`. `-f data` therefore returns different bytes for the SAME audio, + and the gate reported `a 0: payload differs (src 157137 packets/342d39a4, out 157137 + packets/38f9b7e6)` - identical packet counts, one extra header per frame. Measured on + KODV-0081.ts: raw dumps disagree (87f3e5b6 / 955905a0), both sides through + `-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. """ + bsf = [] r = subprocess.run([FFPROBE, "-v", "error", "-select_streams", sel, "-count_packets", - "-show_entries", "stream=nb_read_packets", "-of", "csv=p=0", path], + "-show_entries", "stream=codec_name,nb_read_packets", "-of", "csv=p=0", path], capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=timeout, creationflags=NOWIN) if r.returncode != 0: return None try: - nb = int((r.stdout or "").strip().splitlines()[0]) + parts = (r.stdout or "").strip().splitlines()[0].split(",") + codec_name, nb = (parts[0].strip(), int(parts[1])) except Exception: # noqa: BLE001 return None - p = subprocess.Popen([FFMPEG, "-v", "error", "-i", path, "-map", "0:" + sel, "-c", "copy", - "-f", "data", "-"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, + # 编解码器从 ffprobe 现读,而不是相信调用方传进来的字符串:`plan_` 里存的 `codec` 是 + # 「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() try: