damaniel
damaniel

Reputation: 299

No audio when adding Mp3 to VideoFileClip MoviePy

I'm trying to add an mp3 audio file to a video clip that I'm creating out of images with MoviePy. When the script runs it creates the mp4 file and plays successfully, however there's no audio. I'm not really sure why and can't seem to find a ton of documentation around this in general. MoviePy is pretty new to me so any help would be appreciated - thank-you!

def make_video(images):
    image_clips = []
    for img in images:
        if not os.path.exists(img):
            raise FileNotFoundError(img)
        ic = ImageClip(img).set_duration(3)
        image_clips.append(ic)

    video = concatenate(image_clips, method="compose")
    video.set_audio(AudioFileClip("audio.mp3")) 
    video.write_videofile("mp4_with_audio.mp4", fps=60, codec="mpeg4")

Upvotes: 12

Views: 14047

Answers (7)

Dev A
Dev A

Reputation: 1

video_clip.audio = AudioFileClip(audio_path)

video_clip.write_videofile("output_video.mp4")

This worked for me

Upvotes: 0

Chirag Awale
Chirag Awale

Reputation: 66

Use this:

video.write_videofile("output.mp4", fps=30, audio_codec="aac", audio_bitrate="192k")

Upvotes: 1

filipmu
filipmu

Reputation: 66

I was doing something similar and found that moviepy 1.0.1 did not call ffmpeg with the right arguments to combine the video and audio for mp4 video. I solved this through a workaround using ffmpeg directly. It uses the temp audio file and video file from moviepy to create a final file. This is a similar question: Output video has no sound Since you are working with mp3, you may need to have ffmpeg convert to aac, so this code does that.

This link helped me with ffmpeg:https://superuser.com/questions/277642/how-to-merge-audio-and-video-file-in-ffmpeg

video_with_new_audio = video.set_audio(AudioFileClip("audio.mp3")) 
video_with_new_audio.write_videofile("temp_moviepy.mp4", temp_audiofile="tempaudio.m4a",codec="libx264",remove_temp=False,audio_codec='aac')

import subprocess as sp

command = ['ffmpeg',
           '-y', #approve output file overwite
           '-i', "temp_moviepy.mp4",
           '-i', "tempaudio.m4a",
           '-c:v', 'copy',
           '-c:a', 'aac', #to convert mp3 to aac
           '-shortest', 
           "mp4_with_audio.mp4" ]

with open(ffmpeg_log, 'w') as f:
    process = sp.Popen(command, stderr=f)

Upvotes: 1

racket99
racket99

Reputation: 655

This worked for me:

clip.write_videofile(out_path, 
                     codec='libx264', 
                     audio_codec='aac', 
                     temp_audiofile='temp-audio.m4a', 
                     remove_temp=True
                     )

Found it here: https://github.com/Zulko/moviepy/issues/51

Upvotes: 26

Gustav
Gustav

Reputation: 537

Admittedly, this question is old but comes high in search results for the problem. I had the same issue and think the solution can be clarified.

The line:

video.set_audio(AudioFileClip("audio.mp3")) 

actually does not change the audio track of the "video" object, but returns a copy of the object with the new AudioFileClip attached to it.

That means that the method:

video.write_videofile("mp4_with_audio.mp4", fps=60, codec="mpeg4")

does not write the final file with the new audio track, since the "video" object remains unchanged.

Changing the script as per the below solved the issue for me.

video_with_new_audio = video.set_audio(AudioFileClip("audio.mp3")) 
video_with_new_audio.write_videofile("mp4_with_audio.mp4", fps=60, codec="mpeg4")

See also the docs

Upvotes: 18

MrMagix
MrMagix

Reputation: 116

I run into this problem too. I found a solution, try

video = video.set_audio(AudioFileClip("audio.mp3"))

Upvotes: 1

danishshres
danishshres

Reputation: 61

Check the video mp4_with_audio.mp4 with VLC media player, i also have same issue with quick player.

Upvotes: 6

Related Questions