| 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
|
| print(f"Downloaded {output_file}")
|
| if resize:
|
| ffmpeg_cmd = [
|
| 'ffmpeg',
|
| '-i', tmp_file,
|
| '-c:v', 'libx264',
|
| '-preset', 'veryfast',
|
| '-crf', '23',
|
| '-r', '30',
|
| '-maxrate', '2M',
|
| '-bufsize', '4M',
|
| '-vf', f"scale=-2:300",
|
| '-c:a', 'aac',
|
| '-b:a', '128k',
|
| '-y',
|
| output_file
|
| ]
|
| print(' '.join(ffmpeg_cmd))
|
| 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)
|
| print(f"Converted {output_file}")
|
|
|
| return output_file
|
|
|