forked from lithdew/youtube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.go
127 lines (94 loc) · 3.93 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
package youtube
import (
"github.com/lithdew/bytesutil"
"github.com/valyala/fastjson"
)
type Format struct {
AverageBitrate uint `json:"averageBitrate"`
ApproxDurationMs string `json:"approxDurationMs"`
ContentLength string `json:"contentLength"`
Bitrate uint `json:"bitrate"`
URL *string `json:"url,omitempty"`
Cipher *Cipher `json:"cipher,omitempty"`
Quality string `json:"quality"`
QualityLabel string `json:"qualityLabel"`
ITag uint `json:"itag"`
MIMEType string `json:"mimeType"`
Width uint `json:"width"`
Height uint `json:"height"`
FPS *uint `json:"fps,omitempty"`
ColorInfo *ColorInfo `json:"colorInfo,omitempty"`
AudioQuality *string `json:"audioQuality,omitempty"`
AudioChannels *uint `json:"audioChannels,omitempty"`
AudioSampleRate *string `json:"audioSampleRate,omitempty"`
InitRange *TimeRange `json:"initRange,omitempty"`
IndexRange *TimeRange `json:"indexRange,omitempty"`
LastModified string `json:"lastModified"`
HighReplication bool `json:"highReplication,omitempty"`
ProjectionType string `json:"projectionType"`
}
func (f Format) FileExtension() string {
return ITags[f.ITag].Extension
}
type ColorInfo struct {
Primaries string `json:"primaries"`
TransferCharacteristics string `json:"transferCharacteristics"`
MatrixCoefficients string `json:"matrixCoefficients"`
}
func ParseColorInfoJSON(v *fastjson.Value) ColorInfo {
return ColorInfo{
Primaries: string(v.GetStringBytes("primaries")),
TransferCharacteristics: string(v.GetStringBytes("transferCharacteristics")),
MatrixCoefficients: string(v.GetStringBytes("matrixCoefficients")),
}
}
type TimeRange struct {
Start string `json:"start"`
End string `json:"end"`
}
func ParseTimeRangeJSON(v *fastjson.Value) TimeRange {
return TimeRange{
Start: string(v.GetStringBytes("start")),
End: string(v.GetStringBytes("end")),
}
}
func ParseFormatJSON(v *fastjson.Value) Format {
var format Format
format.AverageBitrate = v.GetUint("averageBitrate")
format.ApproxDurationMs = bytesutil.String(v.GetStringBytes("approxDurationMs"))
format.ContentLength = bytesutil.String(v.GetStringBytes("contentLength"))
format.Bitrate = v.GetUint("bitrate")
if u := v.GetStringBytes("url"); len(u) > 0 {
format.URL = func(s string) *string { return &s }(bytesutil.String(u))
}
if u := v.Get("cipher"); u != nil {
format.Cipher = func(c Cipher) *Cipher { return &c }(ParseCipherJSON(u))
}
format.Quality = bytesutil.String(v.GetStringBytes("quality"))
format.QualityLabel = bytesutil.String(v.GetStringBytes("qualityLabel"))
format.ITag = v.GetUint("itag")
format.MIMEType = bytesutil.String(v.GetStringBytes("mimeType"))
format.Width = v.GetUint("width")
format.Height = v.GetUint("height")
format.FPS = func(u uint) *uint { return &u }(v.GetUint("fps"))
if colorInfo := v.Get("colorInfo"); colorInfo != nil {
format.ColorInfo = func(i ColorInfo) *ColorInfo { return &i }(ParseColorInfoJSON(colorInfo))
}
if audioQuality := v.GetStringBytes("audioQuality"); len(audioQuality) > 0 {
format.AudioQuality = func(s string) *string { return &s }(string(audioQuality))
}
format.AudioChannels = func(u uint) *uint { return &u }(v.GetUint("audioChannels"))
if audioSampleRate := v.GetStringBytes("audioSampleRate"); len(audioSampleRate) > 0 {
format.AudioSampleRate = func(s string) *string { return &s }(string(audioSampleRate))
}
if initRange := v.Get("initRange"); initRange != nil {
format.InitRange = func(t TimeRange) *TimeRange { return &t }(ParseTimeRangeJSON(initRange))
}
if indexRange := v.Get("indexRange"); indexRange != nil {
format.IndexRange = func(t TimeRange) *TimeRange { return &t }(ParseTimeRangeJSON(indexRange))
}
format.HighReplication = v.GetBool("highReplication")
format.LastModified = bytesutil.String(v.GetStringBytes("lastModified"))
format.ProjectionType = bytesutil.String(v.GetStringBytes("projectionType"))
return format
}