NotSoShabby
NotSoShabby

Reputation: 3678

Moviepy - audio gets corrupted when extracted from video

a simple code like this is creating a corrupted audio file for some reasons:

from moviepy import *

clip = VideoFileClip("cut.mp4")
audio = clip.audio
audio.to_audiofile('temp-audio.mp3')

Expected Behavior Audio should be the same as the audio in the video

Actual Behavior audio is corrupted in the end (repeats the end segment a few times like a broken record)

Steps to Reproduce the Problem:

Run the code above on this video with latest moviepy version (don't make fun it's just a trial lol) and you will get this audio which is corrupted (I compressed it in a zip): here

Specifications

Python Version: Python 3.9.9

Moviepy Version: 1.0.3

Upvotes: 0

Views: 812

Answers (2)

Jake
Jake

Reputation: 13141

This is caused by ffmepg incorrectly reporting the duration of the audio. This causes iter_chunks in AudioClip to try to read frames outside the length of the file. In FFMPEG_AudioReader get_frame reads the end of the file again, resulting in a glitch. For me, the fix was to set the env var FFMPEG_BINARY=/usr/bin/ffmpeg to use ffmpeg 6.0 from my system instead of ffmpeg 4.2.2 from inside imageio_ffmpeg

Upvotes: 0

hmody3000
hmody3000

Reputation: 47

may this will help you

from moviepy import *

clip = VideoFileClip("cut.mp4").subclip(0,1) 
#subclip mean video duration its from the place to start to the end
audio = clip.audio
audio.to_audiofile('temp-audio.mp3')

I hope I've been helpful

Upvotes: 1

Related Questions