0%

Python实现扇贝精听

使用Python实现扇贝精听功能,可以对英文原著进行逐句精听,语音合成使用百度AI开放平台。

0ad61a4d6e408b10cb09f25680a77f12

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
58
from aip import AipSpeech
from pygame import mixer
import os
from pydub import AudioSegment
# import Play_mp3
from playsound import playsound
import nltk.tokenize as tk

doc = open("Harry Potter The Complete Collection (J.K. Rowling).txt", encoding="utf-8")
doc_txt = doc.read()
vo_text = []

# 分句
tokens = tk.sent_tokenize(doc_txt)
for token in tokens:
vo_text.append(token)


def shan_bei_vo(txtname, book_name):
""" 你的 APPID AK SK """
APP_ID = 'APP_ID' # 此处填写你的APP_ID
API_KEY = 'API_KEY' # 此处填写你的API_KEY
SECRET_KEY = 'SECRET_KEY' # 此处填写你的SECRET_KEY
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
result = client.synthesis(txtname, 'zh', 1, {'vol': 5, 'per': 3})
if not isinstance(result, dict):
# with open('{}/the_vo.mp3'.format(book_name), 'wb+') as f:
with open('{}/the_vo.mp3'.format(book_name), 'wb+') as f:
f.write(result)
playsound('Harry_Potter/the_vo.mp3')



if __name__ == '__main__':
num = 7
book_name = 'Harry_Potter'
while True:
print("第%d句" %(num+1))
txtname = vo_text[num]
shan_bei_vo(txtname, book_name)
choice = input("1.再听一遍 2.显示答案 3.下一句\n")
while int(choice) == 1:
playsound('Harry_Potter/the_vo.mp3')
choice = input("1.再听一遍 2.显示答案 3.下一句\n")
if int(choice) != 1:
break
if int(choice) == 2:
print(txtname)
choice = input("1.再听一遍 3.下一句\n")
while int(choice) == 1:
playsound('Harry_Potter/the_vo.mp3')
choice = input("1.再听一遍 3.下一句\n")
if int(choice) != 1:
break
if int(choice) == 3:
num += 1
os.remove("Harry_Potter/the_vo.mp3")