import subprocess
import os

url = 'https://123-4.download.real-debrid.com/d/NXRXDHTZXEN2M/The.Early.Spring.2026.S01E02.1080p.NF.RGzsRutracker.mkv'
out_path = '/opt/scrapling-runner/the_early_spring_s01e02_eng.srt'

print('[*] Extraindo faixa #0:5 com ffmpeg direto do stream...')
cmd = [
    'ffmpeg', '-y',
    '-headers', 'User-Agent: Mozilla/5.0\r\n',
    '-i', url,
    '-map', '0:5',
    '-c:s', 'text',
    out_path
]

res = subprocess.run(cmd, capture_output=True, text=True, timeout=40)
print('Retcode:', res.returncode)
if os.path.exists(out_path):
    print('Size:', os.path.getsize(out_path))
    with open(out_path, 'r', errors='ignore') as f:
        print('Primeiras linhas:')
        for _ in range(15):
            print(f.readline(), end='')
else:
    print('Stderr:', res.stderr[-500:])
