Alpersx commited on
Commit
b5de06b
·
verified ·
1 Parent(s): 5ec5d5a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -69
app.py CHANGED
@@ -1,69 +1,69 @@
1
- import gradio as gr
2
- from moviepy import VideoFileClip, AudioFileClip
3
- import librosa
4
- import noisereduce as nr
5
- import soundfile as sf
6
- import numpy as np
7
- from scipy.signal import butter, filtfilt
8
- import os
9
-
10
- def remove_wind_noise(video_file):
11
- # === Temporary filenames ===
12
- temp_audio = "original_audio.wav"
13
- clean_audio = "clean_audio.wav"
14
- output_video = "cleaned_video.mp4"
15
-
16
- # === Extract audio ===
17
- video = VideoFileClip(video_file)
18
- video.audio.write_audiofile(temp_audio, logger=None)
19
-
20
- # === Load audio ===
21
- audio_data, sr = librosa.load(temp_audio, sr=None)
22
-
23
- # === High-pass filter to remove rumble ===
24
- def highpass_filter(data, sr, cutoff=100, order=3):
25
- nyquist = 0.5 * sr
26
- normal_cutoff = cutoff / nyquist
27
- b, a = butter(order, normal_cutoff, btype='high', analog=False)
28
- return filtfilt(b, a, data)
29
-
30
- filtered_audio = highpass_filter(audio_data, sr)
31
-
32
- # === Noise reduction ===
33
- noise_clip = filtered_audio[:int(sr * 0.5)]
34
- reduced_noise = nr.reduce_noise(
35
- y=filtered_audio,
36
- y_noise=noise_clip,
37
- sr=sr,
38
- prop_decrease=0.5,
39
- stationary=False
40
- )
41
-
42
- # === Save cleaned audio ===
43
- sf.write(clean_audio, reduced_noise, sr)
44
-
45
- # === Merge audio back into video ===
46
- clean_audio_clip = AudioFileClip(clean_audio)
47
- final_video = video.with_audio(clean_audio_clip)
48
- final_video.write_videofile(output_video, codec="libx264", audio_codec="aac", logger=None)
49
-
50
- # === Cleanup ===
51
- video.close()
52
- clean_audio_clip.close()
53
- os.remove(temp_audio)
54
- os.remove(clean_audio)
55
-
56
- return output_video
57
-
58
- # === Gradio Interface ===
59
- interface = gr.Interface(
60
- fn=remove_wind_noise,
61
- inputs=gr.Video(label="Upload your .MOV video"),
62
- outputs=gr.Video(label="Cleaned Video"),
63
- title="🎧 Wind Noise Remover",
64
- description="Upload a .MOV video and automatically remove wind noise using a high-pass filter + noise reduction.",
65
- allow_flagging="never",
66
- )
67
-
68
- if __name__ == "__main__":
69
- interface.launch()
 
1
+ import gradio as gr
2
+ from moviepy.editor import VideoFileClip, AudioFileClip
3
+ import librosa
4
+ import noisereduce as nr
5
+ import soundfile as sf
6
+ import numpy as np
7
+ from scipy.signal import butter, filtfilt
8
+ import os
9
+
10
+ def remove_wind_noise(video_file):
11
+ # === Temporary filenames ===
12
+ temp_audio = "original_audio.wav"
13
+ clean_audio = "clean_audio.wav"
14
+ output_video = "cleaned_video.mp4"
15
+
16
+ # === Extract audio ===
17
+ video = VideoFileClip(video_file)
18
+ video.audio.write_audiofile(temp_audio, logger=None)
19
+
20
+ # === Load audio ===
21
+ audio_data, sr = librosa.load(temp_audio, sr=None)
22
+
23
+ # === High-pass filter to remove rumble ===
24
+ def highpass_filter(data, sr, cutoff=100, order=3):
25
+ nyquist = 0.5 * sr
26
+ normal_cutoff = cutoff / nyquist
27
+ b, a = butter(order, normal_cutoff, btype='high', analog=False)
28
+ return filtfilt(b, a, data)
29
+
30
+ filtered_audio = highpass_filter(audio_data, sr)
31
+
32
+ # === Noise reduction ===
33
+ noise_clip = filtered_audio[:int(sr * 0.5)]
34
+ reduced_noise = nr.reduce_noise(
35
+ y=filtered_audio,
36
+ y_noise=noise_clip,
37
+ sr=sr,
38
+ prop_decrease=0.5,
39
+ stationary=False
40
+ )
41
+
42
+ # === Save cleaned audio ===
43
+ sf.write(clean_audio, reduced_noise, sr)
44
+
45
+ # === Merge audio back into video ===
46
+ clean_audio_clip = AudioFileClip(clean_audio)
47
+ final_video = video.with_audio(clean_audio_clip)
48
+ final_video.write_videofile(output_video, codec="libx264", audio_codec="aac", logger=None)
49
+
50
+ # === Cleanup ===
51
+ video.close()
52
+ clean_audio_clip.close()
53
+ os.remove(temp_audio)
54
+ os.remove(clean_audio)
55
+
56
+ return output_video
57
+
58
+ # === Gradio Interface ===
59
+ interface = gr.Interface(
60
+ fn=remove_wind_noise,
61
+ inputs=gr.Video(label="Upload your .MOV video"),
62
+ outputs=gr.Video(label="Cleaned Video"),
63
+ title="🎧 Wind Noise Remover",
64
+ description="Upload a .MOV video and automatically remove wind noise using a high-pass filter + noise reduction.",
65
+ allow_flagging="never",
66
+ )
67
+
68
+ if __name__ == "__main__":
69
+ interface.launch()