import json, subprocess, os
from pathlib import Path

EP = Path("/home/kai/runs/ep-cat-worker-001")
OUT = Path("/home/kai/shared/qwen-eye-exp/frames")
shots = json.load(open(EP/'.pipeline-assets/shot-list.json'))['value']

for s in shots:
    sid = s['shot_id']
    # P11 文件名 S1-01.mp4 vs shot_id S1-01
    for cand in [EP/f'assets/P11/{sid}.mp4', EP/f'assets/P11/{sid.replace("-","_SH")}.mp4']:
        if cand.exists(): vid = cand; break
    else:
        print(f'MISS {sid}'); continue
    d = OUT/sid; d.mkdir(exist_ok=True)
    # 4 帧均匀抽取: 10%, 37%, 63%, 90% + 首帧
    for tag, pct in [('f0',0.02),('f1',0.3),('f2',0.55),('f3',0.8),('f4',0.97)]:
        # 用 fps 过滤不可靠(时长未知), 先 probe
        pass
    dur = float(subprocess.check_output(['ffprobe','-v','quiet','-show_entries','format=duration','-of','csv=p=0',str(vid)]).decode().strip())
    for tag, pct in [('f0',0.03),('f1',0.3),('f2',0.55),('f3',0.8),('f4',0.95)]:
        t = dur*pct
        subprocess.run(['ffmpeg','-y','-loglevel','error','-ss',str(t),'-i',str(vid),
            '-frames:v','1','-vf','scale=800:-2','-q:v','7',str(d/f'{tag}.jpg')],
            check=True, timeout=60)
    print(f'OK {sid} dur={dur:.1f}s')
