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

你可能感兴趣的:(python,python)