twlite / discord-ytdl-core

Simple ytdl wrapper for discord bots with custom ffmpeg args support.
https://www.npmjs.com/package/discord-ytdl-core
Apache License 2.0
52 stars 10 forks source link

Problem with seek option #17

Closed FredTheNoob closed 3 years ago

FredTheNoob commented 3 years ago

Hi,

I'm trying to make a seek command using this package, now when I originally play the song I do it like this:

// This plays the audio
        serverQueue.connection.play(await stream, {type: 'opus', bitrate: 'auto', seek: 0})
            // if the song ends
            .on('finish', reason => {
                if (reason === 'Stream is not generating quickly enough.') console.log('Song ended.');
                else console.error("line 49 music controller, log: " + reason);

                serverQueue.songs.shift();
                // play the first song in the array
                this.startPlay(serverQueue.songs[0], message);
            })
            .on('error', error => {
                console.error("Lib/MusicController.js:" + error);
                message.channel.send(embeds.error(message.author, `\`${error}\``))
                queue.delete(message.guild.id);
                return;
            })
            .on('begin', error => console.error("MusicController.js Begin Error:" + error));
        // Set the default volume
        serverQueue.connection.dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
        serverQueue.textChannel.send(embeds.extended(message.author, `**Play**`, `🎶 Started playing: **${song.title}**`, `https://img.youtube.com/vi/${song.id}/mqdefault.jpg`));

When seeking I do the following:

async seek(message, timeStamp) {
        serverQueue = queue.get(message.guild.id);

        serverQueue.connection.play(serverQueue.currentSong, {type: 'opus', bitrate: 'auto', seek: timeStamp})
    }

Now when I run the seek function, I can hear that the dispatcher goes a little bit into the song, but shortly dies after. How come I resolve this issue?

Regards Fred

twlite commented 3 years ago

your code is passing seek to discord.js dispatcher and discord.js seek does not work for opus streams. Make sure you are passing seek to discord-ytdl-core

twlite commented 3 years ago

Your code should look something like this:

const stream = ytdl(url, {
    opusEncoded: true,
    seek: TimeInSecondsToSeek
});

connection.play(stream, { type: "opus", bitrate: "auto" });
FredTheNoob commented 3 years ago

Thank you very much, it finally works! Now I've noticed that it takes a while before the seek happens (as in there is some silence where it loads I guess?) - Rythm for instance doesn't have this problem at all, what should I do in order for it to seek instantly? :]

twlite commented 3 years ago

you are re-creating your stream so happens

FredTheNoob commented 3 years ago

I see, I've attempted to save my initial stream onto the queue like so:

serverQueue.currentSong = stream;

When I do this, I hear that the stream will skip instantly, but dies shortly after...

FredTheNoob commented 3 years ago

How come I access the seek options within ytdl from serverQueue.currentSong after I set it equal to the stream?