虎牙直播弹幕转换字幕格式 基于Node.js 的 huya-danmu

Posted 1CM

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了虎牙直播弹幕转换字幕格式 基于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 新文件

const huya_danmu = require(‘./index‘)
var fs = require(‘fs‘)

const roomid = process.argv[2]
const client = new huya_danmu(roomid)
var t1 = 0
var t2 = 0
var interval = 0
var logName = ‘‘

var emots = {
"/{dx" : "[大笑]",
"/{sh" : "[送花]",
"/{tx" : "[偷笑]",
"/{dk" : "[大哭]",
"/{hh" : "[嘿哈]",
"/{66" : "[666]",
"/{gd" : "[感动]",
"/{yw" : "[疑问]",
"/{xh" : "[喜欢]",
"/{jx" : "[奸笑]",
"/{zan" : "[赞]",
"/{ka" : "[可爱]",
"/{am" : "[傲慢]",
"/{kx" : "[开心]",
"/{88" : "[拜拜]",
"/{hx"  : "[害羞]",
"/{zs"  : "[衰]",
"/{pu"  : "[吐血]",
"/{zc"  : "[嘴馋]",
"/{sq"  : "[生气]",
"/{fe"  : "[扶额]",
"/{bz"  : "[闭嘴]",
"/{kw"  : "[枯萎]",
"/{xu"  : "[嘘]",
"/{xk"  : "[笑哭]",
"/{lh"  : "[流汗]",
"/{bk"  : "[不看]",
"/{hq"  : "[哈欠]",
"/{tp"  : "[调皮]",
"/{gl"  : "[鬼脸]",
"/{cl"  : "[戳脸]",
"/{dg"  : "[大哥]",
"/{kun" : "[困]",
"/{yb"  : "[拥抱]",
"/{zt"  : "[猪头]",
"/{kl"  : "[骷髅]",
"/{cc"  : "[臭臭]",
"/{xd"  : "[心动]",
"/{dao" : "[刀]"
}

function checkemot(str)
{
    if(str.includes(‘/{‘) == false ) return str
    for(var key in emots) {
        //console.log(key + ": " + emots[key]);
        //str.replace(/key/g, emots[key])
        str = str.replace(new RegExp(key,‘g‘), emots[key])
    }
    return str
}

function PFI(num)
{
    if(num<10)
        return ‘0‘+num
    else 
        return num
}

function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "";
    var seperator2 = "";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    
    var strHH = date.getHours()
    var strMM = date.getMinutes()
    var strSS = date.getSeconds()
    if (strHH >= 0 && strHH <= 9) strHH = "0" + strHH;
    if (strMM >= 0 && strMM <= 9) strMM = "0" + strMM;
    if (strSS >= 0 && strSS <= 9) strSS = "0" + strSS;
        
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
            + "-" + strHH + seperator2 + strMM + seperator2 + strSS;
    return currentdate;
}

client.on(‘connect‘, () => {
    console.log(`已连接huya ${roomid}`)
    t1 = new Date().getTime()
    var savepath = process.argv[3]
    if(typeof(savepath)==‘undefined‘)
        logName = ‘./‘+getNowFormatDate()+‘_‘+roomid+‘.LRC‘
    else
        logName = savepath+‘/‘+getNowFormatDate()+‘_‘+roomid+‘.LRC‘
    t2 = t1
    t1 = process.uptime()*1000
    console.log(logName)
})

client.on(‘message‘, msg => {
    switch (msg.type) {
        case ‘chat‘:
            //console.log(`[${msg.from.name}]:${msg.content}`)
            t2 = process.uptime()*1000
            interval = (t2 - t1)
            var date = new Date( interval )
            var HH = date.getUTCHours()
            var MM = PFI(date.getUTCMinutes()+HH*60)
            var SS = PFI(date.getUTCSeconds())
            var MS = PFI(parseInt(date.getUTCMilliseconds()/10))
            var msg = `[${MM}:${SS}.${MS}]${msg.content}`
            msg = checkemot(msg)
            console.log(`${msg}`)
            fs.appendFile(logName, msg+‘
‘, function (err) {})
            break
        // case ‘gift‘:
            // console.log(`[${msg.from.name}]->赠送${msg.count}个${msg.name}`)
            // break
        // case ‘online‘:
            // console.log(`[当前人气]:${msg.count}`)
            // break
    }
})

client.on(‘error‘, e => {
    console.log(e)
})

client.on(‘close‘, () => {
    console.log(‘close‘)
})

client.start()

 

参数说明:

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

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

@echo off
title %1
D:
ode-v8.9.1-win-x86
ode.exe D:
ode-v8.9.1-win-x86
ode_moduleshuya-danmuhuya.js %1 %2

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

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

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

ffmpeg -i xxx.LRC xxx.SRT
ffmpeg -i xxx.LRC xxx.ASS

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


以上是关于虎牙直播弹幕转换字幕格式 基于Node.js 的 huya-danmu的主要内容,如果未能解决你的问题,请参考以下文章

有没有虎牙无限刷弹幕软件?

虎牙电脑端怎么设置气泡弹幕

如何用vb获取虎牙弹幕

Bilibili-XMLSubtitle-to-ASS可视化Bilibili本地视频XML弹幕转换ASS字幕转换器:新增自动转换Bilibili下载视频功能

如何在直播中使用 FFMpeg 将 dvb 字幕转换为文本格式,或者如何优化 dvb 刻录过程?

深入浅出了解CDN