FFFMPEG-Python write filters to file then use them

27 Views Asked by At

So I am trying to do a slide animation between a lot of files using FFMpeg Python.

for image_path, audio_path in zip(images_path, audios_path):
        audio_dur =  AudioSegment.from_file(audio_path).duration_seconds
        
        audio = ffmpeg.input(audio_path, t=audio_dur)

        image_width, image_height = get_dim(image_path)
        xscale = video_width*scale
        yscale = image_height*xscale/image_width
        image = ffmpeg.input(image_path)
        image = image.filter('scale', width=xscale, height=yscale)

        x = int(video_width / 2 - xscale / 2)


        if slide and previous_image_yscale < yscale:
            frame_dur = min(audio_dur, max_frames_dur)

            diff = (yscale - previous_image_yscale) / frames
            dpf = frame_dur/frames

            image_split = image.split()

            end_t = duration+audio_dur

            for i in range(frames):


                image_i = image_split[i].filter('crop', xscale, previous_image_yscale+diff*i, 0, 0)
                video = video.overlay(image_i, enable=f"between(t, {duration},{duration+dpf})", x=x, y=put_y)
                duration += dpf


            video = video.overlay(image_split[i+1], enable=f"between(t, {duration},{end_t})", x=x, y=put_y)

            duration = end_t

As you can see, it will do a lot of edits to the video. But when I want to run the command with output.run(overwrite_output = True), since there are so much filters to apply, it will reach the Maximum Command Line characters limit and it won't work.

So I tried to write the filters to a file, then I want to use the file.

cmd = output.get_args()

filters = cmd[cmd.index('-filter_complex')+1]

with open('filters.txt', mode='w') as f:
    f.write(filters)

This should work right? But I don't know what to do after with that.

Maybe I could write the filters to a file directly when I add the overlay and the crop? It would be easier? But again I don't know how to do that

Any help would be appreciated, thank you!

0

There are 0 best solutions below