喜欢听小说的可以来了,用edge-tts转音频

查看 123|回复 10
作者:rangersxiaoyan   
本人喜欢听小说,用的是阅读+TTS Sever。奈何TTS延时太高。每一段话都有1秒左右。听着非常不爽。
于是自己就写了一个小说转音频。用的是微软的edge_tts模块
[Python] 纯文本查看 复制代码import asyncio
import aiohttp
import requests
from lxml import etree
from concurrent.futures import ThreadPoolExecutor, as_completed
from tqdm import tqdm
import os
import edge_tts
import asyncio
class biQuGe:
    def __init__(self):
        self.browser_header = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0'
        }
        self.novel_url = 'http://www.ibiquzw.org/24_24218/'
        self.timeout = 5
        self.chapter_info = []
        self.novel_name = None
        self.novel_txt_path = None
        self.novel_mp3_path = None
    def get_novel_info(self):
        r = requests.get(url=self.novel_url, headers=self.browser_header, timeout=self.timeout)
        r.encoding = r.apparent_encoding
        h = etree.HTML(r.text)
        chapter_name = h.xpath('//div[@id="list"]//a/text()')
        chapter_link = h.xpath('//div[@id="list"]//a/@href')
        self.novel_name = h.xpath('//h1/text()')[0]
        chapter_name = ['章 '.join(str(i).split('章')) for i in chapter_name]
        chapter_name = [' '.join(str(i).split()) for i in chapter_name]
        chapter_link = ['http://www.ibiquzw.org/' + i for i in chapter_link]
        chapter_info = [list(i) for i in zip(chapter_name[12:], chapter_link[12:])]
        for i in range(len(chapter_info)):
            xx = str(i + 1).zfill(4)
            chapter_info[i][0] = xx + "_" + str(chapter_info[i][0])
        self.chapter_info = chapter_info
    def get_chapter_content(self, info):
        async def run_tts(text: str, output: str, voice: str = 'zh-CN-YunxiNeural') -> None:
            communicate = edge_tts.Communicate(text, voice)
            await communicate.save(output)
        chapter_link = info[1]
        chapter_name = info[0]
        chapter_txt_path = self.novel_txt_path + chapter_name + '.txt'
        chapter_mp3_path = self.novel_mp3_path + chapter_name + '.mp3'
        if os.path.isfile(chapter_txt_path):
            return
        else:
            print('正在下载:', chapter_name)
            r = requests.get(url=chapter_link, headers=self.browser_header)
            r.encoding = r.apparent_encoding
            h = etree.HTML(r.text)
            data = h.xpath('//div[@id="content"]/text()')
            data = [''.join(str(i).split()) for i in data]
            data = [i for i in data if i != '']
            # data = [i.replace('|天才一秒记住言情小说s23us.com', '') for i in data]
            # print(data)
            text = ''
            if data:
                for i in data:
                    text += i
                    text += '\n'
                print('\t正在转换:', chapter_name)
                asyncio.run(run_tts(text, chapter_mp3_path))
                with open(file=chapter_txt_path, mode='w+', encoding='utf-8') as f:
                    for i in data:
                        f.write(i)
                        f.write('\n')
                print('\t\t转换成功:', chapter_name)
    def main(self):
        self.get_novel_info()
        self.novel_txt_path = './' + self.novel_name + '/txt/'
        self.novel_mp3_path = './' + self.novel_name + '/mp3/'
        os.makedirs(self.novel_txt_path, exist_ok=True)
        os.makedirs(self.novel_mp3_path, exist_ok=True)
        with ThreadPoolExecutor(max_workers=10) as executor:
            futures = [executor.submit(self.get_chapter_content, i) for i in self.chapter_info]
            for future in as_completed(futures):
                result = future.result()
if __name__ == "__main__":
    novel = biQuGe()
    novel.main()

的是, 来了

20230713G001133   

跟我做的这个有点像呢(把想说的粘贴进去按回车)
import pyttsx3
# 初始化语音引擎
engine = pyttsx3.init()
while True:
    # 获取用户输入的文本
    text = input()
    # 将用户输入的文本转换为语音
    engine.say(text)
    # 等待语音输出完成
    engine.runAndWait()
liangdaxia666   

感谢感谢,谢谢
Lxinghun   

哈哈,这不是阁子吗
zsj118106   

感谢楼主分享
SSKing29   

感谢很有用喜欢晚上听睡觉
Hacking2heart   

借鉴一下,感谢
xyc26   

感谢楼主分享
aliceaierlanta   

感觉不错
Brich01   

感谢楼主分享
您需要登录后才可以回帖 登录 | 立即注册

返回顶部