简体   繁体   English

在 python 中结合 mp4 和 wav

[英]Combining mp4 with wav in python

I am trying to combine a.mp4 file with a.wav file.我正在尝试将 a.mp4 文件与 a.wav 文件结合起来。 I am rendering my mp4 with cv2 videowriter, and I don't think it has anyway of incorporating audio with it.我正在用 cv2 videowriter 渲染我的 mp4,但我认为它无论如何都没有与它合并音频。 I have tried moviepy.editor, and ffmpeg.我试过moviepy.editor和ffmpeg。 moviepy.editor kept messing up the video file and ffmpeg repeatedly kept giving me an error that it couldn't edit existing files in-place. moviepy.editor 不断弄乱视频文件,ffmpeg 不断给我一个错误,它无法就地编辑现有文件。 Combining.mp4 with another audio file type is also fine, but if so it would be nice to also answer how to convert midi files to the file type you answered with.将 .mp4 与另一种音频文件类型结合也很好,但如果是这样,最好也回答如何将 midi 文件转换为您回答的文件类型。 Thanks for the help!谢谢您的帮助!

moviepy.editor workflow: moviepy.editor 工作流程:

video = mpe.VideoFileClip(mp4_path)
os.system(f"timidity {midi_path} -Ow -o {wav_path)}")  # Convert .mid to .wav
video = video.set_audio(mpe.AudioFileClip(wav_path))
video.write_videofile(mp4_path, fps=fps)

ffmpeg workflow: ffmpeg 工作流程:

video = ffmpeg.input(mp4_path)
os.system(f"timidity {midi_path} -Ow -o {wav_path)}")  # Convert .mid to .wav
audio = ffmpeg.input(wav_path)
video = ffmpeg.output(video, audio, path, vcodec='copy', acodec='aac', strict='experimental')
ffmpeg.run(video)

I tested both modules and for moviepy I get correct output video with audio even if I use the same name as output.我测试了两个模块,对于moviepy ,即使我使用与output 相同的名称,我也会得到正确的output 视频和音频。 So I don't know what can mess with output.所以我不知道output有什么问题。

For ffmpeg I had to use different name for output file to resolve problem with couldn't edit existing files in-place对于ffmpeg我不得不为 output 文件使用不同的名称来解决couldn't edit existing files in-place问题

I had to also use object.video and object.audio to replace audio in output file.我还必须使用object.videoobject.audio来替换 output 文件中的音频。

video  = ffmpeg.input(video_path).video  # get only video channel
audio  = ffmpeg.input(audio_path).audio  # get only audio channel

My testing code我的测试代码

def test_moviepy(video_path, audio_path, output_path='output-moviepy.mp4', fps=24):
    import moviepy.editor as mpe
    
    print('--- moviepy ---')

    video = mpe.VideoFileClip(video_path)
    video = video.set_audio(mpe.AudioFileClip(audio_path))
    video.write_videofile(output_path, fps=fps)


def test_ffmpeg(video_path, audio_path, output_path='output-ffmpeg.mp4', fps=24):
    import ffmpeg

    print('--- ffmpeg ---')

    video  = ffmpeg.input(video_path).video # get only video channel
    audio  = ffmpeg.input(audio_path).audio # get only audio channel
    output = ffmpeg.output(video, audio, output_path, vcodec='copy', acodec='aac', strict='experimental')
    ffmpeg.run(output)

# --- main ---

video_path  = 'movie.mp4'
audio_path  = 'sound.wav'
output_path = 'output.mp4'

test_moviepy(video_path, audio_path)#, output_path)
test_ffmpeg(video_path, audio_path)#, output_path)

EDIT:编辑:

After installing python module graphviz and program graphviz I could run安装 python 模块graphviz和程序graphviz后,我可以运行

ffmpeg.view(output, filename='output-ffmpeg.png')

to get image获取图像

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM