Edge-TTS是由微軟推出的文本轉(zhuǎn)語音Python庫,通過微軟Azure Cognitive Services轉(zhuǎn)化文本為自然語音??梢宰鳛楦顿M(fèi)文本轉(zhuǎn)語音TTS服務(wù)的替代品,Edge-TTS支持40多種語言和300種聲音,提供優(yōu)質(zhì)的語音輸出 。edge-tts支持英語、漢語、日語、韓語、法語等40多種語言,共300多種可選聲音。 首先,通過Python包管理工具pip來安裝Edge-TTS庫。只需在命令行中輸入以下命令:pip install edge-tts。 然后,可以通過cmd命令:edge-tts --list-voices 來查看所有的語音,其中美式英語的語音有: Name: en-US-AnaNeural Gender: Female Name: en-US-AndrewMultilingualNeural Gender: Male Name: en-US-AndrewNeural Gender: Male Name: en-US-AriaNeural Gender: Female Name: en-US-AvaMultilingualNeural Gender: Female Name: en-US-AvaNeural Gender: Female Name: en-US-BrianMultilingualNeural Gender: Male Name: en-US-BrianNeural Gender: Male Name: en-US-ChristopherNeural Gender: Male Name: en-US-EmmaMultilingualNeural Gender: Female Name: en-US-EmmaNeural Gender: Female Name: en-US-EricNeural Gender: Male Name: en-US-GuyNeural Gender: Male Name: en-US-JennyNeural Gender: Female Name: en-US-MichelleNeural Gender: Female Name: en-US-RogerNeural Gender: Male Name: en-US-SteffanNeural Gender: Male 下載英語電子書或者其他英語文本文檔等資料,選好英語語音后,在deepseek中輸入提示詞: 你是一個(gè)Python編程專家,寫一個(gè)tts文本轉(zhuǎn)語音的Python腳本,具體步驟如下: 讀取文件夾“F:\aivideo”里面的txt文本文檔; 調(diào)用Edge-TTS庫將txt文本轉(zhuǎn)換成語音,語音格式為mp3,保存到同一個(gè)文件夾中, 具體參數(shù):Gender為女性,Name為:en-US-AriaNeural,語速為:-30%; 注意: 每一步都要輸出信息 使用 asyncio.run 來運(yùn)行異步函數(shù),自動(dòng)管理事件循環(huán)的創(chuàng)建和關(guān)閉。 使用 chardet 庫來檢測(cè)Txt文件編碼格式,然后使用對(duì)應(yīng)的編碼來讀取文件; Txt文件中可能包含非 UTF-8 編碼的字符, 可以使用 errors='ignore' 參數(shù)來忽略這些錯(cuò)誤字符。 源代碼: import os import asyncio import edge_tts import sys async def text_to_speech(text, output_file): communicate = edge_tts.Communicate(text, "en-US-AriaNeural", rate="-30%") await communicate.save(output_file) def process_files(folder_path): for filename in os.listdir(folder_path): if filename.endswith(".txt"): txt_file_path = os.path.join(folder_path, filename) mp3_file_path = os.path.join(folder_path, os.path.splitext(filename)[0] + ".mp3") print(f"Reading text from {txt_file_path}") with open(txt_file_path, 'r', encoding='utf-8') as file: text = file.read() print(f"Converting text to speech and saving to {mp3_file_path}") asyncio.run(text_to_speech(text, mp3_file_path)) print(f"Conversion complete for {txt_file_path}") if __name__ == "__main__": folder_path = r"F:\aivideo" print(f"Processing files in {folder_path}") if sys.platform == "win32": asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) process_files(folder_path) print("All files processed.") |
|