-
Notifications
You must be signed in to change notification settings - Fork 436
/
video_test.go
157 lines (137 loc) · 3.82 KB
/
video_test.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
package youtube
import (
"io"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func ExampleClient_GetStream() {
video, err := testClient.GetVideo("https://www.youtube.com/watch?v=9_MbW9FK1fA")
if err != nil {
panic(err)
}
// Typically youtube only provides separate streams for video and audio.
// If you want audio and video combined, take a look a the downloader package.
formats := video.Formats.Quality("medium")
reader, _, err := testClient.GetStream(video, &formats[0])
if err != nil {
panic(err)
}
// do something with the reader
reader.Close()
}
func TestSimpleTest(t *testing.T) {
video, err := testClient.GetVideo("https://www.youtube.com/watch?v=9_MbW9FK1fA")
require.NoError(t, err, "get body")
_, err = testClient.GetTranscript(video, "en")
require.NoError(t, err, "get transcript")
// Typically youtube only provides separate streams for video and audio.
// If you want audio and video combined, take a look a the downloader package.
formats := video.Formats.Quality("hd1080")
require.NotEmpty(t, formats)
start := time.Now()
reader, _, err := testClient.GetStream(video, &formats[0])
require.NoError(t, err, "get stream")
t.Log("Duration Milliseconds: ", time.Since(start).Milliseconds())
// do something with the reader
b, err := io.ReadAll(reader)
require.NoError(t, err, "read body")
t.Log("Downloaded ", len(b))
}
func TestDownload_Regular(t *testing.T) {
testcases := []struct {
name string
url string
outputFile string
itagNo int
quality string
}{
{
// Video from issue #25
name: "default",
url: "https://www.youtube.com/watch?v=54e6lBE3BoQ",
outputFile: "default_test.mp4",
quality: "",
},
{
// Video from issue #25
name: "quality:medium",
url: "https://www.youtube.com/watch?v=54e6lBE3BoQ",
outputFile: "medium_test.mp4",
quality: "medium",
},
{
name: "without-filename",
url: "https://www.youtube.com/watch?v=n3kPvBCYT3E",
},
{
name: "Format",
url: "https://www.youtube.com/watch?v=54e6lBE3BoQ",
outputFile: "muxedstream_test.mp4",
itagNo: 18,
},
{
name: "AdaptiveFormat_video",
url: "https://www.youtube.com/watch?v=54e6lBE3BoQ",
outputFile: "adaptiveStream_video_test.m4v",
itagNo: 134,
},
{
name: "AdaptiveFormat_audio",
url: "https://www.youtube.com/watch?v=54e6lBE3BoQ",
outputFile: "adaptiveStream_audio_test.m4a",
itagNo: 140,
},
{
// Video from issue #138
name: "NotPlayableInEmbed",
url: "https://www.youtube.com/watch?v=gr-IqFcNExY",
outputFile: "not_playable_in_embed.mp4",
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
require := require.New(t)
video, err := testClient.GetVideo(tc.url)
require.NoError(err)
formats := video.Formats
if tc.itagNo > 0 {
formats = formats.Itag(tc.itagNo)
require.NotEmpty(formats)
}
url, err := testClient.GetStreamURL(video, &video.Formats[0])
require.NoError(err)
require.NotEmpty(url)
})
}
}
func TestDownload_WhenPlayabilityStatusIsNotOK(t *testing.T) {
testcases := []struct {
issue string
videoID string
err string
}{
{
issue: "issue#65",
videoID: "9ja-K2FslBU",
err: `status: ERROR`,
},
{
issue: "issue#59",
videoID: "yZIXLfi8CZQ",
err: ErrVideoPrivate.Error(),
},
}
for _, tc := range testcases {
t.Run(tc.issue, func(t *testing.T) {
_, err := testClient.GetVideo(tc.videoID)
require.Error(t, err)
require.Contains(t, err.Error(), tc.err)
})
}
}
// See https://github.com/kkdai/youtube/pull/238
func TestDownload_SensitiveContent(t *testing.T) {
_, err := testClient.GetVideo("MS91knuzoOA")
require.EqualError(t, err, "can't bypass age restriction: embedding of this video has been disabled")
}