Make a clip as long as a sound using moviepy
Question:
My clip is 14 minutes long. The sound sometimes can last up to an hour or more. I don’t want to make the clip an hour long for example. When I have to make that clip.set_duration(audio.duration)
it’s easy, but how can I make that when the original clip ends, it loops and goes on?
Answers:
If you want to loop the audio to match the duration of the video clip can use below code
from pydub import AudioSegment
audio = AudioSegment.from_file("audio.mp3", format="mp3")
audio_length = len(audio)
desired_length = 14 * 60 * 1000
num_loops = desired_length // audio_length + 1
looped_audio = audio * num_loops
looped_audio.export("looped_audio.mp3", format="mp3")
The same idea can be applied to loop the video so that it lasts as long as the audio
from moviepy.video.io.VideoFileClip import VideoFileClip
video = VideoFileClip("video.mp4")
video_duration = video.duration
audio_duration = 61
num_loops = int(audio_duration / video_duration) + 1
looped_video = video.repeat(num_loops)
looped_video.write_videofile("looped_video.mp4")
My clip is 14 minutes long. The sound sometimes can last up to an hour or more. I don’t want to make the clip an hour long for example. When I have to make that clip.set_duration(audio.duration)
it’s easy, but how can I make that when the original clip ends, it loops and goes on?
If you want to loop the audio to match the duration of the video clip can use below code
from pydub import AudioSegment
audio = AudioSegment.from_file("audio.mp3", format="mp3")
audio_length = len(audio)
desired_length = 14 * 60 * 1000
num_loops = desired_length // audio_length + 1
looped_audio = audio * num_loops
looped_audio.export("looped_audio.mp3", format="mp3")
The same idea can be applied to loop the video so that it lasts as long as the audio
from moviepy.video.io.VideoFileClip import VideoFileClip
video = VideoFileClip("video.mp4")
video_duration = video.duration
audio_duration = 61
num_loops = int(audio_duration / video_duration) + 1
looped_video = video.repeat(num_loops)
looped_video.write_videofile("looped_video.mp4")