Anay Bose
Anay Bose

Reputation: 890

Compositing two soundtracks with moviepy

I am trying to add an audio to a clip. Problem is it's replacing the original soundtrack with the new one i.e. pond5.wav. I first concat some small clips, each of these clips contain original soundtrack. The end result doesn't contain the original soundtracks, however. I need both original and new soundtracks (the new sound track's volume must be lower than the original). I need clues how to modify the following code and composite two soundtracks.

media = "promo"

clips = [VideoFileClip(clip) 
         for clip in get_filepaths(media) 
         if ntpath.basename(clip).startswith('clip_')
         and ntpath.basename(clip).endswith('.mp4')
         ] 

vclip = concatenate_videoclips([clips[0],
                                clips[1].crossfadein(0.3),
                                clips[2].crossfadein(0.3),
                                clips[3].crossfadein(0.3),
                                ], 
                               method="compose", 
                               padding=-0.3)

audio = AudioFileClip(os.path.join(folder,"pond5.wav"))
audio = audio.audio_loop(duration=vclip.duration)
cc = vclip.set_audio(audio
                     .set_duration(vclip.duration)
                     .volumex(0.3)
                     .audio_fadein(1.0)
                     .audio_fadeout(1.0)
                     )
cc.write_videofile(os.path.join(folder, "demo.mp4"), fps=60, codec="libx264", bitrate="20000k", threads=6)

Upvotes: 2

Views: 1289

Answers (1)

blackbox
blackbox

Reputation: 671

You need to create a composite audio clip from vclip's audio and set this as the audio. Something like (did not test) :

    composite = CompositeAudioClip(clicp.audio + [audio])
    # set audio returns a new instance...
    final = vclip.set_audio(composite)
    final.writevideofile(...)

Upvotes: 1

Related Questions