Skip to content

Commit

Permalink
v2.3版本更新
Browse files Browse the repository at this point in the history
  • Loading branch information
kizx committed Apr 14, 2020
1 parent 639d644 commit 310ad20
Show file tree
Hide file tree
Showing 7 changed files with 842 additions and 503 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,4 @@ tempCodeRunnerFile.py
/build/
/dist/
sample.srt
*.mp3
8 changes: 4 additions & 4 deletions ali.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ def process(self, text, audio_name):
finally:
synthesizer.close()

def process_multithread(self, sub, name, sgn=None):
def process_multithread(self, sub, name, sgn=None, sleeptime=1):
thread_list = []
for index, i in enumerate(sub):
audio_name = f"{name}{index + 1}.mp3"
thread = threading.Thread(target=self.process, args=(i, audio_name))
thread_list.append(thread)
thread.start()
time.sleep(0.5)
time.sleep(sleeptime) # 阿里限制请求频率
if sgn:
sgn.progress_update.emit(index + 1)
num = len(sub)
Expand All @@ -90,5 +90,5 @@ def process_multithread(self, sub, name, sgn=None):
my_text = "今天天气不错"
my_audio_name = '阿里语音.mp3'
ali = Ali(ali_st)
# ali.process(my_text, my_audio_name)
ali.process_multithread(['这是第一句', '今天天气不错', '嘟嘟嘟嘟'], 'audio/')
ali.process(my_text, my_audio_name)
# ali.process_multithread(['这是第一句', '今天天气不错', '嘟嘟嘟嘟'], 'audio/')
69 changes: 38 additions & 31 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ def web(*url):
webbrowser.open(i)


def value_change(obj1, obj2):
obj2.setValue(obj1.value())


class MyQLine(QLineEdit):
"""实现文件拖放功能"""

Expand All @@ -42,7 +38,7 @@ def dropEvent(self, e):
self.setText(path)


class Stats(QObject):
class MainWindow(QObject):

def __init__(self):
super().__init__()
Expand Down Expand Up @@ -73,20 +69,6 @@ def __init__(self):
self.ui.file.clicked.connect(self.openfile)
self.ui.generate.clicked.connect(self.generate)

self.ui.spd_bd.valueChanged.connect(lambda: value_change(self.ui.spd_bd, self.ui.spd_spin_bd))
self.ui.spd_spin_bd.valueChanged.connect(lambda: value_change(self.ui.spd_spin_bd, self.ui.spd_bd))
self.ui.pit_bd.valueChanged.connect(lambda: value_change(self.ui.pit_bd, self.ui.pit_spin_bd))
self.ui.pit_spin_bd.valueChanged.connect(lambda: value_change(self.ui.pit_spin_bd, self.ui.pit_bd))
self.ui.vol_bd.valueChanged.connect(lambda: value_change(self.ui.vol_bd, self.ui.vol_spin_bd))
self.ui.vol_spin_bd.valueChanged.connect(lambda: value_change(self.ui.vol_spin_bd, self.ui.vol_bd))

self.ui.spd_ali.valueChanged.connect(lambda: value_change(self.ui.spd_ali, self.ui.spd_spin_ali))
self.ui.spd_spin_ali.valueChanged.connect(lambda: value_change(self.ui.spd_spin_ali, self.ui.spd_ali))
self.ui.pit_ali.valueChanged.connect(lambda: value_change(self.ui.pit_ali, self.ui.pit_spin_ali))
self.ui.pit_spin_ali.valueChanged.connect(lambda: value_change(self.ui.pit_spin_ali, self.ui.pit_ali))
self.ui.vol_ali.valueChanged.connect(lambda: value_change(self.ui.vol_ali, self.ui.vol_spin_ali))
self.ui.vol_spin_ali.valueChanged.connect(lambda: value_change(self.ui.vol_spin_ali, self.ui.vol_ali))

def note(self, info):
QMessageBox.information(self.ui, "提示", info)

Expand Down Expand Up @@ -141,6 +123,9 @@ def generate(self):
return
with open(srt_file, encoding='utf-8') as sub:
subtitle = list(srt.parse(sub))
for index, i in enumerate(subtitle[:]): # 删除空字幕块
if i.content.isspace() or i.content == '':
subtitle.remove(i)
file_path = os.path.dirname(srt_file)
if not os.path.exists(f'{file_path}/audio/'):
os.mkdir(f'{file_path}/audio/')
Expand Down Expand Up @@ -209,27 +194,49 @@ def ali_process(self, file_path, subtitle):
ali_setting = setting.get('ali', {})

ali = Ali(ali_setting, options)
sub = [i.content for i in subtitle]
self.progress = QProgressDialog("正在下载语音文件...", "请稍等", 0, 2 * len(sub), self.ui)
self.progress.setWindowTitle("操作中")
self.progress.setMinimumDuration(0)
self.progress.setWindowModality(Qt.WindowModal)
self.sgn = MySignal()
self.sgn.progress_update.connect(self.setprogress)
ali.process_multithread(sub, f'{file_path}/audio/', self.sgn)
self.corn(ali, file_path, subtitle, flag=1)
is_multi = self.ui.ali_multi.isChecked()
if is_multi: # 多线程
self.progress = QProgressDialog("正在下载语音文件...", "取消", 0, len(subtitle), self.ui)
self.progress.setWindowTitle("下载中")
self.progress.setMinimumDuration(0)
self.progress.setWindowModality(Qt.WindowModal)
self.sgn = MySignal()
self.sgn.progress_update.connect(self.setprogress)
sleeptime = self.ui.sleeptime.value()
ok = self.process_multithread(ali, subtitle, f'{file_path}/audio/', sleeptime=sleeptime)
if ok:
self.corn(ali, file_path, subtitle, flag=1)
else: # 单线程
self.corn(ali, file_path, subtitle)

def setprogress(self, value):
self.progress.setValue(value)

def process_multithread(self, api, sub, name, sleeptime=1):
thread_list = []
for index, i in enumerate(sub):
if self.progress.wasCanceled():
QMessageBox.warning(self.ui, "提示", "操作取消")
return 0
audio_name = f"{name}{i.index}.mp3"
thread = threading.Thread(target=api.process, args=(i.content, audio_name))
thread_list.append(thread)
thread.start()
time.sleep(sleeptime) # 阿里限制请求频率
if self.sgn:
self.sgn.progress_update.emit(index + 1)
for thread in thread_list:
thread.join()
return 1


class MySignal(QObject):
progress_update = Signal(int)


if __name__ == '__main__':
app = QApplication()
app = QApplication([])
app.setWindowIcon(QIcon('static/logo.png'))
stats = Stats()
stats.ui.show()
mainwindow = MainWindow()
mainwindow.ui.show()
app.exec_()
Binary file modified static/logo.ico
Binary file not shown.
Binary file modified static/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 310ad20

Please sign in to comment.