Single-Rope-Contest / hls_download.py
dylanplummer's picture
fewer pixels
34410cd
raw
history blame
1.53 kB
import subprocess
import os
def download_clips(stream_url, out_dir, start_time, end_time, resize=True):
output_file = os.path.join(out_dir, f"train_{len(os.listdir(out_dir))}.mp4")
tmp_file = os.path.join(out_dir, f"train_{len(os.listdir(out_dir))}_tmp.mp4")
yt_dlp_cmd = [
'yt-dlp',
'--download-sections', f'*{start_time}-{end_time}',
stream_url,
'-o', tmp_file
]
print(' '.join(yt_dlp_cmd))
try:
subprocess.run(yt_dlp_cmd, check=True, capture_output=True, text=True)
except subprocess.CalledProcessError as e:
print(f"Error occurred: {e}")
print(f"yt-dlp output: {e.output}")
return None
if resize: # resize and convert to 30 fps
ffmpeg_cmd = [
'ffmpeg',
'-i', tmp_file,
'-c:v', 'libx264',
'-preset', 'veryfast',
'-crf', '23',
'-r', '30',
'-maxrate', '2M',
'-bufsize', '4M',
'-vf', f"scale=-2:320",
'-c:a', 'aac',
'-b:a', '128k',
'-y',
output_file
]
try:
subprocess.run(ffmpeg_cmd, check=True, capture_output=True, text=True)
except subprocess.CalledProcessError as e:
print(f"Error occurred: {e}")
print(f"ffmpeg output: {e.output}")
return None
else:
os.rename(tmp_file, output_file)
return output_file