-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxt2voice.py
57 lines (44 loc) · 1.36 KB
/
txt2voice.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from pathlib import Path
import pyttsx3
# 输入文件
input_file = Path('content.txt')
# 输出文件夹
output_dir = Path('out')
def process_voice():
# 读入文件
with open(input_file, 'r', encoding='utf8') as f:
lines = f.readlines()
# 初始化TTS引擎
tts = pyttsx3.init()
tts.setProperty('rate', 150)
tts.setProperty('volume', 1.0)
voices = tts.getProperty('voices')
for voice in voices:
print(voice)
for line in lines:
line = line.replace('\n', '')
line = line.replace('\r', '')
# 分割每行
info = line.split('\t')
no = info[0]
text = info[1]
# 建立输出文件夹
outdirname = 'DM' + ('000' + no)[-3:]
outdir = output_dir.joinpath(outdirname)
if not outdir.exists():
outdir.mkdir(parents=True)
# 输出文件夹路径
outfile = outdir.joinpath('1.mp3')
# 转换为语音保存至mp3文件中
print(text, outfile)
tts.save_to_file(text, str(outfile))
tts.runAndWait()
# 输出文件夹路径
outfile = output_dir.joinpath('init.mp3')
text = '欢迎使用必利优科技的RGV小车'
# 转换为语音保存至mp3文件中
print(text, outfile)
tts.save_to_file(text, str(outfile))
tts.runAndWait()
if __name__ == "__main__":
process_voice()