Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MoviePy adds sound artifact to the end of the audio file after reading it #1936

Open
YaroslavSchubert opened this issue Mar 16, 2023 · 8 comments
Labels
audio Related to AudioClip, or handling of audio in general. bug Issues that report (apparent) bugs.

Comments

@YaroslavSchubert
Copy link

Expected Behavior

The resulting audio file will be just a copy of the original one.

Actual Behavior

The resulting audio file has a small artifact added to its end.

Steps to Reproduce the Problem

Here is a code to reproduce the problem:

from moviepy.audio.io.AudioFileClip import AudioFileClip

audio_path = "data/yt_short_sample.mp3"
audio = AudioFileClip(audio_path)
audio.write_audiofile("data/artifacts/test.mp3")

Example audio file:
https://soundcloud.com/yaroslav-schubert/sample-audio-to-reproduce-moviepy-issue/s-IZjaAlFxn3L

Here are the screenshots from Audacity:

End of the original audio file
image

End of the resulting audio file
image

Specifications

  • Python Version: 3.10.6
  • MoviePy Version: 1.0.3
  • Platform Name: Linux/Ubuntu
  • Platform Version: 18.04.6
@YaroslavSchubert YaroslavSchubert added the bug Issues that report (apparent) bugs. label Mar 16, 2023
@keikoro keikoro added the audio Related to AudioClip, or handling of audio in general. label Apr 12, 2023
@thertzelle
Copy link

thertzelle commented May 2, 2023

+1 Thanks for posting this. I thought I was going crazy, or I implemented something odd, but every so often, maybe by the 3rd of 4th clip, its like the first microsecond of the audio is played back before the next file starts. It happens no matter what encoding I use for the audio.

@YaroslavSchubert
Copy link
Author

I ended up adding audio to my clips by specifying an audio file path to the audio argument of the write_videofile function:

video_clip.write_videofile(out_file_path, fps=moviepy_fps, audio=audio_path)

@thertzelle
Copy link

So you have multiple clips? But then you have an audio_path? I don't get it, the clips have associated audios, then what is the audio_path for?

How do you keep the audio for each clip synced with the clip?

@YaroslavSchubert
Copy link
Author

YaroslavSchubert commented May 9, 2023

I have a single CompositeVideoClip that combines ImageClip, ColorClip, TextClip , VideoClip. I also have a single audio file that I add to the resulting CompositeVideoClip.

@jake-nz
Copy link

jake-nz commented Sep 29, 2023

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

@jake-nz
Copy link

jake-nz commented Sep 29, 2023

Same issue:
#1854
#1005
#1717
#1710
#1936

@lightwastak3n
Copy link

lightwastak3n commented Oct 10, 2023

@jake-nz
So like this https://colab.research.google.com/drive/1ZKkeWAKJ6GUQ4NuAjID99nbyAiIjE6ix?usp=sharing
It didn't fix it for me. Cutting a bit of the audio at the end and then combining it fixes it for me. But some of my files have speech until the end so it's not always doable.

I thought it was maybe some kind of padding to get the correct fps but I don't see any pattern in length of the audios that produce artifact and the ones that don't.

Update:
I found ~20 of these audio files that produce artifact and cutting 0.05 from their end fixes it for me. (I'm also extending image looping since audio has speech until the very end so this flows nicer)

audio = AudioFileClip(audio_path)
audio = audio.subclip(0, audio.duration - 0.05)

# Combine with image and add pause at the end (optional)
image_clip = ImageClip(image_path, duration=audio.duration + 0.2)
video_clip = image_clip.set_audio(audio)

@gchila
Copy link

gchila commented Mar 28, 2024

why is this closed while the problem is not solved ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
audio Related to AudioClip, or handling of audio in general. bug Issues that report (apparent) bugs.
Projects
None yet
Development

No branches or pull requests

6 participants