txt转mp3批量-python

# -*- coding: utf-8 -*-
#  #pip install gTTS
import time
import os
import json
import re
import urllib
from tkinter.filedialog import *
import requests
import base64
import tkinter as tk
# from gtts import gTTS
import pyttsx3
#pip install pyttsx3
#pip install librosa
#pip install pydub
# from pydub import AudioSegment
import urllib3
from win32com.client import Dispatch
from tkinter.messagebox import *
def main():
    content = t1.get(0.0, 'end')
    content2 = content.replace('\n', '')
    def get_file_content(filePath):
        with open(filePath, 'r',encoding='UTF-8') as fp:
            return fp.read()
    filetxtcontent = get_file_content(content2)
    text = filetxtcontent
    # tts = gTTS(text, lang='zh')
    now = time.strftime('%Y-%m-%d-%H-%M-%S')
    # tts.save("mywave%s.mp3"%now)
    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, "mymp3-%s.mp3"%now)
    engine.runAndWait()
def manytxt2mp3():
    showinfo('注意', '先选择utf8-txt位置.再选择输出mp3位置')
    workwhere2wzsbtxt=askdirectory(title='选择utf8-txt位置') + '/'
    workwhere3wzsbwbtp=askdirectory(title='选择mp3位置') + '/'
    def txt2mp3(filename, output_filename):
        def get_file_content(filePath):
            with open(filePath, 'r', encoding='UTF-8') as fp:
                return fp.read()

        filetxtcontent = get_file_content(filename)
        text = filetxtcontent
        # tts = gTTS(text, lang='zh')
        now = time.strftime('%Y-%m-%d-%H-%M-%S')
        # tts.save("mywave%s.mp3"%now)
        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, output_filename)
        engine.runAndWait()

    dirname = workwhere2wzsbtxt
    dirnameout = workwhere3wzsbwbtp
    filenames = os.listdir(dirname)
    for filename in filenames:
        if filename.endswith('txt')or filename.endswith('TXT') :
            base, ext = filename.split('.')
            new_name = base + '.mp3'
            filename = dirname + '/' + filename
            output_filename = dirnameout + '/' + new_name
            txt2mp3(filename, output_filename)
    start_directoryt2 = dirnameout.replace('/', '\\')
    print(dirnameout)
    os.system("explorer.exe %s" % start_directoryt2)

from tkinter import filedialog
def weizhi():
    file = filedialog.askopenfilename()
    t1.delete(0.0, 'end')
    t1.insert('end', file)
root=tk.Tk()
root.title('txt 2 mp3 by rogabet')
root.geometry()
l1=tk.Label(root,text='请选择txt位置:')
l1.grid()
t1=tk.Text(root,width=40,height=1)
t1.grid()
b1=tk.Button(root,text="1.选择单个 utf-8 编码文本",width=20,command=weizhi)
b1.grid(row=2,column=0)
b2=tk.Button(root,text="2.输出单个MP3",width=20,command=main)
b2.grid(row=3,column=0)
b3=tk.Button(root,text="批量输出MP3",width=20,command=manytxt2mp3)
b3.grid(row=4,column=0)
def qc():
    t1.delete(0.0,'end')
b3=tk.Button(root,text="清除输入框",width=15,command=qc)
b3.grid(row=6,column=0)
root.mainloop()

链2=https://pan.baidu.com/s/1q9YeexOUbGKi8X0tj9fjRw?pwd=0911
老罗补丁包和新包https://pan.baidu.com/s/1ZGB1MwL51L_bDkuqBhQuYQ?pwd=1029

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rogabet-note

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值