skyclo/node-scdl

Stream is closed

North-West-Wind opened this issue · 8 comments

I used my browser's client ID (I dont know if this is the reason it doesn't work) to try to get a readable stream from soundcloud and parse it into music-metadata. However, music-metadata gives me an error about Error: Stream closed.

In fact, I'm trying to make my discord.js music bot play soundcloud tracks, but it didn't work. It just instantly end without any error.

Any idea about what's wrong?

Funny enough, the reason why I created this was for my bot as well...

From a brief overview, it looks as if the NPM package music-metadata takes a readable stream, in which case would be the stream returned from SCDL#getStream, and parses the metadata included. From what I recall, it doesn't look like SoundCloud includes the metadata for uploaded tracks. If you are trying to get metadata from it, I would advise against doing so and instead use the return from SCDL#getInfo (which includes things like artists, song names, albums, etc). I'll be writing documentation on this soon

The thing is with SoundCloud is that the labeling of tracks is very inconsistent so this may be an issue if you are trying to properly format a song title.

Now regarding the Error: Stream closed, I would need to see the code you are using so I can get a better understanding of what is going on.

While unrelated to the package, the proper way you would play this back using discord.js, is by doing this

let connection = await msg.member.voiceChannel.join() // Joins the VC
let stream = new PassThrough() // Creates a PassThrough (which discord.js accepts for ReadableStreams)
let song = await SoundCloudDownloader.getStream(12345789) // Gets the song based on ID

stream.end(Buffer.from(song.body, 'binary')) // Writes to the readable stream with the mp3 file

server.dispatcher = connection.playStream(stream) // Plays the stream

I hope this helps

Oh. The README of this package tells that SCDL#getStream returns a ReadableStream and I think it will just work when playing it on discord.js like ytdl-core.

I tried your code to play music with discord.js and got an error:

/rbd/pnpm-volume/0ee8e202-4c9f-43f0-b5eb-2c1dacae0079/node_modules/.registry.npmjs.org/prism-media/1.2.1/node_modules/prism-media/src/opus/Opus.js:59
    return this.encoder.decode(buffer, Opus.name === 'opusscript' ? null : this._options.frameSize);
                        ^
TypeError: The compressed data passed is corrupted
    at Decoder._decode (/rbd/pnpm-volume/0ee8e202-4c9f-43f0-b5eb-2c1dacae0079/node_modules/.registry.npmjs.org/prism-media/1.2.1/node_modules/prism-media/src/opus/Opus.js:59:25)
    at Decoder._transform (/rbd/pnpm-volume/0ee8e202-4c9f-43f0-b5eb-2c1dacae0079/node_modules/.registry.npmjs.org/prism-media/1.2.1/node_modules/prism-media/src/opus/Opus.js:184:20)
    at Decoder.Transform._read (_stream_transform.js:189:10)
    at Decoder.Transform._write (_stream_transform.js:177:12)
    at doWrite (_stream_writable.js:417:12)
    at writeOrBuffer (_stream_writable.js:401:5)
    at Decoder.Writable.write (_stream_writable.js:301:11)
    at PassThrough.ondata (_stream_readable.js:696:22)
    at PassThrough.emit (events.js:196:13)
    at PassThrough.Readable.read (_stream_readable.js:491:10)
    at flow (_stream_readable.js:960:34)
    at resume_ (_stream_readable.js:941:3)
    at processTicksAndRejections (internal/process/task_queues.js:83:17)

Now, for the code about Error: Stream closed:

const SCDL = require("node-scdl")
const scdl = new SCDL(CLIENT_ID)
const mm = require("music-metadata")

var stream = await scdl.getStream(ID).catch(console.error)
var metadata = await mm.parseStream(stream).catch(console.error)
console.log(metadata)

Update: I tried the link of your package https://api.soundcloud.com/tracks/TRACK_ID/stream?consumer_key=CLIENT_ID in my browser and I received a 403 Forbidden. I tried using another link
https://api.soundcloud.com/tracks/TRACK_ID/stream?client_id=CLIENT_ID and it also gave 403 Forbidden. I don't know if that's the issue...

@North-West-Wind Okay thank you for the information... I will attempt to investigate the issue further in the morning. I have a feeling that either the key is invalid, you're being rate-limited/blocked on SoundCloud or SoundCloud changed something on their end.

Alright, I think my key may be blocked. It gives 403 even when I'm not getting stream. However, I managed to use https://api-v2.soundcloud.com to get track information but the stream page of tracks are not found. It gives me 404 when using the link similar to getStream of your package.

I grabbed another Client ID and tried the link https://api.soundcloud.com/tracks/ID/steam?client_id=CLIENT_ID in my browser and it works. So it's the problem with the client id.

Now I've figured that out, I'm closing this.

Note that the error of playing the stream in discord.js still exists. That will be another issue.

After a few tests with the new client id, I still ran into problems but the client id do work in my browser.

Okay, I now know what it is. I encountered this issue a while back when I was developing my own bot. The new ID's generated by SoundCloud are temporary ID's that have limited access and are thrown out after some time. This is because of the fact that SoundCloud no longer officially supports the old V1 API. Older IDs typically are ones registered with the old SoundCloud API and are permanent. My only advice is to try and look around for an old ID on the internet.

I am keeping this issue open as it is still a relevant and active problem.

Yes I got myself an old ID and it still doesn't work until I found out I was so dumb that I had a line me my connection.play(): type: "opus". This is my fault. When I removed that, the thing works. Solved my problem at last.