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

时间: 2018-12-17阅读: 2058标签: 直播

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+‘\n‘, 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:\node-v8.9.1-win-x86\node.exe D:\node-v8.9.1-win-x86\node_modules\huya-danmu\huya.js %1 %2


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

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

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

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


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


来自:https://www.cnblogs.com/nlsoft/p/10127922.html 

 

站长推荐

1.云服务推荐: 国内主流云服务商,各类云产品的最新活动,优惠券领取。地址:阿里云腾讯云华为云

2.广告联盟: 整理了目前主流的广告联盟平台,如果你有流量,可以作为参考选择适合你的平台点击进入

链接: http://www.fly63.com/article/detial/1625

关闭

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!