To avoid using external dependencies like ffmpeg, I'm using MoviePy to generate and edit videos. However, I'm not sure how to concat efficiently a video from thousands of downloaded video fragments. At this moment, I use this code:
from moviepy.editor import VideoFileClip, concatenate_videoclips
from mysutils.tmp import removable_tmp
from mysutils.file import remove_files, list_dir
def concat_videos(output_file: str, folder: str, chunk: int = CHUNK) -> None:
files = list_dir(folder)
if len(files) == 1:
move(files[0], output_file)
elif files:
with removable_tmp(True) as tmp_dir:
max_digit_length = f'{{:0{len(str(len(files)))}d}}.mp4'
for i in range(0, len(files), chunk):
videos = [files[j] for j in range(i, min([len(files), i + chunk]))]
clips = [VideoFileClip(f) for f in videos]
final_clip = concatenate_videoclips(clips)
file = join(tmp_dir, max_digit_length.format(i // chunk + 1))
final_clip.to_videofile(file, fps=24, remove_temp=True)
for clip in clips:
clip.close()
final_clip.close()
remove_files(*videos)
concat_videos(output_file, tmp_dir, chunk)
The problems of this approach are: 1) I need to download all the video fragments first and store them in a temporal folder, instead of processing them directly; 2) it is very slowly (although it depends on the chunk size I'm using); and 3) with chunk size too high, it requires a lot of memory. I would like an approach to generate a video as I download it, even using the input streaming.
Thanks.