File size: 1,491 Bytes
3908691 ed4eebf 3908691 ed4eebf 3908691 ed4eebf 3908691 ae49a1b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import subprocess
import os
def download_clips(stream_url, out_dir, start_time, end_time, resize=True, use_60fps=False, use_cuda=True):
# remove all .mp4 files in out_dir to avoid confusion
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: # resize and convert to 30 fps
ffmpeg_cmd = [
'ffmpeg',
'-hwaccel', 'cuda' if use_cuda else 'none',
'-i', stream_url,
#'-c:v', 'libx264',
'-crf', '23',
'-r', '30' if not use_60fps else '60',
'-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 output_file
# else:
# os.rename(tmp_file, output_file)
print(f"Converted {output_file}")
return output_file |