1.安装依赖
pip install pyttsx3 pip install librosa pip install pydub
2.代码程序
# encoding=utf8 #-*-coding:utf-8 -*- ''' python将一段文字转为mp3音频文件 ''' #pip install pyttsx3 # pip install librosa #pip install pydub import pyttsx3, librosa, os from pydub import AudioSegment # 实现方法: def txttomp3(text, savemp3): '''把文字为mp3格式并保存文件''' engine = pyttsx3.init(); engine.setProperty('rate', 150) # 调整语速 engine.setProperty('volume', 2.0) # 调整音量 voices = engine.getProperty('voices'); engine.setProperty('voice', voices[0].id); engine.say(text); engine.save_to_file(text, savemp3); engine.runAndWait(); # 播放音频 print("转换成功!" + savemp3); if __name__ == '__main__': with open("第二部分_第一章.txt",encoding="utf-8") as f: text = f.read() print(text) text_list = text.split("仅供学习参考. - Standard") text_list_len = len(text_list) print(text_list) print(text_list_len) for i in range(0,text_list_len): #要转为MP3的文字 one_text = text_list[i] # 保存MP3的目录路径 savemp3 = "第二部分_第一章_%s.mp3" % (i+1); # 调用方法 txttomp3(one_text, savemp3);
标签:engine,python,text,list,savemp3,MP3,install,pip,txt From: https://www.cnblogs.com/jingzaixin/p/17176867.html