Improved YT support

This commit is contained in:
Logan G 2022-07-17 23:25:02 -06:00
parent 7f2c9fa130
commit 872c72c0de
Signed by: logan
GPG key ID: E328528C921E7A7A
2 changed files with 53 additions and 5 deletions

View file

@ -42,7 +42,7 @@ func getLargestFormat(input goutubedl.Result, sizeLimit float64) (format goutube
// Cringe YouTube // Cringe YouTube
func getLargestDashFormat(input goutubedl.Result, sizeLimit float64) (videoFormat goutubedl.Format, audioFormat goutubedl.Format, err error) { 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 var bestAudio goutubedl.Format
for i := len(input.Formats())-1; i >= 0; i-- { for i := len(input.Formats())-1; i >= 0; i-- {
if input.Formats()[i].ACodec == "opus" { 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-- { for i := len(input.Formats())-1; i >= 0; i-- {
if input.Formats()[i].VCodec == "vp9" { if input.Formats()[i].VCodec == "vp9" {
var size float64 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)) 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))
}

View file

@ -5,14 +5,14 @@ import (
"log" "log"
"regexp" "regexp"
"context" "context"
"bytes"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"github.com/wader/goutubedl" "github.com/wader/goutubedl"
"mvdan.cc/xurls/v2" // Peak lazy "mvdan.cc/xurls/v2" // Peak lazy
) )
// This function will be called (due to AddHandler above) every time a new // This function will be called every time a new message is created on any channel that the authenticated bot has access to.
// message is created on any channel that the authenticated bot has access to.
func messageCreate(session *discordgo.Session, message *discordgo.MessageCreate) { func messageCreate(session *discordgo.Session, message *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself // Ignore all messages created by the bot itself
if message.Author.ID == session.State.User.ID { if message.Author.ID == session.State.User.ID {
@ -106,10 +106,17 @@ func messageCreate(session *discordgo.Session, message *discordgo.MessageCreate)
log.Println(err) log.Println(err)
continue continue
} else { } else {
/*
videoChoice, audioChoice, err := getLargestDashFormat(result, 8*1024*1024) 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) log.Printf("Choice: %s+%s | Size: %fM\n", videoChoice.FormatID, audioChoice.FormatID, (videoChoice.Filesize+audioChoice.Filesize)/1024/1024)
if err == nil { if err == nil {
downloadResult, err := result.Download(context.Background(), fmt.Sprintf("%s+%s", videoChoice.FormatID, audioChoice.FormatID)) 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 { if err != nil {
log.Println(err) log.Println(err)
//session.ChannelMessageSend(message.ChannelID, err.Error()) //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 { 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 ContentType: "text/plain", // This is of course not true, but Discord doesn't give a shit
Reader: downloadResult, Reader: downloadResult,
}) })