59 lines
1.9 KiB
Python
59 lines
1.9 KiB
Python
import asyncio
|
|
import edge_tts
|
|
import re
|
|
import subprocess # Cambiado para mayor seguridad
|
|
from pathlib import Path
|
|
|
|
def limpiar_markdown(texto):
|
|
texto = re.sub(r'```.*?```', ' [código] ', texto, flags=re.DOTALL)
|
|
texto = re.sub(r'\|.*?\|', '', texto)
|
|
texto = re.sub(r'[#*_~`>]', '', texto)
|
|
return ' '.join(texto.split())
|
|
|
|
async def convertir_archivo(path_md):
|
|
temp_output = path_md.with_suffix('.temp.mp3')
|
|
final_output = path_md.with_suffix('.mp3')
|
|
nombre_tema = path_md.stem.replace('_', ' ').capitalize()
|
|
|
|
texto = path_md.read_text(encoding="utf-8")
|
|
texto_limpio = limpiar_markdown(texto)
|
|
|
|
# 1. Generar audio
|
|
comunicar = edge_tts.Communicate(texto_limpio, "es-ES-AlvaroNeural")
|
|
await comunicar.save(str(temp_output))
|
|
|
|
# 2. Normalización con FFmpeg usando subprocess
|
|
comando = [
|
|
'ffmpeg', '-i', str(temp_output),
|
|
'-codec:a', 'libmp3lame', '-b:a', '192k', '-ar', '44100',
|
|
'-metadata', f'title={nombre_tema}',
|
|
'-id3v2_version', '3', '-write_id3v1', '1',
|
|
'-y', str(final_output)
|
|
]
|
|
|
|
try:
|
|
# Ejecutar sin mostrar la molesta ventana negra de ffmpeg
|
|
subprocess.run(comando, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
except FileNotFoundError:
|
|
print(f"❌ Error: No se encontró 'ffmpeg'. Asegúrate de que esté instalado.")
|
|
return
|
|
|
|
if temp_output.exists():
|
|
temp_output.unlink()
|
|
|
|
print(f"✅ Listo: {final_output.name} (Título: {nombre_tema})")
|
|
|
|
async def main():
|
|
# Asegúrate de que la carpeta 'bloque1' existe relativa a donde ejecutas el script
|
|
archivos = sorted(Path('bloque1').glob('*.md'))
|
|
if not archivos:
|
|
print("No se encontraron archivos .md en la carpeta 'bloque1'")
|
|
return
|
|
|
|
for md in archivos:
|
|
print(f"Procesando: {md.name}...")
|
|
await convertir_archivo(md)
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|