Video editing with python : adding a background music to a video with sound
Asked Answered
F

3

12

I would like to add a background music to a video file (.mp4) in python.

I have looked the web and did some tricks with moviepy for python, but I did not found a single way to add background music to a video file that already contains music. Any ideas how to do that ?

Edit following Anil_M's comment : Thanks, no I didn't look at this particular thread, althought I knew about ffmpeg but it looks like again, it is a way to merge a video without audio with an audio track.

So now I am going to try to extract audio from the video, merge with another audio file, then merge back with video. Maybe that's not the best way, but at least it is possile as questions about merging 2 audio tracks are answered.

Fasciate answered 11/2, 2018 at 4:2 Comment(1)
Have you looked at this thread ? #28219549Pleurodynia
A
15

You can do this with moviepy's CompositeAudioClip:

import moviepy.editor as mpe
my_clip = mpe.VideoFileClip('some_clip.mp4')
audio_background = mpe.AudioFileClip('some_background.mp3')
final_audio = mpe.CompositeAudioClip([my_clip.audio, audio_background])
final_clip = my_clip.set_audio(final_audio)
Anticlastic answered 19/2, 2018 at 12:50 Comment(1)
Can i use it with Django ?Louannlouanna
B
4

This code works on windows

import moviepy.editor as mp

audio = mp.AudioFileClip("theme.mp3")
video1 = mp.VideoFileClip(video_no_audio)
final = video1.set_audio(audio)
final.write_videofile("output/output.mp4",codec= 'mpeg4' ,audio_codec='libvorbis')
Boltrope answered 17/11, 2020 at 5:48 Comment(0)
G
2

#This code is working Effectively.

import moviepy.editor as mp

audio = mp.AudioFileClip("ck3.mp3")
video1 = mp.VideoFileClip("ck1.mp4")
final = video1.set_audio(audio)

final.write_videofile("output.mp4")
Griqua answered 27/1, 2022 at 13:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.