-
Notifications
You must be signed in to change notification settings - Fork 433
/
format_list.go
148 lines (133 loc) · 4.33 KB
/
format_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package youtube
import (
"sort"
"strconv"
"strings"
)
type FormatList []Format
// Type returns a new FormatList filtered by itag
func (list FormatList) Select(f func(Format) bool) (result FormatList) {
for i := range list {
if f(list[i]) {
result = append(result, list[i])
}
}
return result
}
// Type returns a new FormatList filtered by itag
func (list FormatList) Itag(itagNo int) FormatList {
return list.Select(func(f Format) bool {
return f.ItagNo == itagNo
})
}
// Type returns a new FormatList filtered by mime type
func (list FormatList) Type(value string) FormatList {
return list.Select(func(f Format) bool {
return strings.Contains(f.MimeType, value)
})
}
// Type returns a new FormatList filtered by display name
func (list FormatList) Language(displayName string) FormatList {
return list.Select(func(f Format) bool {
return f.LanguageDisplayName() == displayName
})
}
// Quality returns a new FormatList filtered by quality, quality label or itag,
// but not audio quality
func (list FormatList) Quality(quality string) FormatList {
itag, _ := strconv.Atoi(quality)
return list.Select(func(f Format) bool {
return itag == f.ItagNo || strings.Contains(f.Quality, quality) || strings.Contains(f.QualityLabel, quality)
})
}
// AudioChannels returns a new FormatList filtered by the matching AudioChannels
func (list FormatList) AudioChannels(n int) FormatList {
return list.Select(func(f Format) bool {
return f.AudioChannels == n
})
}
// AudioChannels returns a new FormatList filtered by the matching AudioChannels
func (list FormatList) WithAudioChannels() FormatList {
return list.Select(func(f Format) bool {
return f.AudioChannels > 0
})
}
// FilterQuality reduces the format list to formats matching the quality
func (v *Video) FilterQuality(quality string) {
v.Formats = v.Formats.Quality(quality)
v.Formats.Sort()
}
// Sort sorts all formats fields
func (list FormatList) Sort() {
sort.SliceStable(list, func(i, j int) bool {
return sortFormat(i, j, list)
})
}
// sortFormat sorts video by resolution, FPS, codec (av01, vp9, avc1), bitrate
// sorts audio by default, codec (mp4, opus), channels, bitrate, sample rate
func sortFormat(i int, j int, formats FormatList) bool {
// Sort by Width
if formats[i].Width == formats[j].Width {
// Format 137 downloads slowly, give it less priority
// see https://github.com/kkdai/youtube/pull/171
switch 137 {
case formats[i].ItagNo:
return false
case formats[j].ItagNo:
return true
}
// Sort by FPS
if formats[i].FPS == formats[j].FPS {
if formats[i].FPS == 0 && formats[i].AudioChannels > 0 && formats[j].AudioChannels > 0 {
// Audio
// Sort by default
if (formats[i].AudioTrack == nil && formats[j].AudioTrack == nil) || (formats[i].AudioTrack != nil && formats[j].AudioTrack != nil && formats[i].AudioTrack.AudioIsDefault == formats[j].AudioTrack.AudioIsDefault) {
// Sort by codec
codec := map[int]int{}
for _, index := range []int{i, j} {
if strings.Contains(formats[index].MimeType, "mp4") {
codec[index] = 1
} else if strings.Contains(formats[index].MimeType, "opus") {
codec[index] = 2
}
}
if codec[i] == codec[j] {
// Sort by Audio Channel
if formats[i].AudioChannels == formats[j].AudioChannels {
// Sort by Audio Bitrate
if formats[i].Bitrate == formats[j].Bitrate {
// Sort by Audio Sample Rate
return formats[i].AudioSampleRate > formats[j].AudioSampleRate
}
return formats[i].Bitrate > formats[j].Bitrate
}
return formats[i].AudioChannels > formats[j].AudioChannels
}
return codec[i] < codec[j]
} else if formats[i].AudioTrack != nil && formats[i].AudioTrack.AudioIsDefault {
return true
}
return false
}
// Video
// Sort by codec
codec := map[int]int{}
for _, index := range []int{i, j} {
if strings.Contains(formats[index].MimeType, "av01") {
codec[index] = 1
} else if strings.Contains(formats[index].MimeType, "vp9") {
codec[index] = 2
} else if strings.Contains(formats[index].MimeType, "avc1") {
codec[index] = 3
}
}
if codec[i] == codec[j] {
// Sort by Audio Bitrate
return formats[i].Bitrate > formats[j].Bitrate
}
return codec[i] < codec[j]
}
return formats[i].FPS > formats[j].FPS
}
return formats[i].Width > formats[j].Width
}