]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/videos/video-imports.ts
shared/ typescript types dir server-commands
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / video-imports.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
590fb506 2
590fb506 3import 'mocha'
62549e6c 4import { expect } from 'chai'
1740952b 5import { pathExists, readdir, remove } from 'fs-extra'
62549e6c 6import { join } from 'path'
590fb506 7import {
6910f20f 8 areHttpImportTestsDisabled,
7c3b7976 9 cleanupTests,
254d3579 10 createMultipleServers,
62549e6c 11 createSingleServer,
59bbcced
C
12 doubleFollow,
13 FIXTURE_URLS,
254d3579 14 PeerTubeServer,
b488ba1e 15 setAccessTokensToServers,
62549e6c 16 setDefaultVideoChannel,
6910f20f
C
17 testCaptionFile,
18 testImage,
19 waitJobs
bf54587a 20} from '@shared/server-commands'
d23dd9fb 21import { VideoPrivacy, VideoResolution } from '@shared/models'
590fb506 22
62549e6c
C
23async function checkVideosServer1 (server: PeerTubeServer, idHttp: string, idMagnet: string, idTorrent: string) {
24 const videoHttp = await server.videos.get({ id: idHttp })
25
26 expect(videoHttp.name).to.equal('small video - youtube')
27 // FIXME: youtube-dl seems broken
28 // expect(videoHttp.category.label).to.equal('News & Politics')
29 // expect(videoHttp.licence.label).to.equal('Attribution')
30 expect(videoHttp.language.label).to.equal('Unknown')
31 expect(videoHttp.nsfw).to.be.false
32 expect(videoHttp.description).to.equal('this is a super description')
33 expect(videoHttp.tags).to.deep.equal([ 'tag1', 'tag2' ])
34 expect(videoHttp.files).to.have.lengthOf(1)
35
36 const originallyPublishedAt = new Date(videoHttp.originallyPublishedAt)
37 expect(originallyPublishedAt.getDate()).to.equal(14)
38 expect(originallyPublishedAt.getMonth()).to.equal(0)
39 expect(originallyPublishedAt.getFullYear()).to.equal(2019)
40
41 const videoMagnet = await server.videos.get({ id: idMagnet })
42 const videoTorrent = await server.videos.get({ id: idTorrent })
43
44 for (const video of [ videoMagnet, videoTorrent ]) {
45 expect(video.category.label).to.equal('Misc')
46 expect(video.licence.label).to.equal('Unknown')
47 expect(video.language.label).to.equal('Unknown')
48 expect(video.nsfw).to.be.false
49 expect(video.description).to.equal('this is a super torrent description')
50 expect(video.tags).to.deep.equal([ 'tag_torrent1', 'tag_torrent2' ])
51 expect(video.files).to.have.lengthOf(1)
52 }
53
54 expect(videoTorrent.name).to.contain('你好 世界 720p.mp4')
55 expect(videoMagnet.name).to.contain('super peertube2 video')
56
57 const bodyCaptions = await server.captions.list({ videoId: idHttp })
58 expect(bodyCaptions.total).to.equal(2)
59}
60
61async function checkVideoServer2 (server: PeerTubeServer, id: number | string) {
62 const video = await server.videos.get({ id })
63
64 expect(video.name).to.equal('my super name')
65 expect(video.category.label).to.equal('Entertainment')
66 expect(video.licence.label).to.equal('Public Domain Dedication')
67 expect(video.language.label).to.equal('English')
68 expect(video.nsfw).to.be.false
69 expect(video.description).to.equal('my super description')
70 expect(video.tags).to.deep.equal([ 'supertag1', 'supertag2' ])
71
72 expect(video.files).to.have.lengthOf(1)
73
74 const bodyCaptions = await server.captions.list({ videoId: id })
75 expect(bodyCaptions.total).to.equal(2)
76}
590fb506
C
77
78describe('Test video imports', function () {
590fb506 79
b488ba1e
C
80 if (areHttpImportTestsDisabled()) return
81
62549e6c 82 function runSuite (mode: 'youtube-dl' | 'yt-dlp') {
590fb506 83
62549e6c
C
84 describe('Import ' + mode, function () {
85 let servers: PeerTubeServer[] = []
652c6416 86
62549e6c
C
87 before(async function () {
88 this.timeout(30_000)
590fb506 89
62549e6c
C
90 // Run servers
91 servers = await createMultipleServers(2, {
92 import: {
93 videos: {
94 http: {
95 youtube_dl_release: {
96 url: mode === 'youtube-dl'
97 ? 'https://yt-dl.org/downloads/latest/youtube-dl'
98 : 'https://api.github.com/repos/yt-dlp/yt-dlp/releases',
590fb506 99
62549e6c
C
100 name: mode
101 }
102 }
103 }
104 }
105 })
590fb506 106
62549e6c
C
107 await setAccessTokensToServers(servers)
108 await setDefaultVideoChannel(servers)
652c6416 109
62549e6c
C
110 await doubleFollow(servers[0], servers[1])
111 })
590fb506 112
62549e6c
C
113 it('Should import videos on server 1', async function () {
114 this.timeout(60_000)
590fb506 115
62549e6c
C
116 const baseAttributes = {
117 channelId: servers[0].store.channel.id,
118 privacy: VideoPrivacy.PUBLIC
119 }
590fb506 120
62549e6c
C
121 {
122 const attributes = { ...baseAttributes, targetUrl: FIXTURE_URLS.youtube }
123 const { video } = await servers[0].imports.importVideo({ attributes })
124 expect(video.name).to.equal('small video - youtube')
590fb506 125
62549e6c
C
126 {
127 expect(video.thumbnailPath).to.match(new RegExp(`^/static/thumbnails/.+.jpg$`))
128 expect(video.previewPath).to.match(new RegExp(`^/lazy-static/previews/.+.jpg$`))
590fb506 129
62549e6c
C
130 const suffix = mode === 'yt-dlp'
131 ? '_yt_dlp'
132 : ''
590fb506 133
62549e6c
C
134 await testImage(servers[0].url, 'video_import_thumbnail' + suffix, video.thumbnailPath)
135 await testImage(servers[0].url, 'video_import_preview' + suffix, video.previewPath)
136 }
590fb506 137
62549e6c
C
138 const bodyCaptions = await servers[0].captions.list({ videoId: video.id })
139 const videoCaptions = bodyCaptions.data
140 expect(videoCaptions).to.have.lengthOf(2)
590fb506 141
62549e6c
C
142 {
143 const enCaption = videoCaptions.find(caption => caption.language.id === 'en')
144 expect(enCaption).to.exist
145 expect(enCaption.language.label).to.equal('English')
146 expect(enCaption.captionPath).to.match(new RegExp(`^/lazy-static/video-captions/.+-en.vtt$`))
3e17515e 147
62549e6c
C
148 const regex = `WEBVTT[ \n]+Kind: captions[ \n]+Language: en[ \n]+00:00:01.600 --> 00:00:04.200[ \n]+English \\(US\\)[ \n]+` +
149 `00:00:05.900 --> 00:00:07.999[ \n]+This is a subtitle in American English[ \n]+` +
150 `00:00:10.000 --> 00:00:14.000[ \n]+Adding subtitles is very easy to do`
151 await testCaptionFile(servers[0].url, enCaption.captionPath, new RegExp(regex))
152 }
53c06121 153
62549e6c
C
154 {
155 const frCaption = videoCaptions.find(caption => caption.language.id === 'fr')
156 expect(frCaption).to.exist
157 expect(frCaption.language.label).to.equal('French')
158 expect(frCaption.captionPath).to.match(new RegExp(`^/lazy-static/video-captions/.+-fr.vtt`))
53c06121 159
62549e6c
C
160 const regex = `WEBVTT[ \n]+Kind: captions[ \n]+Language: fr[ \n]+00:00:01.600 --> 00:00:04.200[ \n]+` +
161 `Français \\(FR\\)[ \n]+00:00:05.900 --> 00:00:07.999[ \n]+C'est un sous-titre français[ \n]+` +
162 `00:00:10.000 --> 00:00:14.000[ \n]+Ajouter un sous-titre est vraiment facile`
ba6e9e8f 163
62549e6c
C
164 await testCaptionFile(servers[0].url, frCaption.captionPath, new RegExp(regex))
165 }
166 }
ba6e9e8f 167
62549e6c
C
168 {
169 const attributes = {
170 ...baseAttributes,
171 magnetUri: FIXTURE_URLS.magnet,
172 description: 'this is a super torrent description',
173 tags: [ 'tag_torrent1', 'tag_torrent2' ]
174 }
175 const { video } = await servers[0].imports.importVideo({ attributes })
176 expect(video.name).to.equal('super peertube2 video')
177 }
ba6e9e8f 178
62549e6c
C
179 {
180 const attributes = {
181 ...baseAttributes,
182 torrentfile: 'video-720p.torrent' as any,
183 description: 'this is a super torrent description',
184 tags: [ 'tag_torrent1', 'tag_torrent2' ]
185 }
186 const { video } = await servers[0].imports.importVideo({ attributes })
187 expect(video.name).to.equal('你好 世界 720p.mp4')
188 }
189 })
ba6e9e8f 190
62549e6c
C
191 it('Should list the videos to import in my videos on server 1', async function () {
192 const { total, data } = await servers[0].videos.listMyVideos({ sort: 'createdAt' })
ba6e9e8f 193
62549e6c 194 expect(total).to.equal(3)
ba6e9e8f 195
62549e6c
C
196 expect(data).to.have.lengthOf(3)
197 expect(data[0].name).to.equal('small video - youtube')
198 expect(data[1].name).to.equal('super peertube2 video')
199 expect(data[2].name).to.equal('你好 世界 720p.mp4')
200 })
ba6e9e8f 201
62549e6c
C
202 it('Should list the videos to import in my imports on server 1', async function () {
203 const { total, data: videoImports } = await servers[0].imports.getMyVideoImports({ sort: '-createdAt' })
204 expect(total).to.equal(3)
ba6e9e8f 205
62549e6c 206 expect(videoImports).to.have.lengthOf(3)
ba6e9e8f 207
62549e6c
C
208 expect(videoImports[2].targetUrl).to.equal(FIXTURE_URLS.youtube)
209 expect(videoImports[2].magnetUri).to.be.null
210 expect(videoImports[2].torrentName).to.be.null
211 expect(videoImports[2].video.name).to.equal('small video - youtube')
3e17515e 212
62549e6c
C
213 expect(videoImports[1].targetUrl).to.be.null
214 expect(videoImports[1].magnetUri).to.equal(FIXTURE_URLS.magnet)
215 expect(videoImports[1].torrentName).to.be.null
216 expect(videoImports[1].video.name).to.equal('super peertube2 video')
3e17515e 217
62549e6c
C
218 expect(videoImports[0].targetUrl).to.be.null
219 expect(videoImports[0].magnetUri).to.be.null
220 expect(videoImports[0].torrentName).to.equal('video-720p.torrent')
221 expect(videoImports[0].video.name).to.equal('你好 世界 720p.mp4')
222 })
590fb506 223
62549e6c
C
224 it('Should have the video listed on the two instances', async function () {
225 this.timeout(120_000)
590fb506 226
62549e6c 227 await waitJobs(servers)
590fb506 228
62549e6c
C
229 for (const server of servers) {
230 const { total, data } = await server.videos.list()
231 expect(total).to.equal(3)
232 expect(data).to.have.lengthOf(3)
590fb506 233
62549e6c
C
234 const [ videoHttp, videoMagnet, videoTorrent ] = data
235 await checkVideosServer1(server, videoHttp.uuid, videoMagnet.uuid, videoTorrent.uuid)
236 }
237 })
238
239 it('Should import a video on server 2 with some fields', async function () {
240 this.timeout(60_000)
241
242 const attributes = {
243 targetUrl: FIXTURE_URLS.youtube,
244 channelId: servers[1].store.channel.id,
245 privacy: VideoPrivacy.PUBLIC,
246 category: 10,
247 licence: 7,
248 language: 'en',
249 name: 'my super name',
250 description: 'my super description',
251 tags: [ 'supertag1', 'supertag2' ]
252 }
253 const { video } = await servers[1].imports.importVideo({ attributes })
254 expect(video.name).to.equal('my super name')
255 })
3e17515e 256
62549e6c
C
257 it('Should have the videos listed on the two instances', async function () {
258 this.timeout(120_000)
3e17515e 259
62549e6c 260 await waitJobs(servers)
590fb506 261
62549e6c
C
262 for (const server of servers) {
263 const { total, data } = await server.videos.list()
264 expect(total).to.equal(4)
265 expect(data).to.have.lengthOf(4)
590fb506 266
62549e6c 267 await checkVideoServer2(server, data[0].uuid)
590fb506 268
62549e6c
C
269 const [ , videoHttp, videoMagnet, videoTorrent ] = data
270 await checkVideosServer1(server, videoHttp.uuid, videoMagnet.uuid, videoTorrent.uuid)
271 }
272 })
590fb506 273
62549e6c
C
274 it('Should import a video that will be transcoded', async function () {
275 this.timeout(240_000)
590fb506 276
62549e6c
C
277 const attributes = {
278 name: 'transcoded video',
279 magnetUri: FIXTURE_URLS.magnet,
280 channelId: servers[1].store.channel.id,
281 privacy: VideoPrivacy.PUBLIC
282 }
283 const { video } = await servers[1].imports.importVideo({ attributes })
284 const videoUUID = video.uuid
590fb506 285
62549e6c 286 await waitJobs(servers)
590fb506 287
62549e6c
C
288 for (const server of servers) {
289 const video = await server.videos.get({ id: videoUUID })
590fb506 290
62549e6c
C
291 expect(video.name).to.equal('transcoded video')
292 expect(video.files).to.have.lengthOf(4)
293 }
294 })
295
296 it('Should import no HDR version on a HDR video', async function () {
297 this.timeout(300_000)
298
299 const config = {
300 transcoding: {
301 enabled: true,
302 resolutions: {
8dd754c7 303 '144p': true,
62549e6c
C
304 '240p': true,
305 '360p': false,
306 '480p': false,
307 '720p': false,
308 '1080p': false, // the resulting resolution shouldn't be higher than this, and not vp9.2/av01
309 '1440p': false,
310 '2160p': false
311 },
312 webtorrent: { enabled: true },
313 hls: { enabled: false }
314 },
315 import: {
316 videos: {
317 http: {
318 enabled: true
319 },
320 torrent: {
321 enabled: true
322 }
323 }
324 }
325 }
326 await servers[0].config.updateCustomSubConfig({ newConfig: config })
590fb506 327
62549e6c
C
328 const attributes = {
329 name: 'hdr video',
330 targetUrl: FIXTURE_URLS.youtubeHDR,
331 channelId: servers[0].store.channel.id,
332 privacy: VideoPrivacy.PUBLIC
333 }
334 const { video: videoImported } = await servers[0].imports.importVideo({ attributes })
335 const videoUUID = videoImported.uuid
336
337 await waitJobs(servers)
338
339 // test resolution
340 const video = await servers[0].videos.get({ id: videoUUID })
341 expect(video.name).to.equal('hdr video')
342 const maxResolution = Math.max.apply(Math, video.files.map(function (o) { return o.resolution.id }))
343 expect(maxResolution, 'expected max resolution not met').to.equals(VideoResolution.H_240P)
344 })
345
346 it('Should import a peertube video', async function () {
347 this.timeout(120_000)
348
5480933b
C
349 const toTest = [ FIXTURE_URLS.peertube_long ]
350
62549e6c 351 // TODO: include peertube_short when https://github.com/ytdl-org/youtube-dl/pull/29475 is merged
5480933b
C
352 if (mode === 'yt-dlp') {
353 toTest.push(FIXTURE_URLS.peertube_short)
354 }
355
356 for (const targetUrl of toTest) {
62549e6c
C
357 await servers[0].config.disableTranscoding()
358
359 const attributes = {
360 targetUrl,
361 channelId: servers[0].store.channel.id,
362 privacy: VideoPrivacy.PUBLIC
363 }
364 const { video } = await servers[0].imports.importVideo({ attributes })
365 const videoUUID = video.uuid
590fb506 366
62549e6c 367 await waitJobs(servers)
590fb506 368
62549e6c
C
369 for (const server of servers) {
370 const video = await server.videos.get({ id: videoUUID })
3e17515e 371
62549e6c
C
372 expect(video.name).to.equal('E2E tests')
373 }
374 }
375 })
3e17515e 376
62549e6c
C
377 after(async function () {
378 await cleanupTests(servers)
379 })
380 })
381 }
3e17515e 382
62549e6c 383 runSuite('youtube-dl')
3e17515e 384
62549e6c 385 runSuite('yt-dlp')
3e17515e 386
62549e6c
C
387 describe('Auto update', function () {
388 let server: PeerTubeServer
3e17515e 389
62549e6c
C
390 function quickPeerTubeImport () {
391 const attributes = {
392 targetUrl: FIXTURE_URLS.peertube_long,
393 channelId: server.store.channel.id,
394 privacy: VideoPrivacy.PUBLIC
395 }
396
397 return server.imports.importVideo({ attributes })
590fb506 398 }
590fb506 399
62549e6c
C
400 async function testBinaryUpdate (releaseUrl: string, releaseName: string) {
401 await remove(join(server.servers.buildDirectory('bin'), releaseName))
402
403 await server.kill()
404 await server.run({
405 import: {
406 videos: {
407 http: {
408 youtube_dl_release: {
409 url: releaseUrl,
410 name: releaseName
411 }
412 }
454c20fa
RK
413 }
414 }
62549e6c
C
415 })
416
417 await quickPeerTubeImport()
454c20fa 418
1740952b
C
419 const base = server.servers.buildDirectory('bin')
420 const content = await readdir(base)
421 const binaryPath = join(base, releaseName)
422
423 expect(await pathExists(binaryPath), `${binaryPath} does not exist in ${base} (${content.join(', ')})`).to.be.true
454c20fa 424 }
454c20fa 425
62549e6c
C
426 before(async function () {
427 this.timeout(30_000)
454c20fa 428
62549e6c
C
429 // Run servers
430 server = await createSingleServer(1)
454c20fa 431
62549e6c
C
432 await setAccessTokensToServers([ server ])
433 await setDefaultVideoChannel([ server ])
434 })
e3c9ea72 435
62549e6c
C
436 it('Should update youtube-dl from github URL', async function () {
437 this.timeout(120_000)
e3c9ea72 438
62549e6c
C
439 await testBinaryUpdate('https://api.github.com/repos/ytdl-org/youtube-dl/releases', 'youtube-dl')
440 })
e3c9ea72 441
62549e6c
C
442 it('Should update youtube-dl from raw URL', async function () {
443 this.timeout(120_000)
e3c9ea72 444
62549e6c
C
445 await testBinaryUpdate('https://yt-dl.org/downloads/latest/youtube-dl', 'youtube-dl')
446 })
e3c9ea72 447
62549e6c
C
448 it('Should update youtube-dl from youtube-dl fork', async function () {
449 this.timeout(120_000)
e3c9ea72 450
62549e6c
C
451 await testBinaryUpdate('https://api.github.com/repos/yt-dlp/yt-dlp/releases', 'yt-dlp')
452 })
60409162
C
453
454 after(async function () {
455 await cleanupTests([ server ])
456 })
590fb506
C
457 })
458})