forked from rylio/ytdl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvideo_info.go
382 lines (334 loc) · 10.9 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package ytdl
import (
"bufio"
"bytes"
"context"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"net/url"
"regexp"
"strconv"
"strings"
"time"
"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)
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 = (.*?)\};`)
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 {
data := initialData{}
if err := json.Unmarshal(matches[1], &data); err != nil {
return nil, err
}
contents := data.Contents.TwoColumnWatchNextResults.Results.Results.Contents
if len(contents) >= 2 {
infoRenderer := contents[1].VideoSecondaryInfoRenderer
info.Description = infoRenderer.Description.String()
rows := infoRenderer.MetadataRowContainer.MetadataRowContainerRenderer.Rows
info.Artist = rows.Get("Artist")
info.Album = rows.Get("Album")
info.Song = rows.Get("Song")
info.Writers = rows.Get("Writers")
}
}
info.ID = id
var jsonConfig playerConfig
// match json in javascript
if matches := regexpPlayerConfig.FindSubmatch(html); len(matches) > 1 {
data := append(matches[1], []byte("}")...)
err := json.Unmarshal(data, &jsonConfig)
if err != nil {
return nil, err
}
} else {
log.Debug().Msg("Unable to extract json from default url, trying embedded url")
info, err := c.getVideoInfoFromEmbedded(cx, id)
if err != nil {
return nil, err
}
query := url.Values{
"video_id": []string{id},
"eurl": []string{youtubeVideoEURL + id},
}
if sts, ok := info["sts"].(float64); ok {
query.Add("sts", strconv.Itoa(int(sts)))
}
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")
}
info.htmlPlayerFile = jsonConfig.Assets.JS
if len(formats) == 0 {
log.Debug().Msgf("No formats found")
}
if dashManifest := inf.Dashmpd; dashManifest != "" {
tokens, err := c.getSigTokens(cx, info.htmlPlayerFile)
if err != nil {
return nil, fmt.Errorf("Unable to extract signature tokens: %w", err)
}
regex := regexp.MustCompile("\\/s\\/([a-fA-F0-9\\.]+)")
regexSub := regexp.MustCompile("([a-fA-F0-9\\.]+)")
info.DASHManifestURL = regex.ReplaceAllStringFunc(dashManifest, func(str string) string {
return "/signature/" + decipherTokens(tokens, regexSub.FindString(str))
})
dashFormats, err := c.getDashManifest(cx, info.DASHManifestURL)
if err != nil {
return nil, fmt.Errorf("Unable to extract dash manifest: %w", err)
}
for _, dashFormat := range dashFormats {
added := false
for j, format := range formats {
if dashFormat.Itag == format.Itag {
formats[j] = dashFormat
added = true
break
}
}
if !added {
formats = append(formats, dashFormat)
}
}
}
info.Formats = formats
return info, 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) getVideoInfoFromEmbedded(cx context.Context, id string) (map[string]interface{}, error) {
var jsonConfig map[string]interface{}
html, err := c.httpGetAndCheckResponseReadBody(cx, youtubeEmbeddedBaseURL+id)
if err != nil {
return nil, fmt.Errorf("Embedded url request returned %w", err)
}
// re = regexp.MustCompile("\"sts\"\\s*:\\s*(\\d+)")
re := regexp.MustCompile("yt.setConfig\\({'PLAYER_CONFIG': (.*?)}\\);")
matches := re.FindSubmatch(html)
if len(matches) < 2 {
return nil, fmt.Errorf("Error extracting sts from embedded url response")
}
dec := json.NewDecoder(bytes.NewBuffer(matches[1]))
err = dec.Decode(&jsonConfig)
if err != nil {
return nil, fmt.Errorf("Unable to extract json from embedded url: %w", err)
}
return jsonConfig, nil
}
func (c *Client) getDashManifest(cx context.Context, urlString string) (formats []*Format, err error) {
resp, err := c.httpGetAndCheckResponse(cx, urlString)
if err != nil {
return nil, err
}
defer resp.Body.Close()
dec := xml.NewDecoder(resp.Body)
var token xml.Token
for ; err == nil; token, err = dec.Token() {
if el, ok := token.(xml.StartElement); ok && el.Name.Local == "Representation" {
var rep representation
err = dec.DecodeElement(&rep, &el)
if err != nil {
break
}
if itag := getItag(rep.Itag); itag != nil {
format := &Format{
Itag: *itag,
url: rep.URL,
FromDASH: true,
}
if rep.Height != 0 {
format.Itag.Resolution = strconv.Itoa(rep.Height) + "p"
}
formats = append(formats, format)
} else {
log.Debug().Msgf("No metadata found for itag: %v, skipping...", rep.Itag)
}
}
}
if err != io.EOF {
return nil, err
}
return formats, nil
}