Spaces:
Running
Running
Update conver.py
Browse files
conver.py
CHANGED
@@ -10,6 +10,7 @@ import tempfile
|
|
10 |
from pydub import AudioSegment
|
11 |
import base64
|
12 |
from pathlib import Path
|
|
|
13 |
|
14 |
@dataclass
|
15 |
class ConversationConfig:
|
@@ -81,7 +82,7 @@ class URLToAudioConverter:
|
|
81 |
|
82 |
try:
|
83 |
for i, turn in enumerate(conversation_json["conversation"]):
|
84 |
-
filename = output_dir / f"output_{i}.mp3"
|
85 |
voice = voice_1 if i % 2 == 0 else voice_2
|
86 |
|
87 |
tmp_path, error = await self._generate_audio(turn["text"], voice)
|
@@ -106,7 +107,7 @@ class URLToAudioConverter:
|
|
106 |
pitch_str = f"{pitch:+d}Hz"
|
107 |
communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str)
|
108 |
|
109 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
110 |
tmp_path = tmp_file.name
|
111 |
await communicate.save(tmp_path)
|
112 |
|
@@ -125,17 +126,29 @@ class URLToAudioConverter:
|
|
125 |
try:
|
126 |
combined = AudioSegment.empty()
|
127 |
for filename in filenames:
|
128 |
-
# Leer como MP3
|
129 |
audio_segment = AudioSegment.from_file(filename, format="mp3")
|
130 |
combined += audio_segment
|
131 |
|
132 |
-
# Exportar como MP3
|
133 |
combined.export(output_file, format="mp3")
|
134 |
|
135 |
-
# Limpieza
|
136 |
-
|
137 |
-
|
138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
|
140 |
except Exception as e:
|
141 |
raise RuntimeError(f"Failed to combine audio files: {e}")
|
@@ -156,7 +169,7 @@ class URLToAudioConverter:
|
|
156 |
conversation_json, voice_1, voice_2
|
157 |
)
|
158 |
|
159 |
-
final_output = os.path.join(folder_name, "combined_output.mp3")
|
160 |
self.combine_audio_files(audio_files, final_output)
|
161 |
return final_output, conversation_text
|
162 |
|
@@ -169,6 +182,6 @@ class URLToAudioConverter:
|
|
169 |
audio_files, folder_name = await self.text_to_speech(
|
170 |
conversation_json, voice_1, voice_2
|
171 |
)
|
172 |
-
final_output = os.path.join(folder_name, "combined_output.mp3")
|
173 |
self.combine_audio_files(audio_files, final_output)
|
174 |
return final_output, conversation_text
|
|
|
10 |
from pydub import AudioSegment
|
11 |
import base64
|
12 |
from pathlib import Path
|
13 |
+
import shutil # Importamos shutil para manejo de directorios
|
14 |
|
15 |
@dataclass
|
16 |
class ConversationConfig:
|
|
|
82 |
|
83 |
try:
|
84 |
for i, turn in enumerate(conversation_json["conversation"]):
|
85 |
+
filename = output_dir / f"output_{i}.mp3"
|
86 |
voice = voice_1 if i % 2 == 0 else voice_2
|
87 |
|
88 |
tmp_path, error = await self._generate_audio(turn["text"], voice)
|
|
|
107 |
pitch_str = f"{pitch:+d}Hz"
|
108 |
communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str)
|
109 |
|
110 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
111 |
tmp_path = tmp_file.name
|
112 |
await communicate.save(tmp_path)
|
113 |
|
|
|
126 |
try:
|
127 |
combined = AudioSegment.empty()
|
128 |
for filename in filenames:
|
|
|
129 |
audio_segment = AudioSegment.from_file(filename, format="mp3")
|
130 |
combined += audio_segment
|
131 |
|
|
|
132 |
combined.export(output_file, format="mp3")
|
133 |
|
134 |
+
# Limpieza mejorada y robusta
|
135 |
+
dir_path = os.path.dirname(filenames[0])
|
136 |
+
|
137 |
+
# Eliminar todos los archivos en el directorio
|
138 |
+
for file in os.listdir(dir_path):
|
139 |
+
file_path = os.path.join(dir_path, file)
|
140 |
+
if os.path.isfile(file_path):
|
141 |
+
try:
|
142 |
+
os.remove(file_path)
|
143 |
+
except Exception as e:
|
144 |
+
print(f"Warning: Could not remove file {file_path}: {str(e)}")
|
145 |
+
|
146 |
+
# Intentar eliminar el directorio (no crítico si falla)
|
147 |
+
try:
|
148 |
+
os.rmdir(dir_path)
|
149 |
+
except OSError as e:
|
150 |
+
print(f"Info: Could not remove directory {dir_path}: {str(e)}")
|
151 |
+
# No es crítico, el espacio puede continuar
|
152 |
|
153 |
except Exception as e:
|
154 |
raise RuntimeError(f"Failed to combine audio files: {e}")
|
|
|
169 |
conversation_json, voice_1, voice_2
|
170 |
)
|
171 |
|
172 |
+
final_output = os.path.join(folder_name, "combined_output.mp3")
|
173 |
self.combine_audio_files(audio_files, final_output)
|
174 |
return final_output, conversation_text
|
175 |
|
|
|
182 |
audio_files, folder_name = await self.text_to_speech(
|
183 |
conversation_json, voice_1, voice_2
|
184 |
)
|
185 |
+
final_output = os.path.join(folder_name, "combined_output.mp3")
|
186 |
self.combine_audio_files(audio_files, final_output)
|
187 |
return final_output, conversation_text
|