Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -114,23 +114,41 @@ def infer(audio_input_path):
|
|
| 114 |
def load_chosen_audio(audio_path):
|
| 115 |
return audio_path
|
| 116 |
|
| 117 |
-
def overlay_audio(original_mp3: str, translated_wav: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
# Load original MP3 and convert to WAV
|
| 119 |
original = AudioSegment.from_mp3(original_mp3).set_frame_rate(16000).set_channels(1)
|
| 120 |
|
| 121 |
-
# Lower the volume (
|
| 122 |
-
original = original -
|
| 123 |
|
| 124 |
# Load the translated WAV
|
| 125 |
translated = AudioSegment.from_wav(translated_wav).set_frame_rate(16000).set_channels(1)
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
# Overlay the translated speech over the original
|
| 128 |
combined = original.overlay(translated)
|
| 129 |
-
|
| 130 |
# Create a temporary file to save the output
|
| 131 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
| 132 |
combined.export(temp_file.name, format="wav")
|
| 133 |
-
|
| 134 |
print(f"Final audio saved at: {temp_file.name}")
|
| 135 |
return temp_file.name # Return the temporary file path
|
| 136 |
|
|
@@ -164,7 +182,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 164 |
visible=False,
|
| 165 |
scale=2
|
| 166 |
)
|
| 167 |
-
choose_this_btn = gr.Button("Use this one", scale=1)
|
| 168 |
combined_output = gr.Audio("Combined Outpu", visible=False)
|
| 169 |
with gr.Accordion("Downloadable audio Output list", open=False, visible=False) as result_accordion:
|
| 170 |
wav_list = gr.Files(label="Output Audio List", visible=False)
|
|
|
|
| 114 |
def load_chosen_audio(audio_path):
|
| 115 |
return audio_path
|
| 116 |
|
| 117 |
+
def overlay_audio(original_mp3: str, translated_wav: str, volume_reduction_db: int = 10) -> str:
|
| 118 |
+
"""
|
| 119 |
+
Overlays translated audio on top of the original, reduces the original volume,
|
| 120 |
+
and ensures the final audio lasts as long as the longer of the two tracks.
|
| 121 |
+
|
| 122 |
+
:param original_mp3: Path to the original MP3 file.
|
| 123 |
+
:param translated_wav: Path to the translated WAV file.
|
| 124 |
+
:param volume_reduction_db: Volume reduction in dB (default is -10 dB).
|
| 125 |
+
:return: Path to the temporary output WAV file.
|
| 126 |
+
"""
|
| 127 |
# Load original MP3 and convert to WAV
|
| 128 |
original = AudioSegment.from_mp3(original_mp3).set_frame_rate(16000).set_channels(1)
|
| 129 |
|
| 130 |
+
# Lower the volume (default is -10 dB)
|
| 131 |
+
original = original - volume_reduction_db
|
| 132 |
|
| 133 |
# Load the translated WAV
|
| 134 |
translated = AudioSegment.from_wav(translated_wav).set_frame_rate(16000).set_channels(1)
|
| 135 |
|
| 136 |
+
# Determine the final length (longer of the two)
|
| 137 |
+
final_length = max(len(original), len(translated))
|
| 138 |
+
|
| 139 |
+
# Extend the shorter track with silence to match the longer track
|
| 140 |
+
if len(original) < final_length:
|
| 141 |
+
original = original + AudioSegment.silent(duration=final_length - len(original))
|
| 142 |
+
if len(translated) < final_length:
|
| 143 |
+
translated = translated + AudioSegment.silent(duration=final_length - len(translated))
|
| 144 |
+
|
| 145 |
# Overlay the translated speech over the original
|
| 146 |
combined = original.overlay(translated)
|
| 147 |
+
|
| 148 |
# Create a temporary file to save the output
|
| 149 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
| 150 |
combined.export(temp_file.name, format="wav")
|
| 151 |
+
|
| 152 |
print(f"Final audio saved at: {temp_file.name}")
|
| 153 |
return temp_file.name # Return the temporary file path
|
| 154 |
|
|
|
|
| 182 |
visible=False,
|
| 183 |
scale=2
|
| 184 |
)
|
| 185 |
+
choose_this_btn = gr.Button("Use this one", scale=1, visible=False)
|
| 186 |
combined_output = gr.Audio("Combined Outpu", visible=False)
|
| 187 |
with gr.Accordion("Downloadable audio Output list", open=False, visible=False) as result_accordion:
|
| 188 |
wav_list = gr.Files(label="Output Audio List", visible=False)
|