king_anton
king_anton

Reputation: 351

moviepy introducing weird audio artifacts at end of audio files, when creating videos via Python. Why?

These are not present in the original audio files. They get added to the end during the video creation process somehow. See this screenshot for a before and after comparison of the waveforms:

enter image description here

What's weirder is, the artifacts SOUND kind of like the narrator. Like it sounds as if the next sentence is starting, and he just starts to utter the first 0.1 seconds of the first word, which then abruptly gets cut off. That doesn't make much sense as an explanation though, because all this code does is take the one single audio file, and lay the video elements on top of it.

While there is more to my code, here are the pertinent sections:

    current_audio_file = os.path.join(current_video_folder_path, f'{current_video_title_template}-part-{current_video_section}.mp3')
audio_clip = AudioFileClip(current_audio_file)
        final_clip = concatenate_videoclips(clips).subclip(0, audio_clip.duration)
    final_clip = final_clip.set_audio(audio_clip)
    final_clip_path = os.path.join(current_video_folder_path, f"video-part-{current_video_section}.mp4")

    final_clip.write_videofile(final_clip_path, codec="libx264", audio_codec="aac")

One thing I tried was switching the audio codec to a different one. No difference. In fact it introduced a literally identical audio artifact -- not like a unique one in the same spot, it was literally the identical sound.

My current best idea is a poor, yet functional workaround: Simply chop the last 0.2 seconds from the end of every audio file. I did that here like below, and it worked to eliminate the artifact. Which seems to indicate, it might get generated in the process of converting it into an AudioFileClip() element?

    current_audio_file = os.path.join(current_video_folder_path, f'{current_video_title_template}-part-{current_video_section}.mp3')
audio_clip = AudioFileClip(current_audio_file)

# Trim the last 0.X seconds from the audio
# this is a poor, but functional, workaround to remove those weird artifacts at end of certain audio clips.
audio_duration = audio_clip.duration
audio_clip = audio_clip.subclip(0, max(0, audio_duration - 0.2))

I ran just this, to debug, and indeed this by itself introduced the audio artifact:

current_audio_file = os.path.join(current_video_folder_path, f'{current_video_title_template}-part-{current_video_section}.mp3')
audio_clip = AudioFileClip(current_audio_file)

# Export the trimmed audio for inspection
debug_audio_path = os.path.join(current_video_folder_path, f'debug_{current_video_section}.mp3')
audio_clip.write_audiofile(debug_audio_path)

exit()

Also tried converting it to a WAV audio file, and using that as the input, then also exporting as a WAV audio file -- same issue with the artifact being at the end.

Anyway, if anyone has experienced this before, or has any ideas, I'd be curious.

Upvotes: 2

Views: 241

Answers (2)

FUZAIL SOHAIL
FUZAIL SOHAIL

Reputation: 1

audio_clip = audio_clip.subclip(0, audio_clip.duration - 0.05)

this worked for me too it is likely due to a minor artifact introduced at the end of the audio.

Upvotes: 0

Abdul Rehman
Abdul Rehman

Reputation: 1

The issue you’re experiencing is likely due to a minor artifact introduced at the end of the audio during the processing or encoding step. A simple and effective workaround is to trim a very small portion from the end of the audio file. Here's how you can fix it:

audio_clip = audio_clip.subclip(0, audio_clip.duration - 0.05)

Why this works:

The artifact often occurs at the end of the audio file due to encoding quirks or synchronization issues with moviepy. Trimming the last 0.05 seconds removes this artifact while keeping the rest of the audio intact. This approach has worked consistently for me. If the issue persists, you can adjust the trimming duration slightly (e.g., 0.02 or 0.1 seconds) to better suit your files.

Upvotes: -1

Related Questions