Compare commits
2 commits
7f2c9fa130
...
9b5b980e1d
Author | SHA1 | Date | |
---|---|---|---|
9b5b980e1d | |||
872c72c0de |
2 changed files with 84 additions and 5 deletions
44
helpers.go
44
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))
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
})
|
||||
|
@ -128,6 +136,37 @@ func messageCreate(session *discordgo.Session, message *discordgo.MessageCreate)
|
|||
}
|
||||
}
|
||||
|
||||
respond = true
|
||||
} else if output, _ := regexp.MatchString("(http.*instagram\\.com/p/.*)|(http.*instagram\\.com/tv/.*)|(http.*instagram\\.com/reel/.*)", url); output {
|
||||
log.Println("Instagram detected.")
|
||||
|
||||
response.Content = response.Content + fmt.Sprintf("<%s>", regexp.MustCompile("http.*instagram\\.com").ReplaceAllString(url, "https://bibliogram.art"))
|
||||
|
||||
result, err := goutubedl.New(context.Background(), url, goutubedl.Options{})
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
} else {
|
||||
downloadResult, err := result.Download(context.Background(), result.Formats()[0].FormatID) // Insta only has one possible format to download :(
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
buf.ReadFrom(downloadResult)
|
||||
|
||||
downloadResult.Close()
|
||||
|
||||
if buf.Len() <= 8*1024*1024 {
|
||||
response.Files = append(response.Files, &discordgo.File {
|
||||
Name: fmt.Sprintf("%s.%s", result.Info.ID, result.Formats()[0].Ext),
|
||||
ContentType: "text/plain", // This is of course not true, but Discord doesn't give a shit
|
||||
Reader: buf,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
respond = true
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue