java获取虎牙直播弹幕消息_虎牙直播弹幕转换字幕格式 基于Node.js 的 huya-danmu

1 首先安装nodejs运行环境, 从 http://nodejs.cn/download/ 下载对应的版本

2 安装 huya-danmu 模块, https://github.com/BacooTang/huya-danmu 有详细的安装方法

3 参照 huya-danmu 模块中 test.js 编写 huya.js 新文件

1 const huya_danmu = require('./index')2 var fs = require('fs')3

4 const roomid = process.argv[2]5 const client = newhuya_danmu(roomid)6 var t1 = newDate().getTime()7 var t2 =t18 var interval = 0

9 var logName = ''

10 t1 = process.uptime()*1000

11

12 var emots ={13 "/{dx" : "[大笑]",14 "/{sh" : "[送花]",15 "/{tx" : "[偷笑]",16 "/{dk" : "[大哭]",17 "/{hh" : "[嘿哈]",18 "/{66" : "[666]",19 "/{gd" : "[感动]",20 "/{yw" : "[疑问]",21 "/{xh" : "[喜欢]",22 "/{jx" : "[奸笑]",23 "/{zan" : "[赞]",24 "/{ka" : "[可爱]",25 "/{am" : "[傲慢]",26 "/{kx" : "[开心]",27 "/{88" : "[拜拜]",28 "/{hx" : "[害羞]",29 "/{zs" : "[衰]",30 "/{pu" : "[吐血]",31 "/{zc" : "[嘴馋]",32 "/{sq" : "[生气]",33 "/{fe" : "[扶额]",34 "/{bz" : "[闭嘴]",35 "/{kw" : "[枯萎]",36 "/{xu" : "[嘘]",37 "/{xk" : "[笑哭]",38 "/{lh" : "[流汗]",39 "/{bk" : "[不看]",40 "/{hq" : "[哈欠]",41 "/{tp" : "[调皮]",42 "/{gl" : "[鬼脸]",43 "/{cl" : "[戳脸]",44 "/{dg" : "[大哥]",45 "/{kun" : "[困]",46 "/{yb" : "[拥抱]",47 "/{zt" : "[猪头]",48 "/{kl" : "[骷髅]",49 "/{cc" : "[臭臭]",50 "/{xd" : "[心动]",51 "/{dao" : "[刀]"

52 }53

54 functioncheckemot(str)55 {56 if(str.includes('/{') == false ) returnstr57 for(var key inemots) {58 str = str.replace(new RegExp(key,'g'), emots[key])59 }60 returnstr61 }62

63 functionPFI(num)64 {65 if(num<10)66 return '0'+num67 else

68 returnnum69 }70

71 functionPFI3(num)72 {73 if(num<10) return '00'+num74 if(num<100) return '0'+num75 returnnum76 }77

78 functiongetNowFormatDate()79 {80 var date = newDate();81 var seperator1 = "";82 var seperator2 = "";83 var month = date.getMonth() + 1;84 var strDate =date.getDate();85 if (month >= 1 && month <= 9)86 {87 month = "0" +month;88 }89 if (strDate >= 0 && strDate <= 9)90 {91 strDate = "0" +strDate;92 }93

94 var strHH =date.getHours()95 var strMM =date.getMinutes()96 var strSS =date.getSeconds()97 if (strHH >= 0 && strHH <= 9) strHH = "0" +strHH;98 if (strMM >= 0 && strMM <= 9) strMM = "0" +strMM;99 if (strSS >= 0 && strSS <= 9) strSS = "0" +strSS;100

101 var currentdate = date.getFullYear() + seperator1 + month + seperator1 +strDate102 + "-" + strHH + seperator2 + strMM + seperator2 +strSS;103 returncurrentdate;104 }105

106 functioninit()107 {108 var savepath = process.argv[3]109 var savefn = process.argv[4]110 if(typeof(savefn)=='undefined')111 savefn = ""

112 else

113 savefn = "_" + savefn.replace(/ /g,"_")

114 if(typeof(savepath)=='undefined')115 logName = './'+getNowFormatDate()+'_'+roomid+savefn+'.LRC'

116 else

117 logName = savepath+'/'+getNowFormatDate()+'_'+roomid+savefn+'.LRC'

118 console.log(logName)119 fs.appendFile(logName,'[ti:'+getNowFormatDate()+ ']\n', function(err) {});120 fs.appendFile(logName,'[ar:'+savefn+']\n', function(err) {});121 fs.appendFile(logName,'[offset:0]\n', function(err) {});122 }123

124 init()125

126 client.on('connect', () =>{127 console.log(`已连接huya ${roomid}`)128 })129

130 client.on('message', msg =>{131 switch(msg.type) {132 case 'chat':133 //console.log(`[${msg.from.name}]:${msg.content}`)

134 t2 = process.uptime()*1000

135 interval = (t2 -t1)136 var date = newDate( interval )137 var HH =date.getUTCHours()138 var MM = PFI(date.getUTCMinutes()+HH*60)139 var SS =PFI(date.getUTCSeconds())140 var MS =PFI3(parseInt(date.getUTCMilliseconds()))141 var msg =`[${MM}:${SS}.${MS}] ${msg.content}`142 msg =checkemot(msg)143 console.log(msg)144 fs.appendFile(logName, msg+'\n', function(err) {})145 break

146 //case 'gift':

147 //console.log(`[${msg.from.name}]->赠送${msg.count}个${msg.name}`)

148 //break

149 //case 'online':

150 //console.log(`[当前人气]:${msg.count}`)

151 //break

152 }153 })154

155 client.on('error', e =>{156 console.log(e)157 })158

159 client.on('close', () =>{160 console.log('close')161 })162

163 client.start()

参数说明:

node.exe huya.js [虎牙房间号] [要存储目录的绝对路径] [文件名]

编写调用 huya.js 的 批处理文件 huyaDanmu.cmd

1 @echo off

2 title %1 %3 %date% %time%3 D:\node-v8.9.1-win-x86\node.exe D:\node-v8.9.1-win-x86\node_modules\huya-danmu\huya.js %1 %2 %3

打开控制台窗口 输入 huyaDanmu [虎牙房间号] [要存储目录的绝对路径] [文件名]

不输入存储目录时 直接保存到当前目录

不输入文件名时 自动取名为 YYMMDD-HHMMSS_房间号.LRC

保存的格式是 LRC 歌词文件, 如果想转换 SRT 或者 ASS 格式也很方便, 利用新版 FFMpeg 就可以

1 ffmpeg -i xxx.LRC xxx.SRT

2 ffmpeg -i xxx.LRC xxx.ASS

温馨提示: 录视频的同时运行 huyaDanmu 批处理命令, 就不用在调整时间轴;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值