[2noise/ChatTTS]代码分享

2024-06-12 509 views
0
导入ChatTTS模块和soundfile模块

import ChatTTS import soundfile as sf import os

def text_to_mp3(text, filename): chat = ChatTTS.Chat() chat.load_models() wavs = chat.infer(text, use_decoder=True) sf.write(r"/文件路径/ChatTTS/{filename}.wav", wavs[0][0], 24000)

def split_and_convert_to_mp3(file_path, chunk_size=10000): with open(file_path, 'r', encoding='utf-8') as file: text = file.read() text_length = len(text) chunks = [text[i:i+chunk_size] for i in range(0, text_length, chunk_size)]

for i, chunk in enumerate(chunks, start=1):
    mp3_filename = f"output_{i}"
    text_to_mp3(chunk, mp3_filename)

if name == "main": file_path = "/path/test.txt" # 替换为你的文本文件路径 split_and_convert_to_mp3(file_path)

回答