115 lines
3.9 KiB
Go
115 lines
3.9 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"fmt"
|
|
"net/http"
|
|
"errors"
|
|
|
|
"github.com/wader/goutubedl"
|
|
"golang.org/x/exp/slices"
|
|
)
|
|
|
|
var FileExtensions = []string {"webm","mp4","mkv"}
|
|
|
|
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-- {
|
|
var size float64
|
|
if input.Formats()[i].FilesizeApprox > 0 {
|
|
size = input.Formats()[i].FilesizeApprox
|
|
} else {
|
|
size = input.Formats()[i].Filesize
|
|
}
|
|
|
|
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
|
|
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))
|
|
}
|
|
|
|
// Cringe YouTube
|
|
func getLargestDashFormat(input goutubedl.Result, sizeLimit float64) (videoFormat goutubedl.Format, audioFormat goutubedl.Format, err error) {
|
|
// Get best Opus audio format
|
|
var bestAudio goutubedl.Format
|
|
for i := len(input.Formats())-1; i >= 0; i-- {
|
|
if input.Formats()[i].ACodec == "opus" {
|
|
bestAudio = input.Formats()[i]
|
|
break
|
|
}
|
|
}
|
|
|
|
// 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
|
|
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
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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].FPS > 0 && input.Formats()[i].ASR > 0 && slices.Contains(FileExtensions, input.Formats()[i].Ext) {
|
|
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))
|
|
}
|