Spaces:
Running
on
Zero
Running
on
Zero
feat: add trim at the end of audio
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ import re
|
|
3 |
import subprocess
|
4 |
import os
|
5 |
import shutil
|
6 |
-
from pydub import AudioSegment
|
7 |
import gradio as gr
|
8 |
import traceback
|
9 |
import logging
|
@@ -38,7 +38,7 @@ def handle_file_upload(file):
|
|
38 |
shutil.copy(file.name, input_path)
|
39 |
|
40 |
return input_path, formatted_title
|
41 |
-
|
42 |
def run_inference(model_type, config_path, start_check_point, input_dir, output_dir, device_ids="0"):
|
43 |
command = [
|
44 |
"python", "inference.py",
|
@@ -93,8 +93,6 @@ def move_stems_to_parent(input_dir):
|
|
93 |
new_instrumental_path = os.path.join(parent_dir, "instrumental.wav")
|
94 |
print(f"Moving {instrumental_path} to {new_instrumental_path}")
|
95 |
shutil.move(instrumental_path, new_instrumental_path)
|
96 |
-
else:
|
97 |
-
print(f"Instrumental file not found: {instrumental_path}")
|
98 |
|
99 |
def combine_stems_for_all(input_dir):
|
100 |
for subdir, _, _ in os.walk(input_dir):
|
@@ -118,10 +116,29 @@ def combine_stems_for_all(input_dir):
|
|
118 |
stems = {name: AudioSegment.from_file(path) for name, path in stem_paths.items()}
|
119 |
combined = stems["vocals"].overlay(stems["bass"]).overlay(stems["others"]).overlay(stems["instrumental"])
|
120 |
|
|
|
|
|
|
|
121 |
output_file = os.path.join(subdir, f"{song_name}.MDS.wav")
|
122 |
-
|
123 |
print(f"Exported combined stems to {output_file}")
|
124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
def delete_folders_and_files(input_dir):
|
126 |
folders_to_delete = ['htdemucs', 'mel_band_roformer', 'scnet', 'bs_roformer']
|
127 |
files_to_delete = ['bass.wav', 'vocals.wav', 'other.wav', 'instrumental.wav']
|
|
|
3 |
import subprocess
|
4 |
import os
|
5 |
import shutil
|
6 |
+
from pydub import AudioSegment, silence
|
7 |
import gradio as gr
|
8 |
import traceback
|
9 |
import logging
|
|
|
38 |
shutil.copy(file.name, input_path)
|
39 |
|
40 |
return input_path, formatted_title
|
41 |
+
|
42 |
def run_inference(model_type, config_path, start_check_point, input_dir, output_dir, device_ids="0"):
|
43 |
command = [
|
44 |
"python", "inference.py",
|
|
|
93 |
new_instrumental_path = os.path.join(parent_dir, "instrumental.wav")
|
94 |
print(f"Moving {instrumental_path} to {new_instrumental_path}")
|
95 |
shutil.move(instrumental_path, new_instrumental_path)
|
|
|
|
|
96 |
|
97 |
def combine_stems_for_all(input_dir):
|
98 |
for subdir, _, _ in os.walk(input_dir):
|
|
|
116 |
stems = {name: AudioSegment.from_file(path) for name, path in stem_paths.items()}
|
117 |
combined = stems["vocals"].overlay(stems["bass"]).overlay(stems["others"]).overlay(stems["instrumental"])
|
118 |
|
119 |
+
# Trim silence from the end of the combined audio
|
120 |
+
trimmed_combined = trim_silence_at_end(combined)
|
121 |
+
|
122 |
output_file = os.path.join(subdir, f"{song_name}.MDS.wav")
|
123 |
+
trimmed_combined.export(output_file, format="wav")
|
124 |
print(f"Exported combined stems to {output_file}")
|
125 |
|
126 |
+
def trim_silence_at_end(audio_segment, silence_thresh=-50, chunk_size=10):
|
127 |
+
"""
|
128 |
+
Trims silence at the end of an AudioSegment.
|
129 |
+
:param audio_segment: The audio segment to trim.
|
130 |
+
:param silence_thresh: The threshold in dB below which is considered silence.
|
131 |
+
:param chunk_size: The size of the chunks in milliseconds that are checked for silence.
|
132 |
+
:return: A trimmed AudioSegment with silence removed from the end.
|
133 |
+
"""
|
134 |
+
silence_end = silence.detect_silence(audio_segment, min_silence_len=chunk_size, silence_thresh=silence_thresh)
|
135 |
+
|
136 |
+
if silence_end:
|
137 |
+
last_silence_start = silence_end[-1][0]
|
138 |
+
return audio_segment[:last_silence_start]
|
139 |
+
else:
|
140 |
+
return audio_segment
|
141 |
+
|
142 |
def delete_folders_and_files(input_dir):
|
143 |
folders_to_delete = ['htdemucs', 'mel_band_roformer', 'scnet', 'bs_roformer']
|
144 |
files_to_delete = ['bass.wav', 'vocals.wav', 'other.wav', 'instrumental.wav']
|