Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Make a clip as long as a sound using moviepy

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?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading