discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.39k stars 3.97k forks source link

Strange music playback with opus codec #2360

Closed NickUfer closed 6 years ago

NickUfer commented 6 years ago

Please describe the problem you are having in as much detail as possible:

Music which is played is incomplete / is jumpy / has wrong speed / doesnt play some parts. This bug occurs in the 12.0.0dev version and not in the stable one from npmjs (11.3.0) with the exact same code.

For clarification I recorded the output of my bot: (you can clearly recognize the wrong behaviour from ~25 seconds) https://workupload.com/file/ayX5MHC

And this is the original song: https://www.youtube.com/watch?v=A3mScURQA5U

Include a reproducible code sample here, if possible:

fluent-ffmpeg:

        this.ffmpegProcess = ffmpegCommand()
            .addInput(input)
            .format('data')
            .addOutputOptions(
                [
                    '-vn',
                    '-map 0:a',
                    '-sample_fmt s16',
                    '-acodec libopus',
                    '-b:a 96k',
                    '-ar 48k',
                    '-vbr off'
                ]);

The output of ffmpeg goes into a Transform / Duplex stream which is then played like:

guild.voiceConnection.play(stream, {type: "opus"});

also tried:

guild.voiceConnection.play(stream, {type: "opus", passes: 3});

Further details:

Dependencies:

  "dependencies": {
    "discord.js": "github:discordjs/discord.js",
    "erlpack": "github:discordapp/erlpack",
    "fluent-ffmpeg": "^2.1.2",
    "node-opus": "^0.2.7",
    "sodium": "^2.0.3",
    "typings": "^2.1.1",
    "uws": "^9.14.0"
  }
Andreybest commented 6 years ago

What codec you have installed, I have problem with playing audio on opusscript (11.3.0), but on node-opus everything is good.

NickUfer commented 6 years ago

I am using node-opus v0.2.7. I quickly added my dependencies.

Andreybest commented 6 years ago

Hmm, than its a dev branch bug. I think???

ghost commented 6 years ago

I'm also getting this with type: "opus". It seems to be trying to play it back at the same speed as ffmpeg is outputting it. Could it need a buffer of sorts? This also happens with -frame_duration 20 sent to ffmpeg. I don't believe node-opus comes into play when using type: "opus"

@DerTechNick: What does your Transform stream look like?

Same versions:

Dependencies:

  "bufferutil": "^3.0.3",
  "discord.js": "git+https://github.com/discordjs/discord.js.git",
  "erlpack": "github:discordapp/erlpack",
  "libsodium-wrappers": "^0.7.3",
  "node-opus": "^0.2.7",
  "uws": "^9.14.0",
  "zlib-sync": "^0.1.4"
NickUfer commented 6 years ago

My transform stream is in fact just a duplex stream.

        this._outputStream = new Transform({
            transform: function (chunk, encoding, callback) {
                this.push(chunk);
                callback();
            },
            highWaterMark: 1024 * 4096 // TODO
        });

Btw: I have found a workaround. I have choosen "opus" as output format instead of "data" for ffmpeg and used "ogg/opus" as type when i play the resource.

amishshah commented 6 years ago

Yep, otherwise the stream would send data back to ffmpeg to be reconverted to pcm. As a sidenote, why not just let discord.js convert your stream to opus?