9

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")
damaniel
  • 269
  • 2
  • 3
  • 10

7 Answers7

25

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

racket99
  • 635
  • 8
  • 17
16

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

Gustav
  • 477
  • 4
  • 8
  • I'm not sure how this is any different then what I posed in my question. I think the answer might be that both files need to be the same length. That's what worked for me anyway, not what you're suggesting above. – damaniel Jan 09 '19 at 16:25
6

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

  • VLC Player plays it without audio as well. – damaniel Nov 19 '16 at 06:57
  • have same problem with QuickTime on OSX 10.15.7, but there is audio on VLC. Look like mp4 is not able to play mp3 audio, because when I set audio_codec='aac' in write_videofile, than it is fine. Would be nice to have some error/warning. – WebOrCode Apr 09 '23 at 14:42
1

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

video = video.set_audio(AudioFileClip("audio.mp3"))
MrMagix
  • 116
  • 1
  • 4
  • I've tried this before and I just did again as a sanity check. Same issue, no audio. I'm starting to think that adding audio like this is not as straightforward as the moviepy doc claims. I'm guessing the file might need to be formatted to a wav or something, codecs have to be changed, time has to be exactly the same etc..all of those differences with the generated video I have is probably prevently the audio from being set correctly. I'm really new to A/V tech like this; it's really uncharted territory for me. Was hoping this would be a simple script. – damaniel Nov 22 '16 at 02:47
  • And I just started coding in Python one month ago... :) – MrMagix Nov 24 '16 at 13:26
  • This is working code. It's a bit untidy and not well structured but this is how I got moviepy to work. Have a look in line 51 and 93. https://github.com/JSchwehn/FeedToYoutube/blob/master/videoCreator.py – MrMagix Nov 24 '16 at 13:33
  • I actually got it to work after looking at your code. I'm not really sure what it was, but once I started chopping the audio file to match the length of the video everything started to work correctly. Thanks for your help! – damaniel Nov 26 '16 at 15:25
1

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)
filipmu
  • 66
  • 3
0

Use this:

video.write_videofile("output.mp4", fps=30, audio_codec="aac", audio_bitrate="192k")
0
video_clip.audio = AudioFileClip(audio_path)

video_clip.write_videofile("output_video.mp4")

This worked for me

Dev A
  • 1
  • 1