EP68-Python Script for Audio Effects Processing and Video Combination with Noise Reduction and Effects

Kritthanit Malathong
2 min readMay 9, 2024

--

Date: 09/05/2024

Let’s break down the code step by step:

1. **Importing Required Modules**:
— The code starts by importing necessary modules:
— `VideoFileClip` and `AudioFileClip` from `moviepy.editor` for working with video and audio files.
— `wavfile` from `scipy.io` for reading and writing WAV files.
— `noisereduce` as `nr` for noise reduction.
— `numpy` as `np` for numerical operations.
— `Pedalboard` and other related modules from `pedalboard` for audio effects processing.

2. **Reading Video and Extracting Audio**:
— The video file “Videos/001.mkv” is read using `VideoFileClip` to create a `video_clip` object.
— The audio is extracted from the video clip using the `audio` attribute.

3. **Saving Extracted Audio to WAV File**:
— The extracted audio is saved to a WAV file named “NewVideos/output_audio.wav” using the `write_audiofile` method of the audio clip object. The codec used is `’pcm_s16le’` (16-bit PCM), and the frame rate (`fps`) is set to the frame rate of the audio clip.

4. **Loading and Performing Noise Reduction on the Audio**:
— The saved WAV file is loaded using `wavfile.read()`, which returns the sampling rate (`rate`) and audio data (`data`) as a NumPy array.
— Noise reduction is performed on the audio data (`data`) using `nr.reduce_noise()` function from the `noisereduce` library. The parameters `stationary` and `prop_decrease` are used to control the noise reduction process.
— A `Pedalboard` object (`board`) is created to apply a series of audio effects using the `pedalboard` library. The effects include NoiseGate, Compressor, LowShelfFilter, and Gain.
— Another `Pedalboard` object (`board1`) is created to apply additional audio effects (Chorus and Reverb).

5. **Writing Noise-Reduced and Effected Audio to WAV File**:
— The noise-reduced audio data is reshaped to its original shape using the `reshape` function.
— The noise-reduced audio data is written to a WAV file named “NewVideos/reduced_noise.wav” using `wavfile.write()`.
— The `AudioFile` context manager is used to read the noise-reduced audio file and apply the effects specified in `board` and `board1`. The resampling rate is set to the original sampling rate (`rate`). The processed audio is then written to a WAV file named “NewVideos/reduced_noise_effected.wav”.

6. **Combining Audio with Video**:
— The processed audio file “NewVideos/reduced_noise_effected.wav” is loaded as an `AudioFileClip`.
— The audio of the video clip (`video_clip`) is replaced with the processed audio using the `set_audio` method.
— The combined video with the processed audio is written to a file named “NewVideos/combined_video_effect_1.mp4” using the `write_videofile` method. The video codec is set to `”libx264"`, and the audio codec is set to `”aac”`.

This code essentially reads a video file, extracts its audio, performs noise reduction and audio effects processing, and then combines the processed audio with the original video, producing a new video file with the desired audio effects applied.

--

--

No responses yet