| import subprocess
|
| import os
|
|
|
| def download_clips(stream_url, out_dir, start_time, end_time, resize=True):
|
|
|
| if len(os.listdir(out_dir)) > 5:
|
| for f in os.listdir(out_dir):
|
| if f.endswith('.mp4'):
|
| os.remove(os.path.join(out_dir, f))
|
| output_file = os.path.join(out_dir, f"train_{len(os.listdir(out_dir))}.mp4")
|
| if resize:
|
| ffmpeg_cmd = [
|
| 'ffmpeg',
|
| '-i', stream_url,
|
| '-c:v', 'libx264',
|
| '-crf', '23',
|
| '-r', '30',
|
| '-maxrate', '2M',
|
| '-bufsize', '4M',
|
| '-vf', f"scale=-2:300",
|
| '-ss', start_time] + (['-to', end_time] if end_time != '' else []) + [
|
| '-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
|
|
|
|
|
| print(f"Converted {output_file}")
|
|
|
| return output_file |