let dispatcher = queue.connection.playStream(ytdl(music.url,{quality: 'highestaudio'}))
.on('end', () => {
queue.musics.shift();
votes.votes = 0;
votes.voters = [];
setTimeout(() => {
bot.play(guild, queue.musics[0]);
}, 250);
})
.on('progress', (d, total, length) => {
console.log('progress', total / length);
})
.on('error', err => console.error(err));
How can i download the video and play it
So it does play, but slowly?
Not slowly, but laggy
You might try caching more of the ytdl stream
will it be less laggy if i download it first and after play it ?
It could - should be worth a shot
Either you first download it to a file or you use sth like stream-buffers
bot.play = (guild, music) => {
let queue = bot.queue.get(guild.id);
let votes = bot.votes.get(guild.id)
if (!music) {
queue.voiceChannel.leave();
bot.queue.delete(guild.id);
bot.votes.delete(guild.id);
return queue.textChannel.send(馃幍 finish`);
}
let music1 = ytdl(music.url);
music1.pipe(fs.createWriteStream('video.mp3'))
let dispatcher= queue.connection.playStream(fs.createReadStream('video.mp3'))
.on('end', () => {
queue.musics.shift();
votes.votes = 0;
votes.voters = [];
setTimeout(() => {
bot.play(guild, queue.musics[0]);
}, 250);
})
.on('progress', (d, total, length) => {
console.log('progress', total / length);
})
.on('error', err => console.error(err));
dispatcher.setVolumeLogarithmic(queue.volume / 100);
queue.textChannel.send(`馃幍 **${music.title}** now playing`);
`
what i did wrong here
The solution to this might be the same as https://github.com/fent/node-ytdl-core/issues/402, to use highWaterMark. Look in the comments for that issue for more, but basically, you'd use a higher value for highWaterMark, default is 16kb which may not be enough.
ytdl(url, { highWaterMark: 1<<25 }) // 32mb
I may just increase the default value for this. My concern is people won't like that ytdl takes more memory to use. But surely making it only a couple of mb people won't mind?
Edit: the below comment is unrelevant because I was changing the wrong highWaterMark option (not the one in ytdl).
I have done some testing, and it seems that the "lagginess" is caused by some form of rate limiting from youtube's side. At least that is what I'm suspecting. With a low value for highWaterMark (I tried simply 1), "skipping", that is not playing videos to the end and then starting to play a different or the same video, but with a new stream, everything works fine (except for #402).
However, with a higher value for highWaterMark, "skipping" causes the stream to be slow (I assume download is slower than playback), which causes lag. For me, the higher the value of highWaterMark, the worse it gets. With values of around 500KB, it manages to catch up after a few seconds, but with higher values it didn't seem to catch up.
So this seems to be a bit of trade-off situation. Use higher values for highWaterMark and avoid #402, but have potentially laggier playback. Use lower values and have smoother playback but be plagued by #402.
My bot its not even smooth in some music he lags all the way
`
let dispatcher = queue.connection.playStream(ytdl(music.url,{filter: 'audioonly'},{quality: 'highestaudio'},{ highWaterMark: 1<<25 } ))
.on('end', () => {
queue.musics.shift();
votes.votes = 0;
votes.voters = [];
setTimeout(() => {
bot.play(guild, queue.musics[0]);
}, 250);
})
.on('error', err => console.error(err));
dispatcher.setVolumeLogarithmic(queue.volume / 100);
queue.textChannel.send(`馃幍 **${music.title}** 褋械泄褔邪褋 懈谐褉邪械褌`);`
it didn`t help
`
let dispatcher = queue.connection.playStream(ytdl(music.url,{filter: 'audioonly'},{quality: 'highestaudio'},{ highWaterMark: 1<<25 } ))
.on('end', () => {
queue.musics.shift();
votes.votes = 0;
votes.voters = [];
setTimeout(() => {
bot.play(guild, queue.musics[0]);
}, 250);
})
.on('error', err => console.error(err));
dispatcher.setVolumeLogarithmic(queue.volume / 100);queue.textChannel.send(`馃幍 **${music.title}** 褋械泄褔邪褋 懈谐褉邪械褌`);`it didn`t help
Edit (got confused because both ytdl and discord.js have a highWaterMark option):
You should pass only one options object (not sure if your way even works).
Do ytdl(music.url,{filter: 'audioonly', quality: 'highestaudio', highWaterMark: 1<<25 }) instead and try again.
Nothing changed
If you can join my discord server i would explain you better
If you are using discord.js, you can try setting their highWaterMark to a lower value (which works well for me). I'm using 1. When I had accidentally increased that value instead of the one in ytdl-core, I had also experienced laggy/choppy playback.
Like so:
let dispatcher = queue.connection.playStream(ytdl(music.url,{filter: 'audioonly', quality: 'highestaudio', highWaterMark: 1<<25 }), {highWaterMark: 1})
If that doesn't help you can try switching to the master branch of discord.js. I'm using that version.
That worked
Closing due to issue being solved.
Most helpful comment
If you are using discord.js, you can try setting their
highWaterMarkto a lower value (which works well for me). I'm using 1. When I had accidentally increased that value instead of the one in ytdl-core, I had also experienced laggy/choppy playback.Like so:
let dispatcher = queue.connection.playStream(ytdl(music.url,{filter: 'audioonly', quality: 'highestaudio', highWaterMark: 1<<25 }), {highWaterMark: 1})If that doesn't help you can try switching to the master branch of discord.js. I'm using that version.