qeled / discordie

Predictable JavaScript abstractions for Discord API.
https://qeled.github.io/discordie/
BSD 2-Clause "Simplified" License
190 stars 45 forks source link

Music stops playing before song ends #62

Closed Mochlate closed 7 years ago

Mochlate commented 7 years ago

Often my music just cuts off in the middle of a song. Pretty sure I've narrowed it to a discordie problem. Not sure if anyone else is having this problem?

qeled commented 7 years ago

If it's a network stream, it's most likely server dropping the connection. Some servers (like YouTube in some regions - some people have problems, some don't) may cut off streams for slow readers a bit too early. Increasing incoming buffer size (or proxying incoming data through a stream with large highWaterMark) may reduce amount of such forced disconnections.

You can also implement resuming logic with FFmpeg's seek option -ss (if used) by comparing duration (if known) with current time when it ends, or otherwise write your own stream adapter that resumes the download of the bytestream on error.

Same issue can be reproduced with youtube-dl with rate limit option enabled (youtube-dl -f bestaudio --rate-limit 2k <video>) to simulate slow playback:

[youtube] ...: Downloading webpage
[youtube] ...: Downloading video info webpage
[youtube] ...: Extracting video information
[youtube] ...: Downloading MPD manifest
[download] Destination: [---].webm
[download]  97.8% of 4.66MiB at  2.01KiB/s ETA 00:52 ERROR: unable to download video data: [WinError 10054] An existing connection was forcibly closed by the remote host
Mochlate commented 7 years ago

How can I increase the incoming buffer size for the discordie ffmpeg wrapper?

qeled commented 7 years ago

Actually after some rtfm I found out that FFmpeg supports reconnecting, so applying these as input stream options should work:

voiceConnection.createExternalEncoder({
  ...,
  inputArgs: ["-reconnect", "1", "-reconnect_streamed", "1", "-reconnect_delay_max", "2"]
});

Which when tested in a standalone FFmpeg on my machine results in:

size=    2877kB time=00:00:12.54 bitrate=1536.0kbits/s
[tls @ 0000000005bfe300] Error in the pull function./s
[https @ 00000000050ca300] Will reconnect at 5174111 error=Error number -5 occurred.
[https @ 00000000050ca300] request: GET /videoplayback?id=[---] HTTP/1.1
User-Agent: Lavf/57.2.100
Accept: */*
Range: bytes=5174111-
Connection: close
Host: [---].googlevideo.com
Icy-MetaData: 1

size=    3015kB time=00:00:13.26 bitrate=1536.0kbits/s
Mochlate commented 7 years ago

Doing that actually results in the song terminating immediately.

Mochlate commented 7 years ago

It results in this:

[FFmpeg] Unrecognized option 'reconnect_streamed'. [FFmpeg] Error splitting the argument list: [FFmpeg] Option not found FFmpegEncoder: external encoder exited with code 1

but I see that reconnect_streamed is an arg according to the docs,

also apparently reconnect_delay_max is also unrecognized

KeineLimonade commented 7 years ago

Maybe you have another version of ffmpeg?

Mochlate commented 7 years ago

I did apt-get install -yq ffmpeg

TheBITLINK commented 7 years ago

@Mochlate that's the problem, Ubuntu's ffmpeg builds are old and incomplete, in my case i fixed it using the static builds:

wget https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz
tar -xvf ffmpeg-git-64bit-static.tar.xz
sudo cp ffmpeg-git-201*-static/ff* /usr/bin
SSUHan commented 6 years ago

http://blog.programster.org/ubuntu-16-04-install-ffmpeg-from-ppa

This blog would be helpful for me.