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 148
/
Copy pathvideo_info.go
339 lines (292 loc) · 9.5 KB
/
video_info.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package ytdl
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/url"
"regexp"
"strconv"
"strings"
"time"
"github.com/antchfx/jsonquery"
"github.com/rs/zerolog/log"
)
const youtubeBaseURL = "https://www.youtube.com/watch"
const youtubeEmbeddedBaseURL = "https://www.youtube.com/embed/"
const youtubeVideoEURL = "https://youtube.googleapis.com/v/"
const youtubeVideoInfoURL = "https://www.youtube.com/get_video_info"
const youtubeDateFormat = "2006-01-02"
// VideoInfo contains the info a youtube video
type VideoInfo struct {
ID string // The video ID
Title string // The video title
Description string // The video description
DatePublished time.Time // The date the video was published
Formats FormatList // Formats the video is available in
DASHManifestURL string // URI of the DASH manifest file
HLSManifestURL string // URI of the HLS manifest file
Keywords []string // List of keywords associated with the video
Uploader string // Author of the video
Song string
Artist string
Album string
Writers string
Duration time.Duration // Duration of the video
htmlPlayerFile string
}
// GetVideoInfo fetches info from a url string, url object, or a url string
func GetVideoInfo(cx context.Context, value interface{}) (*VideoInfo, error) {
return DefaultClient.GetVideoInfo(cx, value)
}
// GetVideoInfo fetches info from a url string, url object, or a url string
func (c *Client) GetVideoInfo(cx context.Context, value interface{}) (*VideoInfo, error) {
switch t := value.(type) {
case *url.URL:
videoID := extractVideoID(t)
if len(videoID) == 0 {
return nil, fmt.Errorf("invalid youtube URL, no video id")
}
return c.GetVideoInfoFromID(cx, videoID)
case string:
if strings.HasPrefix(t, "https://") {
uri, err := url.ParseRequestURI(t)
if err != nil {
return nil, err
}
return c.GetVideoInfo(cx, uri)
}
return c.GetVideoInfoFromID(cx, t)
default:
return nil, fmt.Errorf("Identifier type must be a string, *url.URL, or []byte")
}
}
// GetVideoInfoFromShortURL fetches video info from a short youtube url
func extractVideoID(u *url.URL) string {
switch u.Host {
case "www.youtube.com", "youtube.com", "m.youtube.com":
if u.Path == "/watch" {
return u.Query().Get("v")
}
if strings.HasPrefix(u.Path, "/embed/") {
return u.Path[7:]
}
case "youtu.be":
if len(u.Path) > 1 {
return u.Path[1:]
}
}
return ""
}
// GetVideoInfoFromID fetches video info from a youtube video id
func (c *Client) GetVideoInfoFromID(cx context.Context, id string) (*VideoInfo, error) {
body, err := c.httpGetAndCheckResponseReadBody(cx, youtubeBaseURL+"?v="+id+"&gl=US&hl=en&has_verified=1&bpctr=9999999999")
if err != nil {
return nil, err
}
return c.getVideoInfoFromHTML(cx, id, body)
}
// GetDownloadURL gets the download url for a format
func (c *Client) GetDownloadURL(cx context.Context, info *VideoInfo, format *Format) (*url.URL, error) {
return c.getDownloadURL(cx, format, info.htmlPlayerFile)
}
// GetThumbnailURL returns a url for the thumbnail image
// with the given quality
func (info *VideoInfo) GetThumbnailURL(quality ThumbnailQuality) *url.URL {
u, _ := url.Parse(fmt.Sprintf("http://img.youtube.com/vi/%s/%s.jpg",
info.ID, quality))
return u
}
// Download is a convenience method to download a format to an io.Writer
func (c *Client) Download(cx context.Context, info *VideoInfo, format *Format, dest io.Writer) error {
u, err := c.GetDownloadURL(cx, info, format)
if err != nil {
return err
}
resp, err := c.httpGetAndCheckResponse(cx, u.String())
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(dest, resp.Body)
return err
}
var (
regexpPlayerConfig = regexp.MustCompile("ytplayer\\.config = (.*?);ytplayer\\.")
regexpPlayerConfigEmbedded = regexp.MustCompile("yt.setConfig\\({'PLAYER_CONFIG': (.*?)}\\);")
regexpInitialData = regexp.MustCompile(`\["ytInitialData"\] = (.+);`)
regexpInitialPlayerResponse = regexp.MustCompile(`\["ytInitialPlayerResponse"\] = (.+);`)
)
func (c *Client) getVideoInfoFromHTML(cx context.Context, id string, html []byte) (*VideoInfo, error) {
info := &VideoInfo{}
if matches := regexpInitialData.FindSubmatch(html); len(matches) > 0 {
if err := info.addMetadata(matches[1]); err != nil {
log.Debug().Msgf("Unable to parse metadata rows: %v", err)
}
}
info.ID = id
var jsonConfig playerConfig
// match json in javascript
if matches := regexpPlayerConfig.FindSubmatch(html); len(matches) > 1 {
err := json.Unmarshal(matches[1], &jsonConfig)
if err != nil {
return nil, err
}
} else {
log.Debug().Msg("Unable to extract json from default url, trying embedded url")
embeddedInfo, err := c.getPlayerConfigFromEmbedded(cx, id)
if err != nil {
return nil, err
}
info.htmlPlayerFile = embeddedInfo.Assets.JS
query := url.Values{
"video_id": []string{id},
"eurl": []string{youtubeVideoEURL + id},
}
body, err := c.httpGetAndCheckResponseReadBody(cx, youtubeVideoInfoURL+"?"+query.Encode())
if err != nil {
return nil, fmt.Errorf("Unable to read video info: %w", err)
}
query, err = url.ParseQuery(string(body))
if err != nil {
return nil, fmt.Errorf("Unable to parse video info data: %w", err)
}
for k, v := range query {
switch k {
case "errorcode":
jsonConfig.Args.Errorcode = v[0]
case "reason":
jsonConfig.Args.Reason = v[0]
case "status":
jsonConfig.Args.Status = v[0]
case "player_response":
jsonConfig.Args.PlayerResponse = v[0]
case "url_encoded_fmt_stream_map":
jsonConfig.Args.URLEncodedFmtStreamMap = v[0]
case "adaptive_fmts":
jsonConfig.Args.AdaptiveFmts = v[0]
case "dashmpd":
jsonConfig.Args.Dashmpd = v[0]
default:
// log.Debug().Msgf("unknown query param: %v", k)
}
}
}
inf := jsonConfig.Args
if inf.Status == "fail" {
return nil, fmt.Errorf("Error %s:%s", inf.Errorcode, inf.Reason)
}
var formats FormatList
c.addFormatsByQueryStrings(&formats, strings.NewReader(inf.URLEncodedFmtStreamMap), false)
if inf.AdaptiveFmts != "" {
c.addFormatsByQueryStrings(&formats, strings.NewReader(inf.AdaptiveFmts), true)
}
if inf.PlayerResponse != "" {
response := &playerResponse{}
if err := json.Unmarshal([]byte(inf.PlayerResponse), &response); err != nil {
return nil, fmt.Errorf("Couldn't parse player response: %w", err)
}
info.DASHManifestURL = response.StreamingData.DashManifestUrl
info.HLSManifestURL = response.StreamingData.HlsManifestUrl
if response.PlayabilityStatus.Status != "OK" {
return nil, fmt.Errorf("Unavailable because: %s", response.PlayabilityStatus.Reason)
}
c.addFormatsByInfos(&formats, response.StreamingData.Formats, false)
c.addFormatsByInfos(&formats, response.StreamingData.AdaptiveFormats, true)
if seconds := response.VideoDetails.LengthSeconds; seconds != "" {
val, err := strconv.Atoi(seconds)
if err == nil {
info.Duration = time.Duration(val) * time.Second
}
}
if date, err := time.Parse(youtubeDateFormat, response.Microformat.Renderer.PublishDate); err == nil {
info.DatePublished = date
} else {
log.Debug().Msgf("Unable to parse date published %v", err)
}
info.Title = response.VideoDetails.Title
info.Uploader = response.VideoDetails.Author
} else {
log.Debug().Msg("Unable to extract player response JSON")
}
if info.htmlPlayerFile == "" {
info.htmlPlayerFile = jsonConfig.Assets.JS
}
if len(formats) == 0 {
log.Debug().Msgf("No formats found")
}
info.Formats = formats
return info, nil
}
func (info *VideoInfo) addMetadata(jsondata []byte) error {
doc, err := jsonquery.Parse(bytes.NewReader(jsondata))
if err != nil {
return err
}
// Extract description
descr, err := jsonquery.Query(doc, "//videoSecondaryInfoRenderer/description")
if err != nil {
return err
}
if descr != nil {
var sb strings.Builder
for _, child := range jsonquery.Find(descr, "//text") {
sb.WriteString(child.InnerText())
}
info.Description = sb.String()
}
// Extract metadata rows
rows := jsonquery.Find(doc, "//metadataRowContainer/*/rows/*/metadataRowRenderer")
for _, row := range rows {
key, value := getMetaDataRow(row)
switch key {
case "Artist":
info.Artist = value
case "Song":
info.Song = value
case "Album":
info.Album = value
case "Writers":
info.Writers = value
}
}
return nil
}
func (c *Client) addFormatsByInfos(formats *FormatList, infos []formatInfo, adaptive bool) {
for _, info := range infos {
if err := formats.addByInfo(info, adaptive); err != nil {
log.Debug().Err(err)
}
}
}
func (c *Client) addFormatsByQueryStrings(formats *FormatList, rd io.Reader, adaptive bool) {
r := bufio.NewReader(rd)
for {
line, err := r.ReadString(',')
if err == io.EOF {
break
}
if err := formats.addByQueryString(line[:len(line)-1], adaptive); err != nil {
log.Debug().Err(err)
}
}
}
func (c *Client) getPlayerConfigFromEmbedded(cx context.Context, id string) (*playerConfig, error) {
html, err := c.httpGetAndCheckResponseReadBody(cx, youtubeEmbeddedBaseURL+id)
if err != nil {
return nil, fmt.Errorf("Embedded url request returned %w", err)
}
matches := regexpPlayerConfigEmbedded.FindSubmatch(html)
if len(matches) < 2 {
return nil, fmt.Errorf("Error extracting sts from embedded url response")
}
config := &playerConfig{}
dec := json.NewDecoder(bytes.NewBuffer(matches[1]))
err = dec.Decode(config)
if err != nil {
return nil, fmt.Errorf("Unable to extract json from embedded url: %w", err)
}
return config, nil
}