2021-09-06 21:45:52 +05:00
# Create Stream
This is a checkpoint where discord-player calls `createStream` before downloading stream.
2021-09-07 09:48:56 +05:00
### Custom stream Engine
Discord Player by default uses ** [node-ytdl-core ](https://github.com/fent/node-ytdl-core )** for youtube and some other extractors for other sources.
If you need to modify this behavior without touching extractors, you need to use `createStream` functionality of discord player.
Here's an example on how you can use ** [play-dl ](https://npmjs.com/package/play-dl )** to download youtube streams instead of using ytdl-core.
2021-09-06 21:45:52 +05:00
```js
2021-09-07 09:48:56 +05:00
const playdl = require("play-dl");
// other code
2021-09-25 20:35:19 +05:00
const queue = player.createQueue(..., {
...,
async onBeforeCreateStream(track, source, _queue) {
2021-09-07 09:48:56 +05:00
// only trap youtube source
if (source === "youtube") {
// track here would be youtube track
2022-01-26 00:12:52 +05:00
return (await playdl.stream(track.url, { discordPlayerCompatibility : true })).stream;
2021-09-07 09:48:56 +05:00
// we must return readable stream or void (returning void means telling discord-player to look for default extractor)
}
2021-09-25 20:35:19 +05:00
}
});
2021-09-07 09:48:56 +05:00
```
2021-09-25 20:35:19 +05:00
`<Queue>.onBeforeCreateStream` is called before actually downloading the stream. It is a different concept from extractors, where you are **just** downloading
streams. `source` here will be a video source. Streams from `onBeforeCreateStream` are then piped to `FFmpeg` and finally sent to Discord voice servers.
2021-09-07 09:48:56 +05:00
# FAQ
## How can I remove this?
> If you already made this change and want to switch to default mode in runtime,
2021-09-25 20:35:19 +05:00
> you can set `queue.onBeforeCreateStream` to `null` which will make `discord-player` use default config.
2021-09-07 09:48:56 +05:00
## Which stream format should I return?
> It's not necessary to return opus format or whatever, since every streams have to be converted to s16le, due to inline volume.
## Can I use ytdl-core-discord?
> Yes, you can.
## Can I use this for other sources, like soundcloud?
2021-11-28 14:26:12 +05:00
> Absolutely.
## This is not working properly
> `onBeforeCreateStream` may not work properly if you have `spotifyBridge` enabled (enabled by default).