File size: 2,824 Bytes
da7fa56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import gradio as gr
import os
from moviepy.editor import VideoFileClip
import speech_recognition as sr
import noisereduce as nr
import pydub

def extract_audio(video_path):
    """Extrae el audio del video y devuelve la ruta del archivo de audio."""
    audio_path = "temp_audio.wav"
    video = VideoFileClip(video_path)
    audio = video.audio
    audio.write_audiofile(audio_path)
    audio.close()
    video.close()
    return audio_path

def split_audio(audio_path, segment_duration=10):
    """Divide el audio en segmentos más pequeños y devuelve la lista de rutas."""
    audio = pydub.AudioSegment.from_wav(audio_path)
    segments = []
    for i in range(0, len(audio), segment_duration * 1000):
        segment = audio[i:i + segment_duration * 1000]
        segment_path = f'segment_{i // 1000}.wav'
        segment.export(segment_path, format='wav')
        segments.append(segment_path)
    return segments

def convert_audio_to_text(audio_path):
    """Convierte el audio a texto utilizando SpeechRecognition."""
    recognizer = sr.Recognizer()
    with sr.AudioFile(audio_path) as source:
        audio_data = recognizer.record(source)
        # Reduce el ruido (ajusta según sea necesario)
        audio_data = nr.reduce_noise(y=audio_data, sr=16000)
        try:
            text = recognizer.recognize_google(audio_data, language='es-ES')
            return text
        except sr.UnknownValueError:
            return "[No se pudo reconocer el audio]"
        except sr.RequestError:
            return "[Error en la solicitud]"

def generate_subtitles(video_path):
    """Genera subtítulos a partir de un video."""
    audio_path = extract_audio(video_path)
    segments = split_audio(audio_path)
    subtitles = []

    for index, segment in enumerate(segments):
        text = convert_audio_to_text(segment)
        # Agregar el texto de cada segmento a la lista de subtítulos
        subtitles.append((index * 10, (index + 1) * 10, text))  # Asumiendo segmentos de 10 segundos

    # Generar el archivo de subtítulos SRT
    srt_path = "subtitles.srt"
    with open(srt_path, 'w') as f:
        for i, (start, end, subtitle) in enumerate(subtitles):
            f.write(f"{i + 1}\n")
            f.write(f"00:00:{start:02},000 --> 00:00:{end:02},000\n")
            f.write(f"{subtitle}\n\n")

    # Limpiar los segmentos temporales
    for segment in segments:
        os.remove(segment)
    os.remove(audio_path)

    return srt_path

# Crear la interfaz de Gradio
iface = gr.Interface(
    fn=generate_subtitles,
    inputs=gr.inputs.File(label="Sube tu video"),
    outputs=gr.outputs.File(label="Descargar subtítulos"),
    title="Generador de Subtítulos",
    description="Convierte el audio de un video en subtítulos automáticamente."
)

if __name__ == "__main__":
    iface.launch()