35 lines
962 B
Go
35 lines
962 B
Go
package media
|
|
|
|
import (
|
|
"sort"
|
|
"strings"
|
|
|
|
youtubev2 "github.com/kkdai/youtube/v2"
|
|
)
|
|
|
|
// sortAudio returns the provided formats ordered in descending preferred
|
|
// order. The ideal candidate is opus-encoded stereo audio in a webm container,
|
|
// with the lowest available bitrate.
|
|
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 {
|
|
return formats[i].ContentLength < formats[j].ContentLength
|
|
}
|
|
return isStereoI
|
|
}
|
|
return isOpusI
|
|
})
|
|
return formats
|
|
}
|