#!/usr/bin/env python3 """恢复视频的原生时钟及真实MEMO完整1FPS解码审计;不生成回答。""" import hashlib import json from pathlib import Path import sys import time import decord import numpy as np import torch ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / 'scripts')) sys.path.insert(0, str(ROOT / 'repos/MEMO')) from download_assets import atomic, sha from memo.stage1.frame_extractor import FrameExtractor from memo.stage1.sampling import build_sample_indices def main(): torch.set_num_threads(4) started = time.perf_counter() identity = json.loads((ROOT / 'provenance/sample332_publisher_identity_audit.json').read_text()) download = json.loads((ROOT / 'provenance/youtube_candidate_full_download.json').read_text()) assert identity['passed'] and identity['samples'] == 50 and download['completed'] path = ROOT / 'data/media_recovery/sample_332_restored_publisher_copy.mp4' source = ROOT / download['path'] assert sha(source) == download['sha256'] source_vr = decord.VideoReader(str(source), ctx=decord.cpu(0), num_threads=1) restored_vr = decord.VideoReader(str(path), ctx=decord.cpu(0), num_threads=1) official_vr = decord.VideoReader(str(ROOT / 'data/StreamingBench/videos/sample_332_real.mp4'), ctx=decord.cpu(0), num_threads=1) count = len(restored_vr) fps = float(restored_vr.get_avg_fps()) assert count == 18220 and abs(fps-30000/1001) < 1e-8 source_pts = np.asarray(source_vr.get_frame_timestamp(list(range(count+2)))) restored_pts = np.asarray(restored_vr.get_frame_timestamp(list(range(count)))) official_pts = np.asarray(official_vr.get_frame_timestamp(list(range(count)))) # 原官方片段也是按DTS取18220个packet,最后显示帧对应发布者18220而非18219。 # 必须保持官方全部PTS;不能为符合直觉而修改原采样器或末帧时钟。 assert np.array_equal(official_pts, restored_pts) source_mapping = np.searchsorted(source_pts[:, 0], restored_pts[:, 0]) assert np.array_equal(source_pts[source_mapping, 0], restored_pts[:, 0]) assert np.all(np.diff(restored_pts[:, 0]) > 0) indices = build_sample_indices(count, fps, 1., anchor_end=False) video = FrameExtractor(fps=1., backend='decord', anchor_end=False).extract(str(path)) assert video.native_indices == indices and len(video.frames) == 607 rows = [] for frame, index in zip(video.frames, indices): assert frame.image.shape == (1080, 1920, 3) and frame.image.dtype == np.uint8 rows.append(dict(native_index=index, timestamp=float(frame.timestamp), native_pts=float(restored_pts[index, 0]), rgb_sha256=hashlib.sha256(frame.image.tobytes()).hexdigest())) # remux不重编码;在全片11个位置核对源视频的真实RGB,含原坏区后的部分。 decord.bridge.set_bridge('native') comparisons = [] for i in list(range(0, len(indices), 60)) + [len(indices)-1]: index = indices[i] source_index = int(source_mapping[index]) image = source_vr[source_index].asnumpy() digest = hashlib.sha256(image.tobytes()).hexdigest() comparisons.append(dict(native_index=index, publisher_native_index=source_index, source_rgb_sha256=digest, restored_rgb_sha256=rows[i]['rgb_sha256'], passed=digest == rows[i]['rgb_sha256'])) assert all(row['passed'] for row in comparisons) times = [13., 175., 180., 329., 500.] queries = [] for query_time in times: visible = [row for row in rows if row['timestamp'] <= query_time+1e-6] assert visible and max(row['native_pts'] for row in visible) <= query_time+1e-6 queries.append(dict(query_time=query_time, ingested_frames=len(visible), max_native_pts=max(row['native_pts'] for row in visible))) proof = dict(passed=True, original_official_path='data/StreamingBench/videos/sample_332_real.mp4', original_official_sha256=identity['official_sha256'], recovered_path=str(path.relative_to(ROOT)), recovered_sha256=sha(path), recovered_bytes=path.stat().st_size, publisher_source_path=download['path'], publisher_source_sha256=download['sha256'], public_source=download['public_source'], identity_audit_sha256=sha(ROOT / 'provenance/sample332_publisher_identity_audit.json'), frames=count, fps=fps, duration=count/fps, sampled_frames=len(rows), all_native_PTS_exactly_equal_to_official_clip=True, publisher_native_mapping='0..18218 then 18220; original_and_restored_DTS_packet_cut_identical', original_MEMO_Full_FrameExtractor_decode_passed=True, decode_fallback='see_logs/source332_restored_audit.rerun.log; no_custom_decoder_or_sampling_change', original_bad_file_preserved=True, reencoding=False, synthesized_frames=0, questions_or_answers_changed=False, query_checks=queries, sampled_frame_hashes=rows, source_RGB_comparisons=comparisons, wall_CPU_seconds=time.perf_counter()-started, boundary='source_recovery_from_verified_publisher; P_frame_encoding_differs_from_original_benchmark; common_R0_R1_R2_media_required') atomic(ROOT / 'provenance/sample332_restored_decode_audit.json', proof) print(json.dumps({key: value for key, value in proof.items() if key not in ('sampled_frame_hashes', 'source_RGB_comparisons')}), flush=True) if __name__ == '__main__': main()