2021-09-24 05:15:40 +00:00
|
|
|
package youtube
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
youtubev2 "github.com/kkdai/youtube/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func FormatDebugString(format *youtubev2.Format, includeURL bool) string {
|
2021-10-08 14:38:35 +00:00
|
|
|
url := "hidden"
|
2021-09-24 05:15:40 +00:00
|
|
|
if includeURL {
|
|
|
|
url = format.URL
|
|
|
|
}
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"iTag = %d, mime_type = %s, quality = %s, quality_label = %s, bitrate = %d, fps = %d, width = %d, height = %d, content_length = %d, duration = %v, audio_channels = %d, audio_sample_rate = %s, audio_quality = %s, url = %s",
|
|
|
|
format.ItagNo,
|
|
|
|
format.MimeType,
|
|
|
|
format.Quality,
|
|
|
|
format.QualityLabel,
|
|
|
|
format.Bitrate,
|
|
|
|
format.FPS,
|
|
|
|
format.Width,
|
|
|
|
format.Height,
|
|
|
|
format.ContentLength,
|
|
|
|
format.ApproxDurationMs,
|
|
|
|
format.AudioChannels,
|
|
|
|
format.AudioSampleRate,
|
|
|
|
format.AudioQuality,
|
|
|
|
url,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SortAudio returns the provided formats ordered in descending preferred
|
2021-10-19 15:37:54 +00:00
|
|
|
// order. The ideal candidate is opus-encoded stereo audio in a webm container,
|
|
|
|
// with the lowest available bitrate.
|
2021-09-24 05:15:40 +00:00
|
|
|
func SortAudio(inFormats youtubev2.FormatList) youtubev2.FormatList {
|
|
|
|
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 {
|
2021-10-19 15:37:54 +00:00
|
|
|
return formats[i].ContentLength < formats[j].ContentLength
|
2021-09-24 05:15:40 +00:00
|
|
|
}
|
|
|
|
return isStereoI
|
|
|
|
}
|
|
|
|
return isOpusI
|
|
|
|
})
|
|
|
|
return formats
|
|
|
|
}
|
|
|
|
|
|
|
|
// SortVideo returns the provided formats ordered in descending preferred
|
2021-10-08 14:38:35 +00:00
|
|
|
// order. The ideal candidate is video in an mp4 container with a low
|
2021-09-24 05:15:40 +00:00
|
|
|
// bitrate, with audio channels (needed to allow synced playback on the
|
|
|
|
// website).
|
|
|
|
func SortVideo(inFormats youtubev2.FormatList) youtubev2.FormatList {
|
|
|
|
var formats youtubev2.FormatList
|
|
|
|
for _, format := range inFormats {
|
2021-10-19 15:37:54 +00:00
|
|
|
if format.FPS > 0 && format.ContentLength > 0 {
|
2021-09-24 05:15:40 +00:00
|
|
|
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 {
|
2021-10-19 15:37:54 +00:00
|
|
|
return formats[i].ContentLength < formats[j].ContentLength
|
2021-09-24 05:15:40 +00:00
|
|
|
}
|
2021-10-08 14:38:35 +00:00
|
|
|
return isMP4I
|
2021-09-24 05:15:40 +00:00
|
|
|
})
|
|
|
|
return formats
|
|
|
|
}
|