This repository has been archived by the owner on Aug 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 151
/
format.go
146 lines (131 loc) · 2.98 KB
/
format.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
package ytdl
import (
"fmt"
"net/url"
"strconv"
"strings"
)
// FormatKey is a string type containing a key in a video format map
type FormatKey string
// Available format Keys
const (
FormatExtensionKey FormatKey = "ext"
FormatResolutionKey FormatKey = "res"
FormatVideoEncodingKey FormatKey = "videnc"
FormatAudioEncodingKey FormatKey = "audenc"
FormatItagKey FormatKey = "itag"
FormatAudioBitrateKey FormatKey = "audbr"
FormatFPSKey FormatKey = "fps"
)
type Range struct {
Start string `json:"start"`
End string `json:"end"`
}
type Format struct {
Itag
Adaptive bool
// FromDASH indicates that the stream
// was extracted from the DASH manifest file
FromDASH bool
Index *Range
Init *Range
url string
s string
sig string
stream string
conn string
sp string
}
func parseFormat(queryString string) (*Format, error) {
query, err := url.ParseQuery(queryString)
if err != nil {
return nil, err
}
format := Format{}
for k, v := range query {
switch k {
case "itag":
i, err := strconv.Atoi(v[0])
if err != nil {
return nil, fmt.Errorf("unable to parse itag param: %w", err)
}
itag := getItag(i)
if itag == nil {
return nil, fmt.Errorf("no metadata found for itag: %v", i)
}
format.Itag = *itag
case "url":
format.url = v[0]
case "s":
format.s = v[0]
case "sig":
format.sig = v[0]
case "stream":
format.stream = v[0]
case "conn":
format.conn = v[0]
case "sp":
format.sp = v[0]
case "index":
format.Index, err = parseRange(v[0])
if err != nil {
return nil, fmt.Errorf("unable to parse index range")
}
case "init":
format.Init, err = parseRange(v[0])
if err != nil {
return nil, fmt.Errorf("unable to parse init range")
}
}
}
return &format, nil
}
func parseRange(s string) (*Range, error) {
sa := strings.Split(s, "-")
if len(sa) != 2 {
return nil, fmt.Errorf("Invalid range")
}
return &Range{Start: sa[0], End: sa[1]}, nil
}
// ValueForKey gets the format value for a format key, used for filtering
func (f *Format) ValueForKey(key FormatKey) interface{} {
switch key {
case FormatItagKey:
return f.Itag.Number
case FormatExtensionKey:
return f.Extension
case FormatResolutionKey:
return f.Resolution
case FormatVideoEncodingKey:
return f.VideoEncoding
case FormatAudioEncodingKey:
return f.AudioEncoding
case FormatAudioBitrateKey:
return f.AudioBitrate
case FormatFPSKey:
return f.FPS
default:
return fmt.Errorf("Unknown format key: %v", key)
}
}
func (f *Format) CompareKey(other *Format, key FormatKey) int {
switch key {
case FormatResolutionKey:
return f.resolution() - other.resolution()
case FormatAudioBitrateKey:
return f.AudioBitrate - other.AudioBitrate
case FormatFPSKey:
return f.FPS - other.FPS
default:
return 0
}
}
// width in pixels
func (f *Format) resolution() int {
res := f.Itag.Resolution
if len(res) < 2 {
return 0
}
width, _ := strconv.Atoi(res[:len(res)-2])
return width
}