Quantcast
Viewing all articles
Browse latest Browse all 14069

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:

Image may be NSFW.
Clik here to view.
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.durationaudio_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 inspectiondebug_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.


Viewing all articles
Browse latest Browse all 14069

Trending Articles