pythonvideomovieclipmoviepy

Adding audio to the clip


I have the following code and receiving no sound in the video output. I'm using MoviePy package.

from moviepy.editor import *  

picture = VideoFileClip("/Users/name/Desktop/trial.jpg", audio=False).set_duration(50)

txt_clip = TextClip("Hey",fontsize = 150, color='white')
txt_clip = txt_clip.set_pos('center').set_duration(10)

audio = AudioFileClip("/Users/name/Desktop/music.mp3").subclip(0,50)

video_with_new_audio = picture.set_audio(audio)

final_video = CompositeVideoClip([video_with_new_audio,txt_clip])

final_video.write_videofile("trial.mp4")

What can be the issue?:c


Solution

  • Could it be a missing MP3 codec, "LAME" etc.? Your code worked on Windows:

    from moviepy.editor import *  
    
    picture = VideoFileClip("img.jpg", audio=False).set_duration(20)
    
    txt_clip = TextClip("Hey",fontsize = 150, color='white')
    txt_clip = txt_clip.set_pos('center').set_duration(10)
    
    audio = AudioFileClip(r"C:\Users\Public\Music\Sample Music\Kalimba.mp3").subclip(0,20)
    
    video_with_new_audio = picture.set_audio(audio)
    
    final_video = CompositeVideoClip([video_with_new_audio,txt_clip])
    
    final_video.write_videofile("trial.mp4")
    

    That's the output: trial.mp4

    Can you run ffprobe trial.mp4 or ffmpeg -i trial.mp4 and show the output?

    That's the output of ffprobe on my video:

    ffprobe z:\trial.mp4 
    (...)
        Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'z:\trial.mp4':
          Metadata:
            major_brand     : isom
            minor_version   : 512
            compatible_brands: isomiso2avc1mp41
            encoder         : Lavf58.29.100
          Duration: 00:00:20.04, start: 0.000000, bitrate: 236 kb/s
            Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 822x556,
        100 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
            Metadata:
              handler_name    : VideoHandler
            Stream #0:1(und): Audio: mp3 (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 12
        7 kb/s (default)
            Metadata:
              handler_name    : SoundHandler
    

    That may be related, ffmpeg settings and version of moviepy: No audio on concatenating clips in MoviePy