-
Notifications
You must be signed in to change notification settings - Fork 433
/
video.go
147 lines (119 loc) · 4.17 KB
/
video.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
package youtube
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"regexp"
"sort"
"strconv"
"strings"
"time"
)
type Video struct {
ID string
Title string
Description string
Author string
ChannelID string
ChannelHandle string
Views int
Duration time.Duration
PublishDate time.Time
Formats FormatList
Thumbnails Thumbnails
DASHManifestURL string // URI of the DASH manifest file
HLSManifestURL string // URI of the HLS manifest file
CaptionTracks []CaptionTrack
}
const dateFormat = "2006-01-02"
func (v *Video) parseVideoInfo(body []byte) error {
var prData playerResponseData
if err := json.Unmarshal(body, &prData); err != nil {
return fmt.Errorf("unable to parse player response JSON: %w", err)
}
if err := v.isVideoFromInfoDownloadable(prData); err != nil {
return err
}
return v.extractDataFromPlayerResponse(prData)
}
func (v *Video) isVideoFromInfoDownloadable(prData playerResponseData) error {
return v.isVideoDownloadable(prData, false)
}
var playerResponsePattern = regexp.MustCompile(`var ytInitialPlayerResponse\s*=\s*(\{.+?\});`)
func (v *Video) parseVideoPage(body []byte) error {
initialPlayerResponse := playerResponsePattern.FindSubmatch(body)
if initialPlayerResponse == nil || len(initialPlayerResponse) < 2 {
return errors.New("no ytInitialPlayerResponse found in the server's answer")
}
var prData playerResponseData
if err := json.Unmarshal(initialPlayerResponse[1], &prData); err != nil {
return fmt.Errorf("unable to parse player response JSON: %w", err)
}
if err := v.isVideoFromPageDownloadable(prData); err != nil {
return err
}
return v.extractDataFromPlayerResponse(prData)
}
func (v *Video) isVideoFromPageDownloadable(prData playerResponseData) error {
return v.isVideoDownloadable(prData, true)
}
func (v *Video) isVideoDownloadable(prData playerResponseData, isVideoPage bool) error {
// Check if video is downloadable
switch prData.PlayabilityStatus.Status {
case "OK":
return nil
case "LOGIN_REQUIRED":
// for some reason they use same status message for age-restricted and private videos
if strings.HasPrefix(prData.PlayabilityStatus.Reason, "This video is private") {
return ErrVideoPrivate
}
return ErrLoginRequired
}
if !isVideoPage && !prData.PlayabilityStatus.PlayableInEmbed {
return ErrNotPlayableInEmbed
}
return &ErrPlayabiltyStatus{
Status: prData.PlayabilityStatus.Status,
Reason: prData.PlayabilityStatus.Reason,
}
}
func (v *Video) extractDataFromPlayerResponse(prData playerResponseData) error {
v.Title = prData.VideoDetails.Title
v.Description = prData.VideoDetails.ShortDescription
v.Author = prData.VideoDetails.Author
v.Thumbnails = prData.VideoDetails.Thumbnail.Thumbnails
v.ChannelID = prData.VideoDetails.ChannelID
v.CaptionTracks = prData.Captions.PlayerCaptionsTracklistRenderer.CaptionTracks
if views, _ := strconv.Atoi(prData.VideoDetails.ViewCount); views > 0 {
v.Views = views
}
if seconds, _ := strconv.Atoi(prData.VideoDetails.LengthSeconds); seconds > 0 {
v.Duration = time.Duration(seconds) * time.Second
}
if seconds, _ := strconv.Atoi(prData.Microformat.PlayerMicroformatRenderer.LengthSeconds); seconds > 0 {
v.Duration = time.Duration(seconds) * time.Second
}
if str := prData.Microformat.PlayerMicroformatRenderer.PublishDate; str != "" {
v.PublishDate, _ = time.Parse(dateFormat, str)
}
if profileURL, err := url.Parse(prData.Microformat.PlayerMicroformatRenderer.OwnerProfileURL); err == nil && len(profileURL.Path) > 1 {
v.ChannelHandle = profileURL.Path[1:]
}
// Assign Streams
v.Formats = append(prData.StreamingData.Formats, prData.StreamingData.AdaptiveFormats...)
if len(v.Formats) == 0 {
return errors.New("no formats found in the server's answer")
}
// Sort formats by bitrate
sort.SliceStable(v.Formats, v.SortBitrateDesc)
v.HLSManifestURL = prData.StreamingData.HlsManifestURL
v.DASHManifestURL = prData.StreamingData.DashManifestURL
return nil
}
func (v *Video) SortBitrateDesc(i int, j int) bool {
return v.Formats[i].Bitrate > v.Formats[j].Bitrate
}
func (v *Video) SortBitrateAsc(i int, j int) bool {
return v.Formats[i].Bitrate < v.Formats[j].Bitrate
}