forked from Dhruv16S/Transcribing-Video-to-Text
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noise_removal.py
28 lines (20 loc) · 906 Bytes
/
noise_removal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import librosa
from pydub import AudioSegment
import os
from plots import *
import noisereduce as nr
from scipy.io import wavfile
audio_path = './audio/original/audio_extracted.mp3'
audio, sr = librosa.load(audio_path, sr=None)
noise_reduced_audio = nr.reduce_noise(y=audio, sr=sr)
output_directory = "./graphs/cleaned"
if not os.path.exists(output_directory):
os.makedirs(output_directory)
plot_audio(noise_reduced_audio, sr, output_directory, title='Cleaned_Audio')
plot_spectrogram(noise_reduced_audio, sr, output_directory, title='Cleaned_Audio_Spectrogram')
output_directory = "./audio/cleaned"
if not os.path.exists(output_directory):
os.makedirs(output_directory)
wavfile.write(f'{output_directory}/cleaned_audio.wav', sr, noise_reduced_audio)
audio = AudioSegment.from_wav(f'{output_directory}/cleaned_audio.wav')
audio.export(f'{output_directory}/cleaned_audio.mp3', format='mp3')