0

I am trying to use MoviePy to do basic video editing. 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")
Marco
  • 1
  • 1

3 Answers3

1

As far as I know, when you extract a VideoFileClip from an mp4 file you're only extracting the video, not the audio. To add the audio to it you can do the same thing but with AudioFileClip instead and adding it to the 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")
LucasCS
  • 56
  • 3
0

I used the solution below since Moviepy kept giving me errors no matter what I did. I use ffmpeg command. Works better.

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

Assigning an audio_codec fixed this issue for me. I think the audio_codec is None by default

final_clip.write_videofile("output/output.mp4", fps=24, audio_codec="aac")
The Guru
  • 55
  • 6