将 Mp3 添加到 VideoFileClip MoviePy 时没有音频 [英] No audio when adding Mp3 to VideoFileClip MoviePy

查看:246
本文介绍了将 Mp3 添加到 VideoFileClip MoviePy 时没有音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 mp3 音频文件添加到我使用 MoviePy 用图像创建的视频剪辑中.当脚本运行时,它会创建 mp4 文件并成功播放,但是没有音频.我不太确定为什么,并且似乎无法找到大量关于此的一般文档.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")

推荐答案

诚然,这个问题很老,但在该问题的搜索结果中排名靠前.我遇到了同样的问题,我认为可以澄清解决方案.

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.

行:

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

实际上不会改变视频"对象的音轨,而是返回一个附加了新的 AudioFileClip 的对象副本.

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.

表示方法:

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

另请参阅文档

这篇关于将 Mp3 添加到 VideoFileClip MoviePy 时没有音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆