@@ -8,68 +8,59 @@ import (
88
99type FormatList []Format
1010
11- // FindByQuality returns the first format matching Quality or QualityLabel
12- //
13- // Examples: tiny, small, medium, large, 720p, hd720, hd1080
14- func (list FormatList ) FindByQuality (quality string ) * Format {
11+ // Type returns a new FormatList filtered by itag
12+ func (list FormatList ) Select (f func (Format ) bool ) (result FormatList ) {
1513 for i := range list {
16- if list [i ]. Quality == quality || list [ i ]. QualityLabel == quality {
17- return & list [i ]
14+ if f ( list [i ]) {
15+ result = append ( result , list [i ])
1816 }
1917 }
20- return nil
18+ return result
2119}
2220
23- // FindByItag returns the first format matching the itag number
24- func (list FormatList ) FindByItag (itagNo int ) * Format {
25- for i := range list {
26- if list [i ].ItagNo == itagNo {
27- return & list [i ]
28- }
29- }
30- return nil
21+ // Type returns a new FormatList filtered by itag
22+ func (list FormatList ) Itag (itagNo int ) FormatList {
23+ return list .Select (func (f Format ) bool {
24+ return f .ItagNo == itagNo
25+ })
3126}
3227
33- // Type returns a new FormatList filtered by mime type of video
34- func (list FormatList ) Type (t string ) (result FormatList ) {
35- for i := range list {
36- if strings .Contains (list [i ].MimeType , t ) {
37- result = append (result , list [i ])
38- }
39- }
40- return result
28+ // Type returns a new FormatList filtered by mime type
29+ func (list FormatList ) Type (value string ) FormatList {
30+ return list .Select (func (f Format ) bool {
31+ return strings .Contains (f .MimeType , value )
32+ })
33+ }
34+
35+ // Type returns a new FormatList filtered by display name
36+ func (list FormatList ) Language (displayName string ) FormatList {
37+ return list .Select (func (f Format ) bool {
38+ return f .LanguageDisplayName () == displayName
39+ })
4140}
4241
4342// Quality returns a new FormatList filtered by quality, quality label or itag,
4443// but not audio quality
45- func (list FormatList ) Quality (quality string ) (result FormatList ) {
46- for _ , f := range list {
47- itag , _ := strconv .Atoi (quality )
48- if itag == f .ItagNo || strings .Contains (f .Quality , quality ) || strings .Contains (f .QualityLabel , quality ) {
49- result = append (result , f )
50- }
51- }
52- return result
44+ func (list FormatList ) Quality (quality string ) FormatList {
45+ itag , _ := strconv .Atoi (quality )
46+
47+ return list .Select (func (f Format ) bool {
48+ return itag == f .ItagNo || strings .Contains (f .Quality , quality ) || strings .Contains (f .QualityLabel , quality )
49+ })
5350}
5451
5552// AudioChannels returns a new FormatList filtered by the matching AudioChannels
56- func (list FormatList ) AudioChannels (n int ) (result FormatList ) {
57- for _ , f := range list {
58- if f .AudioChannels == n {
59- result = append (result , f )
60- }
61- }
62- return result
53+ func (list FormatList ) AudioChannels (n int ) FormatList {
54+ return list .Select (func (f Format ) bool {
55+ return f .AudioChannels == n
56+ })
6357}
6458
6559// AudioChannels returns a new FormatList filtered by the matching AudioChannels
66- func (list FormatList ) WithAudioChannels () (result FormatList ) {
67- for _ , f := range list {
68- if f .AudioChannels > 0 {
69- result = append (result , f )
70- }
71- }
72- return result
60+ func (list FormatList ) WithAudioChannels () FormatList {
61+ return list .Select (func (f Format ) bool {
62+ return f .AudioChannels > 0
63+ })
7364}
7465
7566// FilterQuality reduces the format list to formats matching the quality
0 commit comments