TypeError when using set_duration

482 views Asked by At

I'm trying to add audio to a video clip. I want the audio to be cropped based on the duration of the video clip:

video_clip = mpy.VideoClip(make_counter, duration=12)
audio_clip = mpy.AudioFileClip("audio/ticking.mp3")
audio_clip = audio_clip.set_duration(video_clip) # <= Set the duration of the audio to the same as the video
video_file = "video_test.mp4"
video_clip = video_clip.set_audio(audio_clip)
video_clip.write_videofile(video_file, fps=24)

However I get this error:

TypeError: unsupported operand type(s) for +: 'int' and 'instance'

It works without the set_duration call: a video is rendered that freezes at the last frame of video_clip and audio_clip continues until the rendered video is over.

Any ideas on what could be causing this error?

1

There are 1 answers

0
Tom Burrows On

On line 3, you have audio_clip = audio_clip.set_duration(video_clip). The docs state:

set_duration(t, change_end=True):

Returns a copy of the clip, with the duration attribute set to t, which can be expressed in seconds (15.35), in (min, sec), in (hour, min, sec), or as a string: ‘01:03:05.35’. Also sets the duration of the mask and audio, if any, of the returned clip.

You are using video_clip as the parameter t, but you need to use a length. Moviepy video and audio clips have the attribute duration:

duration: Duration of the clip (in seconds).

So, you can use video_clip.duration as the parameter for time in audio_clip.set_duration which gives the final result on line 3:

audio_clip = audio_clip.set_duration(video_clip.duration) # <= Set the duration of the audio to the same as the video