Spaces:
Runtime error
Runtime error
Commit ·
e3bcd20
1
Parent(s): 977628a
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,26 +5,12 @@ from gtts import gTTS
|
|
| 5 |
import os
|
| 6 |
import tempfile
|
| 7 |
import speech_recognition as sr
|
| 8 |
-
from googletrans import Translator
|
| 9 |
|
| 10 |
def upload_video():
|
| 11 |
st.subheader("Paso 1: Cargar archivo MP4")
|
| 12 |
uploaded_file = st.file_uploader("Seleccione un archivo MP4", type=["mp4"])
|
| 13 |
return uploaded_file
|
| 14 |
|
| 15 |
-
def select_language():
|
| 16 |
-
st.subheader("Seleccione el idioma:")
|
| 17 |
-
language_options = ["español", "inglés"]
|
| 18 |
-
selected_language = st.selectbox("Idioma", language_options)
|
| 19 |
-
return selected_language
|
| 20 |
-
|
| 21 |
-
def translate_to_english(text, language):
|
| 22 |
-
if language == "español":
|
| 23 |
-
translator = Translator()
|
| 24 |
-
return translator.translate(text, dest='en').text
|
| 25 |
-
else:
|
| 26 |
-
return text
|
| 27 |
-
|
| 28 |
def extract_and_convert_audio(video_file):
|
| 29 |
st.subheader("Paso 2: Extraer audio y convertir a MP3")
|
| 30 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video:
|
|
@@ -40,7 +26,7 @@ def extract_and_convert_audio(video_file):
|
|
| 40 |
return text_temp_file, audio_path, video.duration, temp_video_path
|
| 41 |
|
| 42 |
def audio_to_text(audio_path):
|
| 43 |
-
st.subheader("Paso
|
| 44 |
audio = AudioSegment.from_file(audio_path)
|
| 45 |
temp_wav_file = "temp_audio.wav"
|
| 46 |
audio.export(temp_wav_file, format="wav")
|
|
@@ -63,19 +49,18 @@ def audio_to_text(audio_path):
|
|
| 63 |
os.remove(temp_wav_file)
|
| 64 |
|
| 65 |
def save_text_to_temp_file(text):
|
| 66 |
-
st.subheader("Paso
|
| 67 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="w", encoding="utf-8") as temp_text_file:
|
| 68 |
temp_text_file.write(text)
|
| 69 |
temp_text_file_path = temp_text_file.name
|
| 70 |
return temp_text_file_path
|
| 71 |
|
| 72 |
-
def generate_gtts(text_temp_file, duration
|
| 73 |
-
st.subheader("Paso
|
| 74 |
with open(text_temp_file, "r", encoding="utf-8") as text_file:
|
| 75 |
text_for_gtts = text_file.read()
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
tts = gTTS(text_for_gtts, lang=lang_code)
|
| 79 |
tts_duration = len(AudioSegment.from_mp3(audio_path)) / 1000
|
| 80 |
tts.speed = duration / tts_duration
|
| 81 |
tts.save("gtts_audio.mp3")
|
|
@@ -86,15 +71,7 @@ def merge_video_with_audio(video_file, audio_file, result_path):
|
|
| 86 |
video = VideoFileClip(video_file)
|
| 87 |
audio = AudioFileClip(audio_file)
|
| 88 |
video = video.set_audio(audio)
|
| 89 |
-
|
| 90 |
-
if not os.path.exists(result_directory):
|
| 91 |
-
os.makedirs(result_directory)
|
| 92 |
-
|
| 93 |
-
try:
|
| 94 |
-
video.write_videofile(result_path, codec='libx264', audio_codec='aac')
|
| 95 |
-
st.success("Archivo de video creado con éxito en: {}".format(result_path))
|
| 96 |
-
except Exception as e:
|
| 97 |
-
st.error("Error al escribir el archivo de video: {}".format(e))
|
| 98 |
|
| 99 |
return result_path
|
| 100 |
|
|
@@ -102,10 +79,8 @@ st.title("Doblado GTTS de Video")
|
|
| 102 |
uploaded_file = upload_video()
|
| 103 |
|
| 104 |
if uploaded_file:
|
| 105 |
-
selected_language = select_language()
|
| 106 |
text_temp_file, audio_path, duration, temp_video_path = extract_and_convert_audio(uploaded_file)
|
| 107 |
-
|
| 108 |
-
gtts_audio_path = generate_gtts(translated_text, duration, selected_language)
|
| 109 |
result_path = merge_video_with_audio(temp_video_path, gtts_audio_path, "video_con_audio.mp4")
|
| 110 |
|
| 111 |
st.success("Proceso completado.")
|
|
@@ -113,4 +88,4 @@ if uploaded_file:
|
|
| 113 |
video_file = open(result_path, 'rb')
|
| 114 |
video_bytes = video_file.read()
|
| 115 |
st.video(video_bytes)
|
| 116 |
-
video_file.close()
|
|
|
|
| 5 |
import os
|
| 6 |
import tempfile
|
| 7 |
import speech_recognition as sr
|
|
|
|
| 8 |
|
| 9 |
def upload_video():
|
| 10 |
st.subheader("Paso 1: Cargar archivo MP4")
|
| 11 |
uploaded_file = st.file_uploader("Seleccione un archivo MP4", type=["mp4"])
|
| 12 |
return uploaded_file
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def extract_and_convert_audio(video_file):
|
| 15 |
st.subheader("Paso 2: Extraer audio y convertir a MP3")
|
| 16 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video:
|
|
|
|
| 26 |
return text_temp_file, audio_path, video.duration, temp_video_path
|
| 27 |
|
| 28 |
def audio_to_text(audio_path):
|
| 29 |
+
st.subheader("Paso intermedio: Convertir audio a texto temporal")
|
| 30 |
audio = AudioSegment.from_file(audio_path)
|
| 31 |
temp_wav_file = "temp_audio.wav"
|
| 32 |
audio.export(temp_wav_file, format="wav")
|
|
|
|
| 49 |
os.remove(temp_wav_file)
|
| 50 |
|
| 51 |
def save_text_to_temp_file(text):
|
| 52 |
+
st.subheader("Paso intermedio: Guardar texto en archivo temporal")
|
| 53 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="w", encoding="utf-8") as temp_text_file:
|
| 54 |
temp_text_file.write(text)
|
| 55 |
temp_text_file_path = temp_text_file.name
|
| 56 |
return temp_text_file_path
|
| 57 |
|
| 58 |
+
def generate_gtts(text_temp_file, duration):
|
| 59 |
+
st.subheader("Paso 3: Generar GTTS desde el texto")
|
| 60 |
with open(text_temp_file, "r", encoding="utf-8") as text_file:
|
| 61 |
text_for_gtts = text_file.read()
|
| 62 |
+
|
| 63 |
+
tts = gTTS(text_for_gtts, lang="es")
|
|
|
|
| 64 |
tts_duration = len(AudioSegment.from_mp3(audio_path)) / 1000
|
| 65 |
tts.speed = duration / tts_duration
|
| 66 |
tts.save("gtts_audio.mp3")
|
|
|
|
| 71 |
video = VideoFileClip(video_file)
|
| 72 |
audio = AudioFileClip(audio_file)
|
| 73 |
video = video.set_audio(audio)
|
| 74 |
+
video.write_videofile(result_path, codec='libx264', audio_codec='aac')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
return result_path
|
| 77 |
|
|
|
|
| 79 |
uploaded_file = upload_video()
|
| 80 |
|
| 81 |
if uploaded_file:
|
|
|
|
| 82 |
text_temp_file, audio_path, duration, temp_video_path = extract_and_convert_audio(uploaded_file)
|
| 83 |
+
gtts_audio_path = generate_gtts(text_temp_file, duration)
|
|
|
|
| 84 |
result_path = merge_video_with_audio(temp_video_path, gtts_audio_path, "video_con_audio.mp4")
|
| 85 |
|
| 86 |
st.success("Proceso completado.")
|
|
|
|
| 88 |
video_file = open(result_path, 'rb')
|
| 89 |
video_bytes = video_file.read()
|
| 90 |
st.video(video_bytes)
|
| 91 |
+
video_file.close()
|