随笔 - 633,  文章 - 0,  评论 - 13,  阅读 - 48万
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

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);
复制代码

 

posted on   大话人生  阅读(357)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2020-03-03 文件下载漏洞
2020-03-03 URL攻击
2020-03-03 点击劫持(UI覆盖攻击)
2020-03-03 csrf攻击
点击右上角即可分享
微信分享提示