Spaces:
Sleeping
Sleeping
Ali Safaya
commited on
Commit
·
195b17b
1
Parent(s):
c3ae9ef
update
Browse files- .streamlit/config.toml +2 -0
- app.py +86 -21
- packages.txt +1 -0
.streamlit/config.toml
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[server]
|
| 2 |
+
maxUploadSize = 1000
|
app.py
CHANGED
|
@@ -22,7 +22,7 @@ def convert_audio(input_file):
|
|
| 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=
|
| 26 |
return segment_pattern
|
| 27 |
|
| 28 |
# Function to filter each audio segment
|
|
@@ -48,41 +48,87 @@ 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
return input_file
|
| 86 |
except Exception as e:
|
| 87 |
log_error(f"Error occurred during processing: {str(e)}")
|
| 88 |
return None
|
|
@@ -97,6 +143,17 @@ if uploaded_file is not None:
|
|
| 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]}"
|
|
@@ -104,17 +161,25 @@ if uploaded_file is not None:
|
|
| 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=
|
| 117 |
else:
|
| 118 |
st.error("File processing failed.")
|
| 119 |
except Exception as e:
|
| 120 |
-
log_error(f"Error occurred during file processing: {str(e)}")
|
|
|
|
| 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=60).run()
|
| 26 |
return segment_pattern
|
| 27 |
|
| 28 |
# Function to filter each audio segment
|
|
|
|
| 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 |
+
# Function to increase volume of audio with sox
|
| 52 |
+
def increase_volume(input_file):
|
| 53 |
+
os.system(f"sox -v 1.5 {input_file} {input_file}.vol.wav")
|
| 54 |
+
os.rename(f"{input_file}.vol.wav", input_file)
|
| 55 |
+
return input_file
|
| 56 |
+
|
| 57 |
+
def restore_metadata(input, output):
|
| 58 |
+
os.system(f"ffmpeg -y -i {input} -c copy -map_metadata 0 -f ffmetadata {input}.txt")
|
| 59 |
+
|
| 60 |
+
lines = open(f"{input}.txt").readlines()
|
| 61 |
+
|
| 62 |
+
if len(lines) > 1:
|
| 63 |
+
lines = [lines[0],] + [line for line in lines if any(x in line for x in ("title", "artist", "album", "track", "date", "comment"))]
|
| 64 |
+
open(f"{input}.txt", "w").writelines(lines)
|
| 65 |
+
os.system(f"ffmpeg -y -i {output} -f ffmetadata -i {input}.txt -c copy -map_metadata 1 fixed.{output}")
|
| 66 |
+
os.rename(f"fixed.{output}", output)
|
| 67 |
+
|
| 68 |
+
os.remove(f"{input}.txt")
|
| 69 |
+
return output
|
| 70 |
+
|
| 71 |
+
def override_metadata(output, title="", artist="", album=""):
|
| 72 |
+
lines = [";FFMETADATA1", f"title={title}", f"artist={artist}", f"album={album}"]
|
| 73 |
+
open(f"{output}.txt", "w").write("\n".join(lines) + "\n")
|
| 74 |
+
|
| 75 |
+
# strip existing metadata
|
| 76 |
+
os.system(f"ffmpeg -y -i {output} -map_metadata -1 -c:v copy -c:a copy temp.{output}")
|
| 77 |
+
|
| 78 |
+
# add new metadata
|
| 79 |
+
os.system(f"ffmpeg -y -i temp.{output} -f ffmetadata -i {output}.txt -c copy -map_metadata 1 {output}")
|
| 80 |
+
|
| 81 |
+
os.remove(f"temp.{output}")
|
| 82 |
+
os.remove(f"{output}.txt")
|
| 83 |
+
return output
|
| 84 |
+
|
| 85 |
+
def get_samplerate(input_file):
|
| 86 |
+
result = os.popen(f"ffprobe -v error -show_entries stream=sample_rate -of default=noprint_wrappers=1:nokey=1 {input_file}").read()
|
| 87 |
+
return int(result.strip())
|
| 88 |
|
| 89 |
# Function to handle video or audio input
|
| 90 |
+
def process_input(input_file, convert_to_mp3=False):
|
| 91 |
try:
|
| 92 |
+
# extract audio from video and convert to wav
|
| 93 |
audio_file = convert_audio(input_file)
|
| 94 |
+
|
| 95 |
+
# split audio into smaller segments
|
| 96 |
segments = split_audio(audio_file)
|
| 97 |
+
|
| 98 |
+
# filter each segment
|
| 99 |
filtered_dir = filter_audio(segments)
|
| 100 |
+
|
| 101 |
+
# combine filtered segments
|
| 102 |
output_file = combine_segments(filtered_dir, f"{input_file}.filtered.wav")
|
| 103 |
+
shutil.rmtree(filtered_dir) # delete temp directory
|
| 104 |
+
|
| 105 |
+
# increase volume
|
| 106 |
+
output_file = increase_volume(output_file)
|
| 107 |
output_format = input_file.split(".")[-1]
|
| 108 |
|
| 109 |
+
if convert_to_mp3:
|
| 110 |
+
output_format = "mp3"
|
| 111 |
+
|
| 112 |
+
processed_file = f"filtered-{input_file}.{output_format}"
|
| 113 |
+
|
| 114 |
+
if output_format in ["mp4", "mkv", "webm", "avi", "mov", "flv", "wmv", "m4v"]:
|
| 115 |
audio_aac_file = f"filtered-{input_file}.aac"
|
| 116 |
ffmpeg.input(output_file).output(audio_aac_file, ar=44100, codec="aac").run()
|
| 117 |
|
| 118 |
video_stream = ffmpeg.input(input_file).video
|
| 119 |
audio_aac_stream = ffmpeg.input(audio_aac_file).audio
|
| 120 |
+
ffmpeg.output(video_stream, audio_aac_stream, processed_file, vcodec="copy", acodec="copy").run()
|
|
|
|
|
|
|
|
|
|
| 121 |
os.remove(audio_aac_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
else:
|
| 123 |
+
sample_rate = 16000 if convert_to_mp3 else get_samplerate(input_file)
|
| 124 |
+
ffmpeg.input(output_file).output(processed_file, ac=1, ar=16000 if sample_rate < 16000 else sample_rate).run()
|
| 125 |
|
| 126 |
+
os.remove(output_file)
|
| 127 |
+
os.remove(audio_file)
|
| 128 |
+
|
| 129 |
+
restore_metadata(input_file, processed_file)
|
| 130 |
+
return processed_file
|
| 131 |
|
|
|
|
| 132 |
except Exception as e:
|
| 133 |
log_error(f"Error occurred during processing: {str(e)}")
|
| 134 |
return None
|
|
|
|
| 143 |
file_details = {"FileName": uploaded_file.name, "FileType": uploaded_file.type, "FileSize": uploaded_file.size}
|
| 144 |
st.write(file_details)
|
| 145 |
|
| 146 |
+
convert_to_mp3 = st.toggle("Convert to MP3", False)
|
| 147 |
+
title = st.text_input("Title", "")
|
| 148 |
+
artist = st.text_input("Artist", "")
|
| 149 |
+
album = st.text_input("Album", "")
|
| 150 |
+
|
| 151 |
+
args = {
|
| 152 |
+
"title": title,
|
| 153 |
+
"artist": artist,
|
| 154 |
+
"album": album,
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
if st.button("Process File"):
|
| 158 |
# generate a random file name to save the uploaded file
|
| 159 |
file_path = f"temp-{str(uuid.uuid4())[:8]}.{uploaded_file.name.split('.')[-1]}"
|
|
|
|
| 161 |
with open(file_path, "wb") as f:
|
| 162 |
f.write(uploaded_file.getbuffer())
|
| 163 |
|
| 164 |
+
output_file_path = process_input(file_path, convert_to_mp3=convert_to_mp3)
|
| 165 |
+
|
| 166 |
+
# Override metadata if any
|
| 167 |
+
if title or artist or album:
|
| 168 |
+
override_metadata(output_file_path, title=title, artist=artist, album=album)
|
| 169 |
|
| 170 |
if output_file_path and os.path.exists(output_file_path):
|
| 171 |
data = open(output_file_path, "rb").read()
|
| 172 |
os.remove(file_path)
|
| 173 |
+
os.remove(output_file_path)
|
| 174 |
+
|
| 175 |
+
# uploaded file name without extension
|
| 176 |
+
download_name = "filtered-" + uploaded_file.name.split('.')[0] + "." + output_file_path.split(".")[-1]
|
| 177 |
|
| 178 |
st.success("File processed successfully!")
|
| 179 |
st.download_button(label="Download Processed File",
|
| 180 |
data=data,
|
| 181 |
+
file_name=download_name)
|
| 182 |
else:
|
| 183 |
st.error("File processing failed.")
|
| 184 |
except Exception as e:
|
| 185 |
+
log_error(f"Error occurred during file processing: {str(e)}")
|
packages.txt
CHANGED
|
@@ -1 +1,2 @@
|
|
|
|
|
| 1 |
sox
|
|
|
|
| 1 |
+
ffmpeg
|
| 2 |
sox
|