node-ytdl-core: Very rare 403 error when streaming from YouTube
Very rarely I get a 403 error when trying to stream a YouTube video with my discord bot. However, I can then immediately retry to play the exact same video and then it works.
I haven’t found a consistent way of reproducing this error and it is quite rare, maybe 1 in 1000 stream attempts would be my guess. Also continuously trying to stream the same video doesn’t seem to be able to reproduce the error. So any attempt would have to probably use a collection of a lot of different videos (1000+) and have a lot of time, but hopefully there is another way of figuring this out.
Here is the stack trace (which seems quite useless, as it’s just the status code that miniget receives):
Error: Status code: 403
at ClientRequest.httpLib.get (/REDACTED/node_modules/miniget/lib/index.js:123:19)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at ClientRequest.emit (events.js:211:7)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:543:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:112:17)
at TLSSocket.socketOnData (_http_client.js:440:20)
at emitOne (events.js:116:13)
at TLSSocket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12) +0ms
The error always seems to appear twice (I only posted one to keep the text shorter), completely identically. I thought your miniget module would only retry on a 500 error, so I’m not sure why it shows up twice.
I am using the newest ytdl-core like this stream = ytdl(url, opts);.
And I’m using discord.js on the master branch, where I pass the stream like this this.conn.play(this.stream, opts);
I think if ytdl-core could simply retry once after receiving a 403, this might solve the problem. Of course a 403 with This video is not available. would need no retry. If I’m not mistaken ytdl-core doesn’t retry on a receiving a 403 as of now, of course rightly so as normally a 403 means that the video is not available.
In this case, I think something might be going wrong on YouTube’s side, but we can’t just wait for them to fix it. And I know that this is quite a difficult problem, as it is not easily reproducible.
If you do not want to change anything in this module, because this error is very vague, I would gladly try out a changed version myself, I might just need a hint how I could add a retry when a 403 is received.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 19 (3 by maintainers)
YouTube sometimes will provide a different signature type for URLs that appears to be base64’d, rather than the usual
012345A.BCFDE. It uses the same decrypt function, but it needs to be appended as&sig=, rather than&signature=. YouTube provides the parameter name as a&sp=, so for example:becomes:
So the
/signature/on this line would need to take that into account.It appears to be more common on music videos, appearing roughly 1 out of 40. Here’s the relevant line in Invidious and a relevant PR in youtube-dl.