Reputation: 21
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")
Upvotes: 2
Views: 3071
Reputation: 147
Use subprocess and call ffmpeg.
import subprocess
# Paths to input video, input audio (MP3), and output video
input_video = "video.mp4"
input_audio = "audio.mp3"
# FFmpeg command to overwrite the audio in the video with the new audio
ffmpeg_command = f'ffmpeg -i {input_video} -i {input_audio} -c:v copy -c:a aac -strict experimental -map 0:v -map 1:a -shortest output_video.mp4'
# Run the FFmpeg command
subprocess.call(ffmpeg_command, shell=True)
Upvotes: 0
Reputation: 115
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")
Upvotes: 3
Reputation: 11
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
Upvotes: 0
Reputation: 66
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")
Upvotes: 1