taiage/md_a_audio.py

62 lines
1.4 KiB
Python

import os
import re
from gtts import gTTS
def limpiar_markdown(texto):
"""Elimina sintaxis básica de markdown para que el audio suene natural."""
texto = re.sub(r'#', '', texto)
texto = re.sub(r'\[(.*?)\]\(.*?\)', r'\1', texto)
texto = texto.replace("**", "")
texto = texto.replace("*", "")
texto = re.sub(r'```.*?```', '', texto, flags=re.DOTALL)
texto = texto.replace('\n', ' ')
return texto
def convertir_md_a_audio(md_file):
base_name = os.path.splitext(md_file)[0]
audio_output = f"{base_name}.mp3"
print(f"\n--- Procesando {md_file} ---")
try:
with open(md_file, "r", encoding="utf-8") as f:
texto = f.read()
texto = limpiar_markdown(texto)
if len(texto.strip()) < 10:
print("⚠️ No hay texto suficiente.")
return
print(f"Texto leído ({len(texto)} caracteres)")
tts = gTTS(text=texto, lang="es")
tts.save(audio_output)
print(f"✅ Audio generado: {audio_output}")
except Exception as e:
print(f"❌ Error procesando {md_file}: {e}")
def procesar_carpeta():
archivos = sorted([f for f in os.listdir() if f.endswith(".md")])
if not archivos:
print("No se encontraron archivos .md")
return
print(f"Encontrados {len(archivos)} archivos Markdown\n")
for md in archivos:
convertir_md_a_audio(md)
if __name__ == "__main__":
procesar_carpeta()