"""Inspect actual downloaded stems. Does not generate or separate audio."""
import argparse,json,subprocess,hashlib
from pathlib import Path
p=argparse.ArgumentParser();p.add_argument('stems',nargs='+');args=p.parse_args()
rows=[]
for name in args.stems:
 path=Path(name)
 meta=json.loads(subprocess.check_output(['ffprobe','-v','error','-show_entries','stream=codec_name,sample_rate,channels,start_time,duration:format=duration','-of','json',str(path)],text=True))
 stream=next(x for x in meta['streams'] if x.get('sample_rate'))
 rows.append({'file':path.name,'bytes':path.stat().st_size,'sha256':hashlib.sha256(path.read_bytes()).hexdigest(),'duration':float(stream.get('duration',meta['format']['duration'])),'sampleRate':int(stream['sample_rate']),'channels':stream['channels'],'startTime':float(stream.get('start_time','0')),'codec':stream['codec_name']})
report={'actualDownloadedFiles':rows,'sameDurationWithinOneSample':max(r['duration'] for r in rows)-min(r['duration'] for r in rows)<=1/min(r['sampleRate'] for r in rows),'sameSampleRate':len({r['sampleRate'] for r in rows})==1,'sameStartTime':len({r['startTime'] for r in rows})==1,'limits':['Matching container start times does not prove sample-accurate musical alignment; audition all stems from the same timeline origin.','No inference performed. No isolated-stem quality, leakage, lyric or copyright claims made.']}
Path('actual-stems-inspection.json').write_text(json.dumps(report,ensure_ascii=False,indent=2));print(json.dumps(report,ensure_ascii=False,indent=2))
