Spaces:
Sleeping
Sleeping
Ali Safaya commited on
Commit ·
ac3b66c
1
Parent(s): 2ca69ee
initial commit
Browse files- README.md +1 -1
- app.py +120 -0
- packages.txt +1 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
---
|
| 2 |
-
title: Deep Filter
|
| 3 |
emoji: 📈
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: green
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Audio Enhancement with Deep Filter
|
| 3 |
emoji: 📈
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: green
|
app.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import ffmpeg
|
| 3 |
+
import uuid
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import shutil
|
| 6 |
+
import logging
|
| 7 |
+
|
| 8 |
+
logging.basicConfig(level=logging.INFO) # Set the logging level
|
| 9 |
+
|
| 10 |
+
def log_info(message):
|
| 11 |
+
logging.info(message)
|
| 12 |
+
|
| 13 |
+
def log_error(error_message):
|
| 14 |
+
logging.error(error_message)
|
| 15 |
+
|
| 16 |
+
# Function to extract audio using ffmpeg
|
| 17 |
+
def convert_audio(input_file):
|
| 18 |
+
audio_file = f"{input_file}.wav"
|
| 19 |
+
ffmpeg.input(input_file).output(audio_file, ar=48000, ac=1).run()
|
| 20 |
+
return audio_file
|
| 21 |
+
|
| 22 |
+
# Function to split audio into segments
|
| 23 |
+
def split_audio(audio_file):
|
| 24 |
+
segment_pattern = os.path.splitext(audio_file)[0] + ".segmented_%03d.wav"
|
| 25 |
+
ffmpeg.input(audio_file).output(segment_pattern, f="segment", segment_time=300).run()
|
| 26 |
+
return segment_pattern
|
| 27 |
+
|
| 28 |
+
# Function to filter each audio segment
|
| 29 |
+
def filter_audio(segments):
|
| 30 |
+
filtered_dir = "temp-out"
|
| 31 |
+
os.makedirs(filtered_dir, exist_ok=True)
|
| 32 |
+
os.system(f"deepFilter -a 20 -o {filtered_dir} {segments.replace('%03d.wav', '')}*.wav")
|
| 33 |
+
os.system(f"rm {segments.replace('%03d.wav', '')}*.wav")
|
| 34 |
+
return filtered_dir
|
| 35 |
+
|
| 36 |
+
# Function to combine filtered segments using sox
|
| 37 |
+
def combine_segments(filtered_dir, output_file):
|
| 38 |
+
os.system(f"sox {filtered_dir}/*.wav {output_file}")
|
| 39 |
+
return output_file
|
| 40 |
+
|
| 41 |
+
# Function to check if input file is video
|
| 42 |
+
def is_video(input_file):
|
| 43 |
+
result = os.popen(f"ffprobe -v error -show_entries stream=codec_type -of default=noprint_wrappers=1:nokey=1 {input_file}").read()
|
| 44 |
+
return "video" in result.lower()
|
| 45 |
+
|
| 46 |
+
# Function to get format info for audio files
|
| 47 |
+
def get_format_info(input_file):
|
| 48 |
+
result = os.popen(f"ffprobe -v error -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 {input_file}").read()
|
| 49 |
+
return result
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
# Function to handle video or audio input
|
| 53 |
+
def process_input(input_file):
|
| 54 |
+
try:
|
| 55 |
+
audio_file = convert_audio(input_file)
|
| 56 |
+
segments = split_audio(audio_file)
|
| 57 |
+
filtered_dir = filter_audio(segments)
|
| 58 |
+
output_file = combine_segments(filtered_dir, f"{input_file}.filtered.wav")
|
| 59 |
+
shutil.rmtree(filtered_dir)
|
| 60 |
+
output_format = input_file.split(".")[-1]
|
| 61 |
+
|
| 62 |
+
if is_video(input_file):
|
| 63 |
+
audio_aac_file = f"filtered-{input_file}.aac"
|
| 64 |
+
ffmpeg.input(output_file).output(audio_aac_file, ar=44100, codec="aac").run()
|
| 65 |
+
|
| 66 |
+
video_stream = ffmpeg.input(input_file).video
|
| 67 |
+
audio_aac_stream = ffmpeg.input(audio_aac_file).audio
|
| 68 |
+
|
| 69 |
+
merged_file = f"filtered-{input_file}.{output_format}"
|
| 70 |
+
ffmpeg.output(video_stream, audio_aac_stream, merged_file, vcodec="copy", acodec="copy").run()
|
| 71 |
+
|
| 72 |
+
os.remove(audio_aac_file)
|
| 73 |
+
os.remove(output_file)
|
| 74 |
+
os.remove(audio_file)
|
| 75 |
+
os.rename(merged_file, input_file)
|
| 76 |
+
|
| 77 |
+
return input_file
|
| 78 |
+
else:
|
| 79 |
+
ffmpeg.input(output_file).output(f"filtered-{input_file}.{output_format}", ac=1, ar=16000).run()
|
| 80 |
+
|
| 81 |
+
os.rename(f"filtered-{input_file}.{output_format}", input_file)
|
| 82 |
+
os.remove(output_file)
|
| 83 |
+
os.remove(audio_file)
|
| 84 |
+
|
| 85 |
+
return input_file
|
| 86 |
+
except Exception as e:
|
| 87 |
+
log_error(f"Error occurred during processing: {str(e)}")
|
| 88 |
+
return None
|
| 89 |
+
|
| 90 |
+
# Streamlit UI
|
| 91 |
+
st.title("Audio Enhancement with Deep Filter")
|
| 92 |
+
|
| 93 |
+
uploaded_file = st.file_uploader("Upload a file", type=["mp4", "wav", "mp3", "aac", "m4a", "flac", "ogg", "opus", "wma", "webm", "mkv"])
|
| 94 |
+
|
| 95 |
+
if uploaded_file is not None:
|
| 96 |
+
try:
|
| 97 |
+
file_details = {"FileName": uploaded_file.name, "FileType": uploaded_file.type, "FileSize": uploaded_file.size}
|
| 98 |
+
st.write(file_details)
|
| 99 |
+
|
| 100 |
+
if st.button("Process File"):
|
| 101 |
+
# generate a random file name to save the uploaded file
|
| 102 |
+
file_path = f"temp-{str(uuid.uuid4())[:8]}.{uploaded_file.name.split('.')[-1]}"
|
| 103 |
+
|
| 104 |
+
with open(file_path, "wb") as f:
|
| 105 |
+
f.write(uploaded_file.getbuffer())
|
| 106 |
+
|
| 107 |
+
output_file_path = process_input(file_path)
|
| 108 |
+
|
| 109 |
+
if output_file_path and os.path.exists(output_file_path):
|
| 110 |
+
data = open(output_file_path, "rb").read()
|
| 111 |
+
os.remove(file_path)
|
| 112 |
+
|
| 113 |
+
st.success("File processed successfully!")
|
| 114 |
+
st.download_button(label="Download Processed File",
|
| 115 |
+
data=data,
|
| 116 |
+
file_name="filtered-" + uploaded_file.name)
|
| 117 |
+
else:
|
| 118 |
+
st.error("File processing failed.")
|
| 119 |
+
except Exception as e:
|
| 120 |
+
log_error(f"Error occurred during file processing: {str(e)}")
|
packages.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
sox
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchaudio
|
| 3 |
+
deepfilternet
|