diff --git a/helpers.go b/helpers.go index a578af0..4ec4095 100644 --- a/helpers.go +++ b/helpers.go @@ -42,7 +42,7 @@ func getLargestFormat(input goutubedl.Result, sizeLimit float64) (format goutube // Cringe YouTube func getLargestDashFormat(input goutubedl.Result, sizeLimit float64) (videoFormat goutubedl.Format, audioFormat goutubedl.Format, err error) { - // Get best audio format + // Get best Opus audio format var bestAudio goutubedl.Format for i := len(input.Formats())-1; i >= 0; i-- { if input.Formats()[i].ACodec == "opus" { @@ -51,7 +51,17 @@ func getLargestDashFormat(input goutubedl.Result, sizeLimit float64) (videoForma } } - // Get largest video that will fit in the limit with the audio + // If there aren't any Opus formats (somehow), use whatever else we can find + if len(bestAudio.FormatID) == 0 { + for i := len(input.Formats())-1; i >= 0; i-- { + if input.Formats()[i].ABR > bestAudio.ABR { + bestAudio = input.Formats()[i] + } + } + } + + + // Get largest VP9 video that will fit in the limit with the audio for i := len(input.Formats())-1; i >= 0; i-- { if input.Formats()[i].VCodec == "vp9" { var size float64 @@ -66,6 +76,36 @@ func getLargestDashFormat(input goutubedl.Result, sizeLimit float64) (videoForma } } + // If VP9 isn't available, just use whatever fits + for i := len(input.Formats())-1; i >= 0; i-- { + var size float64 + if input.Formats()[i].FilesizeApprox > 0 { + size = input.Formats()[i].FilesizeApprox + bestAudio.FilesizeApprox + } else { + size = input.Formats()[i].Filesize + bestAudio.Filesize + } + if size <= sizeLimit { + return input.Formats()[i], bestAudio, nil + } + } + return goutubedl.Format{}, goutubedl.Format{}, errors.New(fmt.Sprintf("Could not find a video format that is under the size limit of %f", sizeLimit)) } +func getLargestYTFormat(input goutubedl.Result, sizeLimit float64) (format goutubedl.Format, err error) { + for i := len(input.Formats())-1; i >= 0; i-- { + if input.Formats()[i].VBR > 0 && input.Formats()[i].ASR > 0 { + var size float64 + if input.Formats()[i].FilesizeApprox > 0 { + size = input.Formats()[i].FilesizeApprox + } else { + size = input.Formats()[i].Filesize + } + if size <= sizeLimit { + return input.Formats()[i], nil + } + } + } + + return goutubedl.Format{}, errors.New(fmt.Sprintf("Could not find a video format that is under the size limit of %f", sizeLimit)) +} diff --git a/messageCreate.go b/messageCreate.go index f774e0f..195d096 100644 --- a/messageCreate.go +++ b/messageCreate.go @@ -5,14 +5,14 @@ import ( "log" "regexp" "context" + "bytes" "github.com/bwmarrin/discordgo" "github.com/wader/goutubedl" "mvdan.cc/xurls/v2" // Peak lazy ) -// This function will be called (due to AddHandler above) every time a new -// message is created on any channel that the authenticated bot has access to. +// This function will be called every time a new message is created on any channel that the authenticated bot has access to. func messageCreate(session *discordgo.Session, message *discordgo.MessageCreate) { // Ignore all messages created by the bot itself if message.Author.ID == session.State.User.ID { @@ -106,10 +106,17 @@ func messageCreate(session *discordgo.Session, message *discordgo.MessageCreate) log.Println(err) continue } else { + /* videoChoice, audioChoice, err := getLargestDashFormat(result, 8*1024*1024) log.Printf("Choice: %s+%s | Size: %fM\n", videoChoice.FormatID, audioChoice.FormatID, (videoChoice.Filesize+audioChoice.Filesize)/1024/1024) if err == nil { downloadResult, err := result.Download(context.Background(), fmt.Sprintf("%s+%s", videoChoice.FormatID, audioChoice.FormatID)) + */ + choice, err := getLargestYTFormat(result, 8*1024*1024) + log.Printf("Choice: %s | Size: %fM\n", choice.FormatID, choice.FilesizeApprox/1024/1024) + if err == nil { + downloadResult, err := result.Download(context.Background(), choice.FormatID) + if err != nil { log.Println(err) //session.ChannelMessageSend(message.ChannelID, err.Error()) @@ -117,7 +124,8 @@ func messageCreate(session *discordgo.Session, message *discordgo.MessageCreate) } response.Files = append(response.Files, &discordgo.File { - Name: fmt.Sprintf("%s.%s", result.Info.ID, videoChoice.Ext), + //Name: fmt.Sprintf("%s.%s", result.Info.ID, videoChoice.Ext), + Name: fmt.Sprintf("%s.%s", result.Info.ID, choice.Ext), ContentType: "text/plain", // This is of course not true, but Discord doesn't give a shit Reader: downloadResult, })