../
开发弹幕朗读器 ============== 2021-10-19 最近有成为透明无声系vTuber的打算,为了增加娱乐效果,搞了一个弹幕朗读器。因为不 想花太多时间,所以就选了Python,准备快速解决。 ## 如何朗读 我直接用了Windows操作系统内置的语音合成功能:SpVoice)。 用PyWin32可以调用该接口。 首先需要安装PyWin32: pip3 install pywin32 示例代码: import win32com.client speaker = win32com.client.Dispatch("SAPI.SpVoice") speaker.Speak("你好,世界!") ## 如何获取弹幕 Twitch的聊天系统比较有意思,他们在提供聊天机器人接口的竟然用的是IRC接口。 https://dev.twitch.tv/docs/irc/guide#connecting-to-twitch-irc 所以,你甚至可以用nc命令上去直接手工操作: [mistivia@arch ~]$ nc irc.chat.twitch.tv 6667 < PASS oauth:<Twitch OAuth token> < NICK <user> > :tmi.twitch.tv 001 <user> :Welcome, GLHF! > :tmi.twitch.tv 002 <user> :Your host is tmi.twitch.tv > :tmi.twitch.tv 003 <user> :This server is rather new > :tmi.twitch.tv 004 <user> :- > :tmi.twitch.tv 375 <user> :- > :tmi.twitch.tv 372 <user> :You are in a maze of twisty passages, all alike. > :tmi.twitch.tv 376 <user> :> JOIN #channel 注意登录之后的提示语: You are in a maze of twitsy passages,all alike. 这其实是一个彩蛋,来自70年代PDP-10计算机上的冒险游戏:Colossal Cave Adventure, 简称Adventure。这是世界上第一部交互式小说,也是第一部冒险游戏和文字冒险游戏。 这款游戏后来在Atari游戏机上还有一个改编的图形版:Adventure,被认为是历史上第一 个带彩蛋的游戏(就是《头号玩家》里面的那个)。 因为IRC是一个非常古老而成熟的文本协议,所以开发的时候只需要用Python上的一些IRC开源库即 可。Twitch官方就提供了一个Python2实现的机器人的例子。稍微改一改即 可使用在Python3上。 里面用到了一个Python的IRC库,需要提前安装上: pip3 install irc 最后的代码: # Windows Only # Dependencies: pip install pywin32 irc import win32com.client import irc.bot # your username username = "mistivia" botname = "mybot" # channel name, prepending '#' is a must channel = "#mistivia" # To get a token, visit: http://twitchapps.com/tmi/ token = "oauth:YOUR TOKEN HERE" server = "irc.chat.twitch.tv" port = 6667 speaker = win32com.client.Dispatch("SAPI.SpVoice") class TwitchBot(irc.bot.SingleServerIRCBot): def __init__(self, username, client_id, token, channel): self.client_id = client_id self.token = token self.channel = channel print('Connecting to ' + server + ' on port ' + str(port) + '...') irc.bot.SingleServerIRCBot.__init__( self, [(server, port, token)], username, username) def on_welcome(self, c, e): print('Joining ' + self.channel) # You must request specific capabilities before you can use them c.join(self.channel) def on_pubmsg(self, c, e): self.on_msg(e.source.nick, e.arguments[0]) def on_msg(self, nick, msg): print(nick + ": " + msg) speaker.Speak(nick + "说:" + msg) if __name__ == "__main__": bot = TwitchBot(username,botname, token, channel) bot.start() -------------------------------------------------------------------- Email: i (at) mistivia (dot) com