bwmarrin / dgvoice

Discordgo Voice - A sub project to work on voice implementation for Discordgo
BSD 3-Clause "New" or "Revised" License
100 stars 39 forks source link

use ffmpeg for opus encoding #11

Closed nhooyr closed 4 years ago

nhooyr commented 7 years ago

I've been writing a discord bot in elixir using this library https://github.com/cronokirby/alchemy

Unlike this library where an opus library is used, the author uses ffmpeg for opus encoding directly from the file stream. In order to do this, ffmpeg has to be compiled with libopus support. On macOS, its as simple as brew install ffmpeg --with-opus.

I've ported it over to Go. It's very crude and only for demonstration purposes but it works really well and eliminates the need for a executable like dca-rs or a go opus library.

https://github.com/nhooyr/botatouille/blob/7e1cd9d5a8d517fd43fd11599b2a62bf832a5c96/cmd/botatouille/music/music.go#L62-L104

Only issue is that if the goroutine reading from ffmpeg's stdout ever falls behind ffmpeg, it may read more than a single frame. Then it will try and send more than a single frame to discord which will cause the audio to get completely screwed up. I wanted to know if you think there is any way to fix this.

I think its highly unlikely because the goroutine has its own thread and is only reading from ffmpeg and not doing anything else but I'm not 100% sure. What do you think?

If you like this method, I can incorporate it into this library.

bwmarrin commented 7 years ago

It's a great idea, but I think it should just be another example repo. This repo specifically needs to almost be removed and re-wrote if anything. But I'd hate to make such an API change to it since I know folks are using it.

Make a nice repo and tag it with discordgo :) I can add it to the discordgo wiki too. I think one downfall of the ffmpeg route is you loose some of the config options possible for opus, right? Like VBA vs Constant and maybe some other things? But, it is a nice way of doing it. Less complicated.

I think the best thing about .dca is really that you can pre-encode the files in advance for things you play often and save them in .dca files that are ready to play.

cocoastorm commented 5 years ago

@nhooyr, hey did you ever end up making a repo with using ffmpeg to encode with gopus directly? thanks πŸ‘

nhooyr commented 5 years ago

Unfortunately I gave up on this.

cocoastorm commented 5 years ago

ah okie that's a shame, but thanks anyways! πŸ‘

nhooyr commented 4 years ago

Going to close as it has been a long time and I'm not sure if this is relevant anymore.

forscht commented 1 year ago

@nhooyr I am working something similar, did you find something ?

nhooyr commented 1 year ago

Nope, never ended up continuing with my project.