为什么我在python moviepy中使用CompositeAudioClip时没有声音?

时间:2018-03-30 03:43:21

标签: python audio video moviepy

这是python脚本。

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")

然后返回

  

[MoviePy]>>>>构建视频new_filename.mp4 [MoviePy]编写音频   在new_filenameTEMP_MPY_wvf_snd.mp3中   100%|████████████████████████████████████████| 795/795 [00:01< 00:00,   466.23it / s] [MoviePy]完成。 [MoviePy]写视频new_filename.mp4 100%|███████████████████████████████████████ | 1072/1072 [01:26< 00:00,   10.31it / s] [MoviePy]完成。 [MoviePy]>>>>视频就绪:new_filename.mp4

1_0_1522314608.m4v和jam_1_Mix.mp3他们都有声音。

但是新文件new_filename.mp4没有声音。

我做错了吗?请帮忙。谢谢。

1 个答案:

答案 0 :(得分:0)

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)