Discord.JS语音记录和数据压缩

3

我目前正在制作一个Discord机器人,拥有“用户录音”功能。我在VC中测试它时注意到,13分钟后输出的.pcm文件大小达到了约20GB。

        this.voiceChannel = await message.member.voice.channel.join()
        this.reciever = this.voiceChannel.receiver
        this.voiceChannel.on('debug', (debug) => {
            let packet = JSON.parse(debug.slice(8))
            console.log(packet.op)

            if(!packet.d || packet.d && packet.d.speaking != 1) return;
            let user = this.client.users.resolve(packet.d.user_id)
            if(packet.d.speaking) {
                let userStream = this.reciever.createStream(user, {mode: 'pcm', end: 'manual'})
                let writeStream = require('fs').createWriteStream('./recording.pcm', {})
                this.us = userStream
                this.ws = writeStream

                this.us.on("data", (chunk) =>{
                    console.log(chunk)
                    this.us.pipe(this.ws)
                })
                this.ws.on("pipe", console.log)
            }
        })

有没有可能将大小约为20GB的.pcm文件压缩至5-10MB?这似乎很奇怪,因为每个来自Discord.js的Buffer都达到了惊人的4000字节(4KB)(这也导致我的磁盘使用率达到100%,写入速度为60MB / s)。


这个回答解决了你的问题吗?https://dev59.com/uGMl5IYBdhLWcg3wwZTd#43829712 - Syntle
@Syntle,它不是将文件放入zip / 压缩文件夹中。它更多地是在接收数据时压缩数据,并将压缩后的数据放入文件本身。尽管如此,该链接确实帮助我深入了解相同模块,并提供了帮助。 - Mozza
1个回答

4

我自己回答这个问题,但我认为我知道我做错了什么。

this.us.on("data", (chunk) =>{
    console.log(chunk)
    this.us.pipe(this.ws)
})

那一段我说错了,因为我每次收到数据时都发送了数据,不止一次,而是两次。我还使用了 zlib 模块(https://npmjs.org/package/zlib),这进一步帮助了数据/语音压缩。

this.us.on("data", (chunk) => {
    let slowBuf = this.zlib.deflate(chunk, (er, res) => {
        console.log(res)
        this.ws.write(res)
    })
})

这个方法非常有效,现在在测试时每2分钟左右可以写入约600KB的数据。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接