9

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!

0

1 Answer 1

14

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

4
  • That did the trick! Thanks. I had to be sure to include 'import moviepy.editor as mpe'. Also 'mpe.VideoFileClip("filename.mp4")' and 'mpe.AudioFileClip("audioname.mp3")' but once I made those changes, it worked like a charm. Thank you!
    – Ghoti
    Commented Feb 4, 2017 at 4:35
  • @Ghoti Oh, sorry, I didn't check the imports. Fixed now Commented Feb 5, 2017 at 17:43
  • I usually just import all (*), but the code I found obviously had imported as mpe. Well spotted. Commented Feb 5, 2017 at 17:44
  • Here there is an issue... If the audio duration is more than the video clip, Then the video clip duration is set to the overall duration and writing the file... Can anyone fix that for me... I tried adjusting the duration of audioClip before compositing the audioClip, But that didn't worked for me Commented Aug 4, 2021 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.