Created
May 21, 2024 01:15
-
-
Save pixelead0/d01a7a80bfeafaf0592acf61de35f72a to your computer and use it in GitHub Desktop.
transcribe_audio_to_text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from pydub import AudioSegment | |
import speech_recognition as sr | |
print("# Define las rutas") | |
path = '/app/audio/a00' | |
audio_file_path = f"{path}.m4a" | |
output_directory = '/app/audio/segments' | |
print("# Crear el directorio de salida si no existe") | |
os.makedirs(output_directory, exist_ok=True) | |
print("# Cargar el archivo de audio") | |
audio = AudioSegment.from_file(audio_file_path) | |
print("# Duración del segmento en milisegundos (10 minutos)") | |
segment_duration = 1 * 60 * 1000 | |
print("# Inicializar el reconocedor") | |
recognizer = sr.Recognizer() | |
print("# Dividir el archivo de audio en segmentos y procesar cada uno") | |
for i, start_time in enumerate(range(0, len(audio), segment_duration)): | |
end_time = start_time + segment_duration | |
segment = audio[start_time:end_time] | |
print("# Guardar el segmento temporalmente") | |
segment_path = os.path.join(output_directory, f'segment_{i+1}.wav') | |
segment.export(segment_path, format='wav') | |
print(f"Segmento {i+1} guardado en {segment_path}") | |
print("# Procesar el segmento") | |
try: | |
with sr.AudioFile(segment_path) as source: | |
audio_data = recognizer.record(source) | |
print("# Transcribir el audio a texto") | |
try: | |
text = recognizer.recognize_google(audio_data, language='es-ES') | |
print(f"Transcripción del segmento {i+1}:") | |
print(text) | |
print("# Guardar la transcripción en un archivo de texto") | |
transcription_path = os.path.join(output_directory, f'transcription_{i+1}.txt') | |
with open(transcription_path, 'w') as f: | |
f.write(text) | |
except sr.UnknownValueError: | |
print(f"No se pudo entender el segmento {i+1}.") | |
except sr.RequestError: | |
print(f"No se pudo procesar la solicitud para el segmento {i+1}.") | |
except FileNotFoundError: | |
print(f"El segmento {i+1} no se encuentra.") | |
except Exception as e: | |
print(f"Error al procesar el segmento {i+1}: {e}") | |
print("Procesamiento completado.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Capaz que agregaria mostrar una primer parte del texto resultado :) Muy buen script 💯