Spaces:
Build error
Build error
La app en questión.
Browse files
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from moviepy.editor import VideoFileClip
|
| 4 |
+
import speech_recognition as sr
|
| 5 |
+
import noisereduce as nr
|
| 6 |
+
import pydub
|
| 7 |
+
|
| 8 |
+
def extract_audio(video_path):
|
| 9 |
+
"""Extrae el audio del video y devuelve la ruta del archivo de audio."""
|
| 10 |
+
audio_path = "temp_audio.wav"
|
| 11 |
+
video = VideoFileClip(video_path)
|
| 12 |
+
audio = video.audio
|
| 13 |
+
audio.write_audiofile(audio_path)
|
| 14 |
+
audio.close()
|
| 15 |
+
video.close()
|
| 16 |
+
return audio_path
|
| 17 |
+
|
| 18 |
+
def split_audio(audio_path, segment_duration=10):
|
| 19 |
+
"""Divide el audio en segmentos más pequeños y devuelve la lista de rutas."""
|
| 20 |
+
audio = pydub.AudioSegment.from_wav(audio_path)
|
| 21 |
+
segments = []
|
| 22 |
+
for i in range(0, len(audio), segment_duration * 1000):
|
| 23 |
+
segment = audio[i:i + segment_duration * 1000]
|
| 24 |
+
segment_path = f'segment_{i // 1000}.wav'
|
| 25 |
+
segment.export(segment_path, format='wav')
|
| 26 |
+
segments.append(segment_path)
|
| 27 |
+
return segments
|
| 28 |
+
|
| 29 |
+
def convert_audio_to_text(audio_path):
|
| 30 |
+
"""Convierte el audio a texto utilizando SpeechRecognition."""
|
| 31 |
+
recognizer = sr.Recognizer()
|
| 32 |
+
with sr.AudioFile(audio_path) as source:
|
| 33 |
+
audio_data = recognizer.record(source)
|
| 34 |
+
# Reduce el ruido (ajusta según sea necesario)
|
| 35 |
+
audio_data = nr.reduce_noise(y=audio_data, sr=16000)
|
| 36 |
+
try:
|
| 37 |
+
text = recognizer.recognize_google(audio_data, language='es-ES')
|
| 38 |
+
return text
|
| 39 |
+
except sr.UnknownValueError:
|
| 40 |
+
return "[No se pudo reconocer el audio]"
|
| 41 |
+
except sr.RequestError:
|
| 42 |
+
return "[Error en la solicitud]"
|
| 43 |
+
|
| 44 |
+
def generate_subtitles(video_path):
|
| 45 |
+
"""Genera subtítulos a partir de un video."""
|
| 46 |
+
audio_path = extract_audio(video_path)
|
| 47 |
+
segments = split_audio(audio_path)
|
| 48 |
+
subtitles = []
|
| 49 |
+
|
| 50 |
+
for index, segment in enumerate(segments):
|
| 51 |
+
text = convert_audio_to_text(segment)
|
| 52 |
+
# Agregar el texto de cada segmento a la lista de subtítulos
|
| 53 |
+
subtitles.append((index * 10, (index + 1) * 10, text)) # Asumiendo segmentos de 10 segundos
|
| 54 |
+
|
| 55 |
+
# Generar el archivo de subtítulos SRT
|
| 56 |
+
srt_path = "subtitles.srt"
|
| 57 |
+
with open(srt_path, 'w') as f:
|
| 58 |
+
for i, (start, end, subtitle) in enumerate(subtitles):
|
| 59 |
+
f.write(f"{i + 1}\n")
|
| 60 |
+
f.write(f"00:00:{start:02},000 --> 00:00:{end:02},000\n")
|
| 61 |
+
f.write(f"{subtitle}\n\n")
|
| 62 |
+
|
| 63 |
+
# Limpiar los segmentos temporales
|
| 64 |
+
for segment in segments:
|
| 65 |
+
os.remove(segment)
|
| 66 |
+
os.remove(audio_path)
|
| 67 |
+
|
| 68 |
+
return srt_path
|
| 69 |
+
|
| 70 |
+
# Crear la interfaz de Gradio
|
| 71 |
+
iface = gr.Interface(
|
| 72 |
+
fn=generate_subtitles,
|
| 73 |
+
inputs=gr.inputs.File(label="Sube tu video"),
|
| 74 |
+
outputs=gr.outputs.File(label="Descargar subtítulos"),
|
| 75 |
+
title="Generador de Subtítulos",
|
| 76 |
+
description="Convierte el audio de un video en subtítulos automáticamente."
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
if __name__ == "__main__":
|
| 80 |
+
iface.launch()
|