代码拉取完成,页面将自动刷新
import os
import asyncio
import threading
import edge_tts
from translator import BaiduTranslator
mp3_folder = 'engtohanmp3'
voice = 'zh-CN-YunxiNeural'
async def text_to_speech(text, filename):
tts = edge_tts.Communicate(text=text, voice=voice)
await tts.save(os.path.join(mp3_folder, filename + '.mp3'))
def translate_text(text):
translator = BaiduTranslator()
translation = translator.translate(text, 'zh')
return translation
def main():
# Check if MP3 folder exists, if not create it
if not os.path.exists(mp3_folder):
os.mkdir(mp3_folder)
loop = asyncio.get_event_loop()
files = os.listdir('engtohan/')
threads = []
for i, file in enumerate(files):
filename = str(i + 1)
with open(os.path.join('engtohan/', file), encoding='utf-8') as f:
text = f.read()
translated = translate_text(text)
thread = threading.Thread(target=asyncio.run,
args=(text_to_speech(translated, filename),))
threads.append(thread)
thread.start()
# Join all threads
for thread in threads:
thread.join()
# Close event loop
loop.close()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。