How to add background music to the voiceover Using python and moviepy

Free Python Code - Aug 2 '23 - - Dev Community

hi every one 🖐
in this post i will show you how to add background music to the voiceover 🙂

step 1

you need to install moviepy

pip install moviepy

step 2

you need any voiceover and background music

if you haven't voiceover you can generate it using the pyttsx3 library for testing

You also get background music
from pixabay

import pyttsx3

def say(text):
    engine = pyttsx3.init()
    engine.setProperty('rate', 160)
    engine.save_to_file(text, 'test.mp3')
    engine.runAndWait()


say('this is text to test 123 and this is number' * 30)

Enter fullscreen mode Exit fullscreen mode

step 3

Now I will merge the audio files using the CompositeAudioClip function

import moviepy.editor as mp

test_voice = mp.AudioFileClip('test.mp3')
music = mp.AudioFileClip('good-night-160166.mp3')

final_audio = mp.CompositeAudioClip([test_voice, music])
final_audio.write_audiofile('out.mp3', fps = 44100)
Enter fullscreen mode Exit fullscreen mode

Thank you all 🙂❤

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .