Spaces:
Sleeping
Sleeping
| 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") | |
| ffmpeg_cmd = [ | |
| 'ffmpeg', | |
| '-f', 'hls', | |
| '-i', stream_url, | |
| '-ss', str(max(0, start_time - 2)), # Ensure start time is not negative | |
| '-to', str(end_time + 2), | |
| '-r', '30', | |
| '-c:v', 'libx264', | |
| '-preset', 'ultrafast', # Use fastest encoding preset | |
| '-crf', '23', # Slightly lower CRF for better quality | |
| '-maxrate', '1M', # Limit bitrate | |
| '-bufsize', '2M', | |
| '-vf', f"scale=-2:{360 if resize else 720}", | |
| '-c:a', 'aac', # Re-encode audio for better compatibility | |
| '-b:a', '128k', | |
| '-max_muxing_queue_size', '1024', # Increase muxing queue size | |
| '-y', # Overwrite output file if it exists | |
| output_file | |
| ] | |
| print([f'{x} ' for x in ffmpeg_cmd]) | |
| try: | |
| subprocess.run(ffmpeg_cmd, check=True, capture_output=True, text=True) | |
| return output_file | |
| except subprocess.CalledProcessError as e: | |
| print(f"Error occurred: {e}") | |
| print(f"FFmpeg output: {e.output}") | |
| return None |