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?
On line 3, you have
audio_clip = audio_clip.set_duration(video_clip)
. The docs state:You are using
video_clip
as the parametert
, but you need to use a length. Moviepy video and audio clips have the attribute duration:So, you can use
video_clip.duration
as the parameter for time inaudio_clip.set_duration
which gives the final result on line 3: