Spaces:
Build error
Build error
| 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() | |