import hashlib
import json
import os
import subprocess
from pathlib import Path

root=Path(__file__).resolve().parents[1]
os.umask(0o077)
path=root/'shared/event_sample/clip.mp4'
raw=subprocess.run(['ffprobe','-v','error','-select_streams','v:0','-show_entries',
                    'packet=pts,dts,pts_time,dts_time,duration_time:stream=time_base,width,height,nb_frames:format=duration',
                    '-of','json',str(path)],capture_output=True,text=True,check=True)
probe=json.loads(raw.stdout)
packets=probe['packets']
assert len(packets)==60
assert all(b['pts']>a['pts'] and b['dts']>a['dts'] for a,b in zip(packets,packets[1:]))
decoded=subprocess.run(['ffmpeg','-hide_banner','-loglevel','error','-nostdin','-i',str(path),
                       '-fps_mode','passthrough','-enc_time_base','-1','-f','null','-'],capture_output=True,text=True)
assert decoded.returncode==0 and not decoded.stderr.strip(), decoded.stderr
data={'packets':60,'pts_strictly_increasing':True,'dts_strictly_increasing':True,
      'full_decode_with_source_timebase':'pass','width':probe['streams'][0]['width'],
      'height':probe['streams'][0]['height'],'duration':probe['format']['duration'],
      'first_null_mux_check_note':'A default output timebase produced duplicate-DTS rounding warnings. Original packet PTS/DTS were increasing; source-timebase passthrough decode produced no errors.',
      'source_sha256':hashlib.sha256(path.read_bytes()).hexdigest()}
(root/'shared/media_verification.json').write_text(json.dumps(data,indent=2)+'\n')
(root/'private/clip_packets.json').write_text(raw.stdout)
print(json.dumps(data))
