pyttsx3 在将大文件转换为 mp3 时挂起
pyttsx3 hangs up on converting big files to mp3
代码:
import os
import glob
import pyttsx3
engine = pyttsx3.init()
file_pattern = '*.txt'
file_name_list = []
if '*' in file_pattern:
file_name_list.extend(glob.glob(file_pattern))
# speech rate
engine.setProperty('rate', 600)
rate = engine.getProperty('rate')
# speech volume
volume = engine.getProperty('volume')
engine.setProperty('volume',1.0)
# speech voice
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.runAndWait() # engine start
for target in file_name_list:
string_to_parse = ''
with open(target,'r',encoding='utf-8', errors='ignore') as text_file:
for i in text_file.readlines():
string_to_parse = string_to_parse + i
mp3_filename = target.replace('.txt','.mp3')
fullPath = os.path.join(os.getcwd(), mp3_filename)
engine.save_to_file(string_to_parse, fullPath)
engine.runAndWait()
问题:
此代码可以将一个包含一个字符串的小文件的文件夹写入 mp3,但如果我尝试转换多本书,它就会永远挂起。我能做什么?
你正在写一行你应该退出 for 循环并在变量 string_to_parse 中包含完整的句子然后你可以将它保存到 mp3
import os
import glob
import pyttsx3
engine = pyttsx3.init()
file_pattern = '*.txt'
file_name_list = []
if '*' in file_pattern:
file_name_list.extend(glob.glob(file_pattern))
# speech rate
engine.setProperty('rate', 600)
rate = engine.getProperty('rate')
# speech volume
volume = engine.getProperty('volume')
engine.setProperty('volume',1.0)
# speech voice
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.runAndWait() # engine start
for target in file_name_list:
string_to_parse = ''
with open(target,'r',encoding='utf-8', errors='ignore') as text_file:
for i in text_file.readlines():
string_to_parse = string_to_parse + i
mp3_filename = target.replace('.txt','.mp3')
fullPath = os.path.join(os.getcwd(), mp3_filename)
engine.save_to_file(string_to_parse, fullPath)
engine.runAndWait()
代码:
import os
import glob
import pyttsx3
engine = pyttsx3.init()
file_pattern = '*.txt'
file_name_list = []
if '*' in file_pattern:
file_name_list.extend(glob.glob(file_pattern))
# speech rate
engine.setProperty('rate', 600)
rate = engine.getProperty('rate')
# speech volume
volume = engine.getProperty('volume')
engine.setProperty('volume',1.0)
# speech voice
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.runAndWait() # engine start
for target in file_name_list:
string_to_parse = ''
with open(target,'r',encoding='utf-8', errors='ignore') as text_file:
for i in text_file.readlines():
string_to_parse = string_to_parse + i
mp3_filename = target.replace('.txt','.mp3')
fullPath = os.path.join(os.getcwd(), mp3_filename)
engine.save_to_file(string_to_parse, fullPath)
engine.runAndWait()
问题:
此代码可以将一个包含一个字符串的小文件的文件夹写入 mp3,但如果我尝试转换多本书,它就会永远挂起。我能做什么?
你正在写一行你应该退出 for 循环并在变量 string_to_parse 中包含完整的句子然后你可以将它保存到 mp3
import os
import glob
import pyttsx3
engine = pyttsx3.init()
file_pattern = '*.txt'
file_name_list = []
if '*' in file_pattern:
file_name_list.extend(glob.glob(file_pattern))
# speech rate
engine.setProperty('rate', 600)
rate = engine.getProperty('rate')
# speech volume
volume = engine.getProperty('volume')
engine.setProperty('volume',1.0)
# speech voice
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.runAndWait() # engine start
for target in file_name_list:
string_to_parse = ''
with open(target,'r',encoding='utf-8', errors='ignore') as text_file:
for i in text_file.readlines():
string_to_parse = string_to_parse + i
mp3_filename = target.replace('.txt','.mp3')
fullPath = os.path.join(os.getcwd(), mp3_filename)
engine.save_to_file(string_to_parse, fullPath)
engine.runAndWait()