简体   繁体   English

使用 MoviePy 时 output 视频中没有音频

[英]No Audio in output video when using MoviePy

I am trying to use MoviePy to do basic video editing.我正在尝试使用 MoviePy 进行基本的视频编辑。 Problem is that, when I save the video, there is no sound attached to it.问题是,当我保存视频时,没有附加声音。 Here's the basic code.这是基本代码。 Any help super appreciated - thanks!非常感谢任何帮助 - 谢谢!

from moviepy.editor import * 
clip =VideoFileClip("video.mp4").subclip(0,5)
clip.write_videofile("audio.mp4")

As far as I know, when you extract a VideoFileClip from an mp4 file you're only extracting the video, not the audio.据我所知,当您从 mp4 文件中提取 VideoFileClip 时,您只是在提取视频,而不是音频。 To add the audio to it you can do the same thing but with AudioFileClip instead and adding it to the VideoClip:要将音频添加到其中,您可以执行相同的操作,但使用 AudioFileClip 并将其添加到 VideoClip:

from moviepy.editor import * 
clip = VideoFileClip("video.mp4").subclip(0,5)
audio = AudioFileClip("video.mp4")
clip.audio  = audio.cutout(5,audio.duration) #adds the audio file and removes whatever comes after 5 seconds
clip.write_videofile("audio.mp4")

I used the solution below since Moviepy kept giving me errors no matter what I did.我使用了下面的解决方案,因为无论我做什么,Moviepy 都会不断给我错误。 I use ffmpeg command.我使用 ffmpeg 命令。 Works better.效果更好。

!ffmpeg -i video.mp4 -i audio.mp3 -map 0:v -map 1:a -c:v copy -shortest output.mp4

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

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