Spaces:
Sleeping
Sleeping
Update conver.py
Browse files
conver.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
from dataclasses import dataclass
|
| 2 |
-
from typing import List, Tuple, Dict
|
| 3 |
import os
|
| 4 |
import re
|
| 5 |
import httpx
|
|
@@ -81,7 +81,7 @@ class URLToAudioConverter:
|
|
| 81 |
|
| 82 |
try:
|
| 83 |
for i, turn in enumerate(conversation_json["conversation"]):
|
| 84 |
-
filename = output_dir / f"output_{i}.
|
| 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 +106,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=".
|
| 110 |
tmp_path = tmp_file.name
|
| 111 |
await communicate.save(tmp_path)
|
| 112 |
|
|
@@ -125,10 +125,12 @@ class URLToAudioConverter:
|
|
| 125 |
try:
|
| 126 |
combined = AudioSegment.empty()
|
| 127 |
for filename in filenames:
|
| 128 |
-
|
|
|
|
| 129 |
combined += audio_segment
|
| 130 |
|
| 131 |
-
|
|
|
|
| 132 |
|
| 133 |
# Limpieza de archivos temporales
|
| 134 |
for filename in filenames:
|
|
@@ -154,12 +156,12 @@ class URLToAudioConverter:
|
|
| 154 |
conversation_json, voice_1, voice_2
|
| 155 |
)
|
| 156 |
|
| 157 |
-
final_output = os.path.join(folder_name, "combined_output.
|
| 158 |
self.combine_audio_files(audio_files, final_output)
|
| 159 |
return final_output, conversation_text
|
| 160 |
|
| 161 |
async def text_to_audio(self, text: str, voice_1: str, voice_2: str) -> Tuple[str, str]:
|
| 162 |
-
"""
|
| 163 |
conversation_json = self.extract_conversation(text)
|
| 164 |
conversation_text = "\n".join(
|
| 165 |
f"{turn['speaker']}: {turn['text']}" for turn in conversation_json["conversation"]
|
|
@@ -167,6 +169,6 @@ class URLToAudioConverter:
|
|
| 167 |
audio_files, folder_name = await self.text_to_speech(
|
| 168 |
conversation_json, voice_1, voice_2
|
| 169 |
)
|
| 170 |
-
final_output = os.path.join(folder_name, "combined_output.
|
| 171 |
self.combine_audio_files(audio_files, final_output)
|
| 172 |
return final_output, conversation_text
|
|
|
|
| 1 |
from dataclasses import dataclass
|
| 2 |
+
from typing import List, Tuple, Dict
|
| 3 |
import os
|
| 4 |
import re
|
| 5 |
import httpx
|
|
|
|
| 81 |
|
| 82 |
try:
|
| 83 |
for i, turn in enumerate(conversation_json["conversation"]):
|
| 84 |
+
filename = output_dir / f"output_{i}.mp3" # Cambiado a 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 |
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: # Cambiado a MP3
|
| 110 |
tmp_path = tmp_file.name
|
| 111 |
await communicate.save(tmp_path)
|
| 112 |
|
|
|
|
| 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 de archivos temporales
|
| 136 |
for filename in filenames:
|
|
|
|
| 156 |
conversation_json, voice_1, voice_2
|
| 157 |
)
|
| 158 |
|
| 159 |
+
final_output = os.path.join(folder_name, "combined_output.mp3") # Cambiado a MP3
|
| 160 |
self.combine_audio_files(audio_files, final_output)
|
| 161 |
return final_output, conversation_text
|
| 162 |
|
| 163 |
async def text_to_audio(self, text: str, voice_1: str, voice_2: str) -> Tuple[str, str]:
|
| 164 |
+
"""Método para procesar texto directo"""
|
| 165 |
conversation_json = self.extract_conversation(text)
|
| 166 |
conversation_text = "\n".join(
|
| 167 |
f"{turn['speaker']}: {turn['text']}" for turn in conversation_json["conversation"]
|
|
|
|
| 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") # Cambiado a MP3
|
| 173 |
self.combine_audio_files(audio_files, final_output)
|
| 174 |
return final_output, conversation_text
|