Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,63 +7,71 @@ import numpy as np
|
|
| 7 |
from scipy.signal import butter, filtfilt
|
| 8 |
import os
|
| 9 |
|
| 10 |
-
def remove_wind_noise(
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
os.remove(clean_audio)
|
| 55 |
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
fn=remove_wind_noise,
|
| 61 |
-
inputs=gr.Video(label="Upload your
|
| 62 |
-
outputs=gr.Video(label="Cleaned
|
| 63 |
-
title="
|
| 64 |
-
description=
|
| 65 |
-
allow_flagging="never",
|
| 66 |
)
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
-
|
|
|
|
| 7 |
from scipy.signal import butter, filtfilt
|
| 8 |
import os
|
| 9 |
|
| 10 |
+
def remove_wind_noise(video_path):
|
| 11 |
+
try:
|
| 12 |
+
# === TEMP FILES ===
|
| 13 |
+
temp_audio = "temp_audio.wav"
|
| 14 |
+
clean_audio = "clean_audio.wav"
|
| 15 |
+
output_video = "cleaned_video.mp4"
|
| 16 |
|
| 17 |
+
# === STEP 1: Extract audio ===
|
| 18 |
+
video = VideoFileClip(video_path)
|
| 19 |
+
video.audio.write_audiofile(temp_audio, logger=None)
|
| 20 |
|
| 21 |
+
# === STEP 2: Load audio ===
|
| 22 |
+
audio_data, sr = librosa.load(temp_audio, sr=None)
|
| 23 |
|
| 24 |
+
# === STEP 3: High-pass filter ===
|
| 25 |
+
def highpass_filter(data, sr, cutoff=100, order=3):
|
| 26 |
+
nyquist = 0.5 * sr
|
| 27 |
+
normal_cutoff = cutoff / nyquist
|
| 28 |
+
b, a = butter(order, normal_cutoff, btype="high", analog=False)
|
| 29 |
+
return filtfilt(b, a, data)
|
| 30 |
|
| 31 |
+
filtered_audio = highpass_filter(audio_data, sr)
|
| 32 |
|
| 33 |
+
# === STEP 4: Noise reduction ===
|
| 34 |
+
noise_clip = filtered_audio[:int(sr * 0.5)]
|
| 35 |
+
reduced_noise = nr.reduce_noise(
|
| 36 |
+
y=filtered_audio,
|
| 37 |
+
y_noise=noise_clip,
|
| 38 |
+
sr=sr,
|
| 39 |
+
prop_decrease=0.5,
|
| 40 |
+
stationary=False
|
| 41 |
+
)
|
| 42 |
|
| 43 |
+
# === STEP 5: Save cleaned audio ===
|
| 44 |
+
sf.write(clean_audio, reduced_noise, sr)
|
| 45 |
|
| 46 |
+
# === STEP 6: Merge cleaned audio with video ===
|
| 47 |
+
clean_audio_clip = AudioFileClip(clean_audio)
|
| 48 |
+
final_video = video.set_audio(clean_audio_clip)
|
| 49 |
+
final_video.write_videofile(output_video, codec="libx264", audio_codec="aac", logger=None)
|
| 50 |
|
| 51 |
+
# Clean up temporary files
|
| 52 |
+
for f in [temp_audio, clean_audio]:
|
| 53 |
+
if os.path.exists(f):
|
| 54 |
+
os.remove(f)
|
|
|
|
| 55 |
|
| 56 |
+
return output_video
|
| 57 |
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return f"❌ Error: {str(e)}"
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
# === GRADIO INTERFACE ===
|
| 63 |
+
description = """
|
| 64 |
+
🎧 **Wind Noise Remover**
|
| 65 |
+
Upload a video (.MOV, .MP4) and remove background wind noise using Python + MoviePy + Librosa.
|
| 66 |
+
"""
|
| 67 |
+
|
| 68 |
+
app = gr.Interface(
|
| 69 |
fn=remove_wind_noise,
|
| 70 |
+
inputs=gr.Video(label="Upload your video"),
|
| 71 |
+
outputs=gr.Video(label="Cleaned video"),
|
| 72 |
+
title="Wind Noise Remover",
|
| 73 |
+
description=description,
|
|
|
|
| 74 |
)
|
| 75 |
|
| 76 |
if __name__ == "__main__":
|
| 77 |
+
app.launch()
|