2016-12-22 2 views
1

Я пытаюсь добавить аудио в видеоклип. Я хочу аудио быть обрезано на основе длительности видеоклипа:TypeError при использовании set_duration

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) 

Однако я получаю эту ошибку:

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

Он работает без set_duration вызова: видео оказывается, что замерзает последний кадр video_clip и audio_clip продолжается до тех пор, пока видео не закончится.

Любые идеи о том, что может вызвать эту ошибку?

+1

Вы кажетесь проездом видеоклип к функции '' set_duration'' ... вы имели в виду передать int или '' video_clip.duration''? – Shadow

ответ

0

В строке 3 у вас есть audio_clip = audio_clip.set_duration(video_clip). docs состояние:

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.

Вы используете video_clip в качестве параметра t, но вы должны использовать длину. Moviepy видео и аудио клипы имеют длительность атрибута:

duration: Duration of the clip (in seconds).

Таким образом, вы можете использовать video_clip.duration в качестве параметра времени в audio_clip.set_duration, который дает окончательный результат в строке 3:

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