Moviepy: infinite audio loop

Created on 15 Apr 2014  Â·  7Comments  Â·  Source: Zulko/moviepy

It would be great to have a way to specify that a specific audio file has to loop all over the duration of the video clip

Most helpful comment

... And if you don't want to reinstall, here is a minimal code:

from moviepy.audio.AudioClip import CompositeAudioClip
import numpy as np

def audio_concatenate(clips):
    durations = [c.duration for c in clips]
    tt = np.cumsum([0]+durations) # start times, and end time.
    newclips= [c.set_start(t) for c,t in zip(clips, tt)]
    return CompositeAudioClip(newclips).set_duration(tt[-1])

def audio_loop(clip, duration):
    nloops = int( duration/ clip.duration)+1
    return audio_concatenate(nloops*[clip]).set_duration(duration)

All 7 comments

Would something like this suit your needs ? (that would be the simplest for me):

video = VideoFileClip("myvideo.mp4")
audio = AudioFileClip("myaudio.mp3").loop(duration= video.duration)
video.audio = audio

Yeah, that was what i was thinking to do, but didn't found it in the spec, so great!

It's not there yet ! It will be very soon.

Le 16/04/2014 17:55, nicopace a écrit :

Yeah, that was what i was thinking to do, but didn't found it in the
spec, so great!

—
Reply to this email directly or view it on GitHub
https://github.com/Zulko/moviepy/issues/33#issuecomment-40616651.

I know, just saying that it felt natural to me to put it there :)

In case you are still interested, it's done. Now you can write:

from moviepy.editor import (VideoFileClip, AudioFileClip)
videoclip = VideoFileClip("movie.mov")
soundtrack = AudioFileClip("./music.mp3")
videoclip.audio = soundtrack.audio_loop(duration=videoclip.duration)
videoclip.to_videofile('movie_with_music.mp4')

... And if you don't want to reinstall, here is a minimal code:

from moviepy.audio.AudioClip import CompositeAudioClip
import numpy as np

def audio_concatenate(clips):
    durations = [c.duration for c in clips]
    tt = np.cumsum([0]+durations) # start times, and end time.
    newclips= [c.set_start(t) for c,t in zip(clips, tt)]
    return CompositeAudioClip(newclips).set_duration(tt[-1])

def audio_loop(clip, duration):
    nloops = int( duration/ clip.duration)+1
    return audio_concatenate(nloops*[clip]).set_duration(duration)

Great, thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Netherdrake picture Netherdrake  Â·  4Comments

Netherdrake picture Netherdrake  Â·  5Comments

djevo1 picture djevo1  Â·  3Comments

cquintini picture cquintini  Â·  4Comments

arianaa30 picture arianaa30  Â·  4Comments