Alpersx commited on
Commit
5cb76bf
·
verified ·
1 Parent(s): 74c21f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -46
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(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()
 
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()