Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing audio files in MoviePy

I've been writing a script using MoviePy. So far I've been able to import videos, clip them, add text, replace the audio and write a new file. It's been a great learning experience. My question is this:

The movie that I'm editing has audio attached. I'd like to be able to import an audio track and add it to the movie without replacing the original audio. In other words, I'd like to mix the new audio file with the audio that's attached to the video so both can be heard.

Does anyone know how to do this? Thanks in advance!

like image 623
Ghoti Avatar asked Feb 02 '17 00:02

Ghoti


1 Answers

I wrote my own version, but then I found this here:

new_audioclip = CompositeAudioClip([videoclip.audio, audioclip])
videoclip.audio = new_audioclip

So, create a CompositeAudioClip with the audio of the video clip and the new audio clip, then set the old videoclip's audio to the composite audio track.

Full working code:

from moviepy.editor import *
videoclip = VideoFileClip("filename.mp4")
audioclip = AudioFileClip("audioname.mp3")

new_audioclip = CompositeAudioClip([videoclip.audio, audioclip])
videoclip.audio = new_audioclip
videoclip.write_videofile("new_filename.mp4")

If you want to change an individual audioclip's volume, refer to audio.fx.volumex.

Documentation

Source Code

like image 98
Tom Burrows Avatar answered Oct 24 '22 09:10

Tom Burrows