clipper/backend/media/youtube.go

67 lines
2.0 KiB
Go
Raw Normal View History

2021-10-27 20:17:59 +00:00
package media
import (
"sort"
"strings"
youtubev2 "github.com/kkdai/youtube/v2"
)
2021-10-28 01:23:45 +00:00
// FilterYoutubeAudio returns the provided formats ordered in descending preferred
// order. It may have fewer items than the original slice, or none at all.
func FilterYoutubeAudio(inFormats youtubev2.FormatList) youtubev2.FormatList {
2021-10-27 20:17:59 +00:00
var formats youtubev2.FormatList
for _, format := range inFormats {
if format.FPS == 0 && format.AudioChannels > 0 {
formats = append(formats, format)
}
}
sort.SliceStable(formats, func(i, j int) bool {
isOpusI := strings.Contains(formats[i].MimeType, "opus")
isOpusJ := strings.Contains(formats[j].MimeType, "opus")
if isOpusI && isOpusJ {
isStereoI := formats[i].AudioChannels == 2
isStereoJ := formats[j].AudioChannels == 2
if isStereoI && isStereoJ {
return formats[i].ContentLength < formats[j].ContentLength
}
return isStereoI
}
return isOpusI
})
return formats
}
2021-10-28 01:23:45 +00:00
// FilterYoutubeVideo returns the provided formats ordered in descending preferred
// order. It may have fewer items than the original slice, or none at all.
func FilterYoutubeVideo(inFormats youtubev2.FormatList) youtubev2.FormatList {
var formats youtubev2.FormatList
for _, format := range inFormats {
if format.FPS > 0 && format.ContentLength > 0 {
formats = append(formats, format)
}
}
sort.SliceStable(formats, func(i, j int) bool {
isMP4I := strings.Contains(formats[i].MimeType, "mp4")
isMP4J := strings.Contains(formats[j].MimeType, "mp4")
if isMP4I && isMP4J {
return formats[i].ContentLength < formats[j].ContentLength
}
return isMP4I
})
return formats
}
2021-11-21 19:43:40 +00:00
// SortYoutubeThumbnails sorts the provided thumbnails ordered in descending preferred order.
func SortYoutubeThumbnails(thumbnails youtubev2.Thumbnails) {
sort.SliceStable(thumbnails, func(i, j int) bool {
// TODO: get rid of these magic 177s.
isMinSizeI := thumbnails[i].Width >= 177
isMinSizeJ := thumbnails[j].Width >= 177
if isMinSizeI && isMinSizeJ {
return thumbnails[i].Width < thumbnails[j].Width
}
return isMinSizeI
})
}