#!/usr/bin/env python3 """元数据规范化之外的跨剪辑内容匹配;完整覆盖前只报告partial审计。""" from array import array import argparse import collections import fcntl import itertools import json import os from pathlib import Path import sys import time ROOT=Path(__file__).resolve().parents[1] sys.path.insert(0,str(ROOT/'scripts')) from download_assets import atomic, safe_flock, sha def main(): parser=argparse.ArgumentParser();parser.add_argument('--allow-partial',action='store_true');args=parser.parse_args() cfg=json.loads((ROOT/'configs/content_overlap_protocol.json').read_text());started=time.perf_counter() manifest=json.loads((ROOT/'manifests/training_videos.candidate.json').read_text()) training_inventory=json.loads((ROOT/'provenance/training_media_inventory.json').read_text()) eval_inventory=json.loads((ROOT/'provenance/eval_media_inventory.json').read_text()) videos=[];stale=[];inputs={} for path in sorted((ROOT/'provenance/fingerprints').glob('*.json')): value=json.loads(path.read_text()) if value['status']!='completed':continue key=value['source_video'] if value['kind']=='training': valid=key in manifest and key in training_inventory and value.get('media_frames')==training_inventory[key]['files'] else:valid=key in eval_inventory and value.get('media_sha256')==eval_inventory[key]['sha256'] if not valid: stale.append(dict(kind=value['kind'],source_video=key,file=str(path.relative_to(ROOT))));continue # 媒体恢复/候选重选后的旧指纹不计覆盖;旧文件保留供来源追溯。 inputs[str(path.relative_to(ROOT))]=sha(path) value['fingerprint_file']=str(path);value['split']=manifest[value['source_video']]['splits'][0] if value['kind']=='training' else 'evaluation' videos.append(value) present={(v['kind'],v['source_video']) for v in videos} expected={('training',v) for v in manifest} media=json.loads((ROOT/'manifests/eval_media_expected.json').read_text()) expected.update(('evaluation','ovo/'+v) for v in media['ovo']);expected.update(('evaluation','streaming/'+v) for v in media['streaming']) missing=sorted(expected-present) if missing and not args.allow_partial:raise RuntimeError('指纹覆盖未完整,不能出最终去重结论') common=collections.defaultdict(set) for vid,video in enumerate(videos): for frame in video['fingerprints']: if frame['std']>=cfg['minimum_gray_standard_deviation']:common[int(frame['phash'],16)].add(vid) ignored={h for h,ids in common.items() if len(ids)>cfg['common_hash_distinct_videos_skip_above']} bandpairs=list(itertools.combinations(range(8),2));buckets={};frames=[] for vid,video in enumerate(videos): for frame in video['fingerprints']: h=int(frame['phash'],16) if frame['std']>(8*i))&255 for i in range(8)] for pair,(a,b) in enumerate(bandpairs): key=(pair<<16)|(parts[a]<<8)|parts[b] if key not in buckets:buckets[key]=array('I') buckets[key].append(index) matches={};checked=0 for index,(vid,t,h,dhash,aspect) in enumerate(frames): parts=[(h>>(8*i))&255 for i in range(8)];candidates=set() for pair,(a,b) in enumerate(bandpairs):candidates.update(buckets[(pair<<16)|(parts[a]<<8)|parts[b]]) for other in candidates: if other>=index:continue other_vid,other_t,other_h,other_dhash,other_aspect=frames[other] if other_vid==vid:continue if abs(aspect/other_aspect-1)>cfg['aspect_ratio_relative_tolerance']:continue checked+=1 if (h^other_h).bit_count()>cfg['phash_hamming_cap'] or (dhash^other_dhash).bit_count()>cfg['dhash_hamming_cap']:continue left,right=(other_vid,vid) if other_vid