2022-07-17 19:36:43 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/wader/goutubedl"
|
2022-09-16 20:24:24 -04:00
|
|
|
"golang.org/x/exp/slices"
|
2022-07-17 19:36:43 -04:00
|
|
|
)
|
|
|
|
|
2022-09-16 20:29:47 -04:00
|
|
|
var FileExtensions = []string {"webm","mp4","mkv"}
|
2022-09-16 20:24:24 -04:00
|
|
|
|
2022-07-17 19:36:43 -04:00
|
|
|
func getRedirectURL(input string) (string, error) {
|
|
|
|
request, err := http.NewRequest("GET", input, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(request)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Found new URL: %s", resp.Request.URL.String())
|
|
|
|
return resp.Request.URL.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLargestFormat(input goutubedl.Result, sizeLimit float64) (format goutubedl.Format, err error) {
|
|
|
|
for i := len(input.Formats())-1; i >= 0; i-- {
|
2022-07-17 23:06:46 -04:00
|
|
|
var size float64
|
|
|
|
if input.Formats()[i].FilesizeApprox > 0 {
|
|
|
|
size = input.Formats()[i].FilesizeApprox
|
|
|
|
} else {
|
|
|
|
size = input.Formats()[i].Filesize
|
|
|
|
}
|
2024-04-16 18:05:01 -04:00
|
|
|
|
|
|
|
if size <= sizeLimit && input.Formats()[i].VCodec != "h265" && input.Formats()[i].ACodec != "none" && slices.Contains(FileExtensions, input.Formats()[i].Ext) { // Discord can't embed HEVC
|
2022-07-17 19:36:43 -04:00
|
|
|
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))
|
|
|
|
}
|
2022-07-17 23:06:46 -04:00
|
|
|
|
|
|
|
// Cringe YouTube
|
|
|
|
func getLargestDashFormat(input goutubedl.Result, sizeLimit float64) (videoFormat goutubedl.Format, audioFormat goutubedl.Format, err error) {
|
2022-07-18 01:25:02 -04:00
|
|
|
// Get best Opus audio format
|
2022-07-17 23:06:46 -04:00
|
|
|
var bestAudio goutubedl.Format
|
|
|
|
for i := len(input.Formats())-1; i >= 0; i-- {
|
|
|
|
if input.Formats()[i].ACodec == "opus" {
|
|
|
|
bestAudio = input.Formats()[i]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 01:25:02 -04:00
|
|
|
// 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
|
2022-07-17 23:06:46 -04:00
|
|
|
for i := len(input.Formats())-1; i >= 0; i-- {
|
|
|
|
if input.Formats()[i].VCodec == "vp9" {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 01:25:02 -04:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-17 23:06:46 -04:00
|
|
|
return goutubedl.Format{}, goutubedl.Format{}, errors.New(fmt.Sprintf("Could not find a video format that is under the size limit of %f", sizeLimit))
|
|
|
|
}
|
|
|
|
|
2022-07-18 01:25:02 -04:00
|
|
|
func getLargestYTFormat(input goutubedl.Result, sizeLimit float64) (format goutubedl.Format, err error) {
|
|
|
|
for i := len(input.Formats())-1; i >= 0; i-- {
|
2023-08-11 04:18:22 -04:00
|
|
|
if input.Formats()[i].FPS > 0 && input.Formats()[i].ASR > 0 && slices.Contains(FileExtensions, input.Formats()[i].Ext) {
|
2022-07-18 01:25:02 -04:00
|
|
|
var size float64
|
|
|
|
if input.Formats()[i].FilesizeApprox > 0 {
|
|
|
|
size = input.Formats()[i].FilesizeApprox
|
|
|
|
} else {
|
|
|
|
size = input.Formats()[i].Filesize
|
|
|
|
}
|
2022-09-16 20:32:40 -04:00
|
|
|
if size <= sizeLimit {
|
2022-07-18 01:25:02 -04:00
|
|
|
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))
|
|
|
|
}
|