2 Commits

Author SHA1 Message Date
37c872c50a Start some code organization 2025-02-11 00:05:42 -05:00
d42bb9b288 Add opusframes package with encoding/decoding (#13)
Closes #12

Added a library to encode videos from yt-dlp into our custom `.of` file format that can be easily read and directly streamed to discord.

Co-authored-by: Xander Bazzi <xander@xbazzi.com>
Reviewed-on: https://www.gitgud.foo/thegrind/papibot/pulls/13
Reviewed-by: xbazzi <xander@xbazzi.com>
Co-authored-by: Javier Feliz <me@javierfeliz.com>
Co-committed-by: Javier Feliz <me@javierfeliz.com>
2025-02-07 12:20:35 -07:00
5 changed files with 281 additions and 151 deletions

1
.gitignore vendored
View File

@@ -1,6 +1,7 @@
testfile.wav
play.opus
output.opus
*.of
.env
*.wav
*.opus

180
main.go
View File

@@ -2,48 +2,33 @@ package main
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"os/signal"
"syscall"
"time"
"git.thegrind.dev/thegrind/papibot/pkg/bot"
"git.thegrind.dev/thegrind/papibot/pkg/opusframes"
"git.thegrind.dev/thegrind/papibot/pkg/youtube"
dg "github.com/bwmarrin/discordgo"
"github.com/javif89/dotenv"
)
var commandHandlers = map[string]func(s *dg.Session, i *dg.InteractionCreate){
"play": playCommand,
}
func main() {
env := dotenv.Load(".env")
botToken := env.Get("DISCORD_BOT_TOKEN")
appId := env.Get("DISCORD_APP_ID")
bot := bot.New(botToken, appId)
log.Println("Starting bot")
err := bot.Start()
log.Println("Error starting the bot", err)
var discord *dg.Session
var err error
discord, err = dg.New(fmt.Sprintf("Bot %s", botToken))
if err != nil {
log.Println("Error starting the bot", err)
return
}
discord.AddHandler(ready)
discord.Identify.Intents = dg.IntentsGuilds | dg.IntentsGuildMessages | dg.IntentsGuildVoiceStates
err = discord.Open()
if err != nil {
log.Println("Error opening Discord session: ", err)
}
// Register slash commands
discord.AddHandler(handleSlashCommand)
log.Println("Adding command handlers")
bot.AddSlashCommand("play", playCommand)
log.Println("Done")
log.Println("Bot is running. Press CTRL+C to exit.")
sc := make(chan os.Signal, 1)
@@ -51,15 +36,7 @@ func main() {
<-sc
log.Println("Bot is exiting...")
log.Println("Removing commands")
appId := env.Get("DISCORD_APP_ID")
cmds, _ := discord.ApplicationCommands(appId, "338782945110392832")
for _, cmd := range cmds {
discord.ApplicationCommandDelete(appId, "338782945110392832", cmd.ID)
}
discord.Close()
bot.Stop()
log.Println("Goodbye!")
}
@@ -75,7 +52,12 @@ func playCommand(s *dg.Session, i *dg.InteractionCreate) {
},
})
downloadVideo(url)
os.Remove("vid.webm")
youtube.Download(url, "vid.webm")
start := time.Now()
opusframes.Encode("vid.webm", "vid.of")
duration := time.Since(start)
log.Printf("Encoding took: %s", duration)
// Get the channel
msgServer, err := s.State.Guild(i.GuildID)
@@ -108,130 +90,26 @@ func playCommand(s *dg.Session, i *dg.InteractionCreate) {
}
func playOnVoiceChannel(voiceChannel *dg.VoiceConnection) {
log.Println("Starting ffmpeg stream")
// I got the original implementation from:
// https://github.com/nhooyr/botatouille/blob/7e1cd9d5a8d517fd43fd11599b2a62bf832a5c96/cmd/botatouille/music/music.go#L62-L104
// after hours of searching.
ffmpeg := exec.Command(
"ffmpeg",
"-i", "vid.webm",
"-hide_banner",
"-loglevel", "quiet",
"-f", "data",
"-map", "0:a",
"-ar", "48k",
"-ac", "2",
"-acodec", "libopus",
"-b:a", "128k",
"pipe:1")
ffmpegOut, err := ffmpeg.StdoutPipe()
log.Println("Decoding")
start := time.Now()
frames, err := opusframes.Decode("vid.of")
if err != nil {
log.Fatal(err)
log.Println("Decoding error: ", err)
return
}
duration := time.Since(start)
log.Printf("Decoding took: %s", duration)
err = ffmpeg.Start()
if err != nil {
log.Fatal(err)
}
packets := [][]byte{}
startTime := time.Now()
bytes_sent := 0
for {
// I read in the RFC that frames will not be bigger than this size
p := make([]byte, 960)
n, err := ffmpegOut.Read(p)
bytes_sent = bytes_sent + n
if err != nil {
log.Printf("Bytes: %d", n)
if err == io.EOF {
log.Println("Done streaming")
break
}
log.Fatal(err)
}
packets = append(packets, p[:n])
}
elapsedTime := time.Since(startTime)
log.Printf("bytes sent = %d", bytes_sent)
log.Printf("Took %s seconds to run", elapsedTime)
// for _, f := range frames {
// log.Printf("Got frame. Size: %d", len(f))
// }
voiceChannel.Speaking(true)
time.Sleep(time.Second * 2)
log.Println("Playing sound")
startTime = time.Now()
bytes_sent = 0
for _, p := range packets {
for _, p := range frames {
log.Printf("Sending packet: %d bytes", len(p))
bytes_sent += len(p)
voiceChannel.OpusSend <- p
}
elapsedTime = time.Since(startTime)
log.Printf("Packets = %d", len(packets))
log.Printf("Network bytes sent = %d", bytes_sent)
log.Printf("Took %s seconds to run", elapsedTime)
log.Println("Ended stream")
}
func handleSlashCommand(s *dg.Session, i *dg.InteractionCreate) {
log.Println("Command received?")
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
}
func ready(s *dg.Session, event *dg.Ready) {
commands := []*dg.ApplicationCommand{
{
Name: "play",
Description: "Play a song from youtube, spotify, apple music, etc",
Options: []*dg.ApplicationCommandOption{
{
Type: dg.ApplicationCommandOptionString,
Name: "url",
Description: "URL to the song",
Required: true,
},
},
},
}
env := dotenv.Load(".env")
appId := env.Get("DISCORD_APP_ID")
log.Println("Bot is ready")
log.Println("Bulk registering commands")
_, err := s.ApplicationCommandBulkOverwrite(appId, "338782945110392832", commands)
if err != nil {
log.Fatal(err)
}
log.Println("Done")
}
func downloadVideo(url string) error {
log.Printf("Downloading: %s", url)
os.Remove("vid.webm")
cmd := exec.Command(
"yt-dlp",
"-i",
"-q",
"-f", "bestaudio",
"-o",
"vid.%(ext)s",
url)
err := cmd.Run()
log.Println("Downloaded")
return err
}

97
pkg/bot/bot.go Normal file
View File

@@ -0,0 +1,97 @@
package bot
import (
"fmt"
"log"
dg "github.com/bwmarrin/discordgo"
)
type Bot struct {
token string
appId string
isPlaying bool
session *dg.Session
slashCommands map[string]func(s *dg.Session, i *dg.InteractionCreate)
}
func New(token, appId string) *Bot {
return &Bot{
token: token,
appId: appId,
slashCommands: make(map[string]func(s *dg.Session, i *dg.InteractionCreate)),
}
}
// TODO: Let's get some custom error types going
// at some point
func (b *Bot) Start() error {
var err error
b.session, err = dg.New(fmt.Sprintf("Bot %s", b.token))
if err != nil {
return err
}
b.session.AddHandler(b.ready)
b.session.Identify.Intents = dg.IntentsGuilds | dg.IntentsGuildMessages | dg.IntentsGuildVoiceStates
err = b.session.Open()
if err != nil {
return err
}
// Register slash commands
b.session.AddHandler(b.handleSlashCommand)
return nil
}
// TODO: Don't hard code the server. We can
// register the commands globally
func (b *Bot) Stop() {
cmds, _ := b.session.ApplicationCommands(b.appId, "338782945110392832")
for _, cmd := range cmds {
b.session.ApplicationCommandDelete(b.appId, "338782945110392832", cmd.ID)
}
b.session.Close()
}
func (b *Bot) AddSlashCommand(name string, handler func(*dg.Session, *dg.InteractionCreate)) {
b.slashCommands[name] = handler
}
func (b *Bot) handleSlashCommand(s *dg.Session, i *dg.InteractionCreate) {
log.Println("Command received?")
if h, ok := b.slashCommands[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
}
func (b *Bot) ready(s *dg.Session, event *dg.Ready) {
commands := []*dg.ApplicationCommand{
{
Name: "play",
Description: "Play a song from youtube, spotify, apple music, etc",
Options: []*dg.ApplicationCommandOption{
{
Type: dg.ApplicationCommandOptionString,
Name: "url",
Description: "URL to the song",
Required: true,
},
},
},
}
log.Println("Bot is ready")
log.Println("Bulk registering commands")
_, err := s.ApplicationCommandBulkOverwrite(b.appId, "338782945110392832", commands)
if err != nil {
log.Fatal(err)
}
log.Println("Done")
}

View File

@@ -0,0 +1,130 @@
package opusframes
// Package to encode/decode .of files.
// The .of file format will allow us to cache/preprocess
// files from the queue and save it in a format that we
// can quickly read and send to discord.
// The format spect is very simple:
// frame size: 2 bytes
// frame data: Up to 1275 bytes
// data will be stored in little endian format
import (
"bytes"
"encoding/binary"
"errors"
"io"
"log"
"os"
"os/exec"
)
type ErrFFMPEG error
type ErrEncoding error
// TODO: Allow streaming frames as they are encoded instead
// of having to wait for the whole file.
// We can probably do this by taking an io.Reader as input
// instead of just a file path. Then we return an io.Writer
// and allow the user to either stream each encoded frame
// or save it to a file. We could also make the Decode
// function take an io.Reader so these functions can
// just be piped into each other.
// TODO: Can we pack the frames into chunks of 960? I wonder
// if that would make playback smoother or just fuck the
// audio. We can try it at some point.
func Encode(input, output string) error {
ffmpeg := exec.Command(
"ffmpeg",
"-i", input,
"-hide_banner",
"-loglevel", "quiet",
"-f", "data",
"-map", "0:a",
"-ar", "48k",
"-ac", "2",
"-acodec", "libopus",
"-b:a", "128k",
"pipe:1")
ffmpegOut, err := ffmpeg.StdoutPipe()
if err != nil {
return ErrFFMPEG(err)
}
err = ffmpeg.Start()
if err != nil {
return ErrFFMPEG(err)
}
var fileBuffer bytes.Buffer
for {
p := make([]byte, 960)
n, err := ffmpegOut.Read(p)
binary.Write(&fileBuffer, binary.LittleEndian, uint16(n))
binary.Write(&fileBuffer, binary.LittleEndian, p[:n])
log.Printf("Frame of: %d", n)
if err != nil {
if err == io.EOF {
break
}
return ErrEncoding(err)
}
}
f, err := os.OpenFile(output, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
if err != nil {
log.Println(err)
}
defer f.Close()
f.Write(fileBuffer.Bytes())
return nil
}
var ErrMalformedFile error = errors.New("malformed file")
// TODO: Allow streaming frames as they are decoded instead
// of having to decode the whole file.
func Decode(input string) ([][]byte, error){
f, err := os.Open(input)
if err != nil {
return nil, err
}
defer f.Close()
// We read the first 2 bytes for the length
// then n bytes after that until we're done
frames := [][]byte{}
for {
var frameSize uint16
err = binary.Read(f, binary.LittleEndian, &frameSize)
if err != nil {
if err == io.EOF {
return frames, nil
}
return nil, err
}
frame := make([]byte, frameSize)
n, err := io.ReadFull(f, frame)
if n != int(frameSize) {
return nil, ErrMalformedFile
}
if err != nil {
return nil, err
}
frames = append(frames, frame)
}
}

24
pkg/youtube/youtube.go Normal file
View File

@@ -0,0 +1,24 @@
package youtube
import (
"os/exec"
)
func Download(url, outPath string) error {
cmd := exec.Command(
"yt-dlp",
"-i",
"-q",
"-f", "bestaudio",
"-o",
outPath,
url)
err := cmd.Run()
if err != nil {
return err
}
return err
}