Search code examples
pythonffmpegmoviepy

How to merge mp3 and mp4 in python / Mute a mp4 file and add a mp3 file on it


I want to merge an mp3 and an mp4 in python, if possible with the moviepy library.

But my mp4 video got some sound in it, so i want to delete this sound before.


Solution

  • I got a lot of difficulties to find something that work, so i think a lot of people could have this difficulties too, so here is the code that worked for me :

    def combine_audio(vidname, audname, outname, fps=60): 
        import moviepy.editor as mpe
        my_clip = mpe.VideoFileClip(vidname)
        audio_background = mpe.AudioFileClip(audname)
        final_clip = my_clip.set_audio(audio_background)
        final_clip.write_videofile(outname,fps=fps)
    
    combine_audio("test.mp4", "test.mp3", "test_over.mp4") # i create a new file
    combine_audio("test.mp4", "test.mp3", "test.mp4") # i rewrite on the same file```