Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infinite audio loop #33

Closed
nicopace opened this issue Apr 15, 2014 · 7 comments
Closed

infinite audio loop #33

nicopace opened this issue Apr 15, 2014 · 7 comments

Comments

@nicopace
Copy link

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

@Zulko
Copy link
Owner

Zulko commented Apr 16, 2014

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

@nicopace
Copy link
Author

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

@Zulko
Copy link
Owner

Zulko commented Apr 16, 2014

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
#33 (comment).

@nicopace
Copy link
Author

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

@Zulko
Copy link
Owner

Zulko commented Jun 26, 2014

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')

@Zulko
Copy link
Owner

Zulko commented Jun 26, 2014

... 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)

@nicopace
Copy link
Author

Great, thanks!

@Zulko Zulko closed this as completed Jun 27, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants