user3751111
user3751111

Reputation: 83

why no sound when I use CompositeAudioClip in python moviepy?

here is the python script.

from moviepy.editor import *
videoclip = VideoFileClip("1_0_1522314608.m4v")
audioclip = AudioFileClip("jam_1_Mix.mp3")

new_audioclip = CompositeAudioClip([videoclip.audio, audioclip])
videoclip.audio = new_audioclip
videoclip.write_videofile("new_filename.mp4")

then returned

[MoviePy] >>>> Building video new_filename.mp4 [MoviePy] Writing audio in new_filenameTEMP_MPY_wvf_snd.mp3 100%|████████████████████████████████████████| 795/795 [00:01<00:00, 466.23it/s] [MoviePy] Done. [MoviePy] Writing video new_filename.mp4 100%|███████████████████████████████████████| 1072/1072 [01:26<00:00, 10.31it/s] [MoviePy] Done. [MoviePy] >>>> Video ready: new_filename.mp4

1_0_1522314608.m4v and jam_1_Mix.mp3 they both have sound.

but the new file new_filename.mp4 no sound.

did I do something wrong? please help. thank you.

Upvotes: 5

Views: 2049

Answers (1)

blackbox
blackbox

Reputation: 671

I had similar issues. I found that sometimes the audio is in fact present, but will not be played by all players. (Quicktime does not work, but VLC does). I finally use something like :

    video_clip.set_audio(composite_audio).write_videofile(
        composite_file_path,
        fps=None,
        codec="libx264",
        audio_codec="aac",
        bitrate=None,
        audio=True,
        audio_fps=44100,
        preset='medium',
        audio_nbytes=4,
        audio_bitrate=None,
        audio_bufsize=2000,
        # temp_audiofile="/tmp/temp.m4a",
        # remove_temp=False,
        # write_logfile=True,
        rewrite_audio=True,
        verbose=True,
        threads=None,
        ffmpeg_params=None,
        progress_bar=True)     

A few remarks :

  • aac seems to work better for quicktime than mp3.
  • logfile file generation helps to diagnose what is happening behind the scenes
  • temp_audiofile can be tested on its own
  • set_audio() returns a new clip and leave the object on which it is called unchanged
  • Setting the audio clip duration to match the video helps :

    composite_audio = composite_audio.subclip(0, video_clip.duration)
    composite_audio.set_duration(video_clip.duration)
    

Upvotes: 1

Related Questions