-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
77 lines (60 loc) · 2.83 KB
/
Copy pathmain.py
File metadata and controls
77 lines (60 loc) · 2.83 KB
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
import subprocess
import os
import glob
# --- CONFIGURAÇÃO DE DIRETÓRIOS ---
PASTA_ENTRADA = "input_textos"
PASTA_SAIDA = "output_audios"
def inicializar_pastas():
# Cria as pastas automaticamente caso não existam
os.makedirs(PASTA_ENTRADA, exist_ok=True)
os.makedirs(PASTA_SAIDA, exist_ok=True)
def processar_lote_arquivos():
modelo_voz = "pt_BR-faber-medium.onnx"
caminho_piper = os.path.join(os.getcwd(), 'venv', 'Scripts', 'piper.exe')
# Validação da infraestrutura
if not os.path.exists(caminho_piper):
print(f"ERRO CRÍTICO: Piper não encontrado em: {caminho_piper}")
return
# Busca todos os arquivos .txt na pasta de entrada
arquivos_txt = glob.glob(os.path.join(PASTA_ENTRADA, "*.txt"))
if not arquivos_txt:
print(f"Status: Nenhum arquivo .txt pendente na pasta '{PASTA_ENTRADA}'.")
return
print(f"--- Iniciando conversão em lote: {len(arquivos_txt)} arquivo(s) ---")
for arquivo_texto in arquivos_txt:
nome_base = os.path.basename(arquivo_texto).replace('.txt', '')
arquivo_saida_mp3 = os.path.join(PASTA_SAIDA, f"{nome_base}.mp3")
temp_wav = os.path.join(PASTA_SAIDA, f"{nome_base}_temp.wav")
print(f"Processando: {nome_base} ...")
# 1. Leitura UTF-8
try:
with open(arquivo_texto, "r", encoding="utf-8") as f:
texto_bruto = f.read()
except Exception as e:
print(f" [Erro na leitura] {e}")
continue
# 2. Síntese Piper
comando_piper = [caminho_piper, '--model', modelo_voz, '--output_file', temp_wav]
try:
processo = subprocess.Popen(comando_piper, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, text=False)
processo.communicate(input=texto_bruto.encode('utf-8'))
except Exception as e:
print(f" [Erro no motor Piper] {e}")
continue
# 3. Conversão FFmpeg e Limpeza
if os.path.exists(temp_wav):
comando_ffmpeg = f'ffmpeg -y -i "{temp_wav}" -codec:a libmp3lame -qscale:a 2 "{arquivo_saida_mp3}"'
try:
subprocess.run(comando_ffmpeg, shell=True, check=True, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
os.remove(temp_wav) # Remove o temporário
# Marca o txt como concluído para não processar novamente na próxima execução
os.rename(arquivo_texto, arquivo_texto + ".concluido")
print(f" -> Sucesso! MP3 salvo em: {PASTA_SAIDA}\\{nome_base}.mp3")
except Exception as e:
print(f" [Erro na conversão FFmpeg] {e}")
if __name__ == "__main__":
inicializar_pastas()
processar_lote_arquivos()
print("\nRotina finalizada.")