]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/video-transcoder.ts
Use an object to represent a server
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / video-transcoder.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import { omit } from 'lodash'
6 import { join } from 'path'
7 import { HttpStatusCode } from '@shared/core-utils'
8 import {
9 buildAbsoluteFixturePath,
10 cleanupTests,
11 doubleFollow,
12 createMultipleServers,
13 generateHighBitrateVideo,
14 generateVideoWithFramerate,
15 makeGetRequest,
16 PeerTubeServer,
17 setAccessTokensToServers,
18 waitJobs,
19 webtorrentAdd
20 } from '@shared/extra-utils'
21 import { getMaxBitrate, VideoResolution, VideoState } from '@shared/models'
22 import { VIDEO_TRANSCODING_FPS } from '../../../../server/initializers/constants'
23 import {
24 canDoQuickTranscode,
25 getAudioStream,
26 getMetadataFromFile,
27 getVideoFileBitrate,
28 getVideoFileFPS,
29 getVideoFileResolution
30 } from '../../../helpers/ffprobe-utils'
31
32 const expect = chai.expect
33
34 function updateConfigForTranscoding (server: PeerTubeServer) {
35 return server.config.updateCustomSubConfig({
36 newConfig: {
37 transcoding: {
38 enabled: true,
39 allowAdditionalExtensions: true,
40 allowAudioFiles: true,
41 hls: { enabled: true },
42 webtorrent: { enabled: true },
43 resolutions: {
44 '0p': false,
45 '240p': true,
46 '360p': true,
47 '480p': true,
48 '720p': true,
49 '1080p': true,
50 '1440p': true,
51 '2160p': true
52 }
53 }
54 }
55 })
56 }
57
58 describe('Test video transcoding', function () {
59 let servers: PeerTubeServer[] = []
60 let video4k: string
61
62 before(async function () {
63 this.timeout(30_000)
64
65 // Run servers
66 servers = await createMultipleServers(2)
67
68 await setAccessTokensToServers(servers)
69
70 await doubleFollow(servers[0], servers[1])
71
72 await updateConfigForTranscoding(servers[1])
73 })
74
75 describe('Basic transcoding (or not)', function () {
76
77 it('Should not transcode video on server 1', async function () {
78 this.timeout(60_000)
79
80 const attributes = {
81 name: 'my super name for server 1',
82 description: 'my super description for server 1',
83 fixture: 'video_short.webm'
84 }
85 await servers[0].videos.upload({ attributes })
86
87 await waitJobs(servers)
88
89 for (const server of servers) {
90 const { data } = await server.videos.list()
91 const video = data[0]
92
93 const videoDetails = await server.videos.get({ id: video.id })
94 expect(videoDetails.files).to.have.lengthOf(1)
95
96 const magnetUri = videoDetails.files[0].magnetUri
97 expect(magnetUri).to.match(/\.webm/)
98
99 const torrent = await webtorrentAdd(magnetUri, true)
100 expect(torrent.files).to.be.an('array')
101 expect(torrent.files.length).to.equal(1)
102 expect(torrent.files[0].path).match(/\.webm$/)
103 }
104 })
105
106 it('Should transcode video on server 2', async function () {
107 this.timeout(120_000)
108
109 const attributes = {
110 name: 'my super name for server 2',
111 description: 'my super description for server 2',
112 fixture: 'video_short.webm'
113 }
114 await servers[1].videos.upload({ attributes })
115
116 await waitJobs(servers)
117
118 for (const server of servers) {
119 const { data } = await server.videos.list()
120
121 const video = data.find(v => v.name === attributes.name)
122 const videoDetails = await server.videos.get({ id: video.id })
123
124 expect(videoDetails.files).to.have.lengthOf(4)
125
126 const magnetUri = videoDetails.files[0].magnetUri
127 expect(magnetUri).to.match(/\.mp4/)
128
129 const torrent = await webtorrentAdd(magnetUri, true)
130 expect(torrent.files).to.be.an('array')
131 expect(torrent.files.length).to.equal(1)
132 expect(torrent.files[0].path).match(/\.mp4$/)
133 }
134 })
135
136 it('Should wait for transcoding before publishing the video', async function () {
137 this.timeout(160_000)
138
139 {
140 // Upload the video, but wait transcoding
141 const attributes = {
142 name: 'waiting video',
143 fixture: 'video_short1.webm',
144 waitTranscoding: true
145 }
146 const { uuid } = await servers[1].videos.upload({ attributes })
147 const videoId = uuid
148
149 // Should be in transcode state
150 const body = await servers[1].videos.get({ id: videoId })
151 expect(body.name).to.equal('waiting video')
152 expect(body.state.id).to.equal(VideoState.TO_TRANSCODE)
153 expect(body.state.label).to.equal('To transcode')
154 expect(body.waitTranscoding).to.be.true
155
156 {
157 // Should have my video
158 const { data } = await servers[1].videos.listMyVideos()
159 const videoToFindInMine = data.find(v => v.name === attributes.name)
160 expect(videoToFindInMine).not.to.be.undefined
161 expect(videoToFindInMine.state.id).to.equal(VideoState.TO_TRANSCODE)
162 expect(videoToFindInMine.state.label).to.equal('To transcode')
163 expect(videoToFindInMine.waitTranscoding).to.be.true
164 }
165
166 {
167 // Should not list this video
168 const { data } = await servers[1].videos.list()
169 const videoToFindInList = data.find(v => v.name === attributes.name)
170 expect(videoToFindInList).to.be.undefined
171 }
172
173 // Server 1 should not have the video yet
174 await servers[0].videos.get({ id: videoId, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
175 }
176
177 await waitJobs(servers)
178
179 for (const server of servers) {
180 const { data } = await server.videos.list()
181 const videoToFind = data.find(v => v.name === 'waiting video')
182 expect(videoToFind).not.to.be.undefined
183
184 const videoDetails = await server.videos.get({ id: videoToFind.id })
185
186 expect(videoDetails.state.id).to.equal(VideoState.PUBLISHED)
187 expect(videoDetails.state.label).to.equal('Published')
188 expect(videoDetails.waitTranscoding).to.be.true
189 }
190 })
191
192 it('Should accept and transcode additional extensions', async function () {
193 this.timeout(300_000)
194
195 let tempFixturePath: string
196
197 {
198 tempFixturePath = await generateHighBitrateVideo()
199
200 const bitrate = await getVideoFileBitrate(tempFixturePath)
201 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
202 }
203
204 for (const fixture of [ 'video_short.mkv', 'video_short.avi' ]) {
205 const attributes = {
206 name: fixture,
207 fixture
208 }
209
210 await servers[1].videos.upload({ attributes })
211
212 await waitJobs(servers)
213
214 for (const server of servers) {
215 const { data } = await server.videos.list()
216
217 const video = data.find(v => v.name === attributes.name)
218 const videoDetails = await server.videos.get({ id: video.id })
219 expect(videoDetails.files).to.have.lengthOf(4)
220
221 const magnetUri = videoDetails.files[0].magnetUri
222 expect(magnetUri).to.contain('.mp4')
223 }
224 }
225 })
226
227 it('Should transcode a 4k video', async function () {
228 this.timeout(200_000)
229
230 const attributes = {
231 name: '4k video',
232 fixture: 'video_short_4k.mp4'
233 }
234
235 const { uuid } = await servers[1].videos.upload({ attributes })
236 video4k = uuid
237
238 await waitJobs(servers)
239
240 const resolutions = [ 240, 360, 480, 720, 1080, 1440, 2160 ]
241
242 for (const server of servers) {
243 const videoDetails = await server.videos.get({ id: video4k })
244 expect(videoDetails.files).to.have.lengthOf(resolutions.length)
245
246 for (const r of resolutions) {
247 expect(videoDetails.files.find(f => f.resolution.id === r)).to.not.be.undefined
248 expect(videoDetails.streamingPlaylists[0].files.find(f => f.resolution.id === r)).to.not.be.undefined
249 }
250 }
251 })
252 })
253
254 describe('Audio transcoding', function () {
255
256 it('Should transcode high bit rate mp3 to proper bit rate', async function () {
257 this.timeout(60_000)
258
259 const attributes = {
260 name: 'mp3_256k',
261 fixture: 'video_short_mp3_256k.mp4'
262 }
263 await servers[1].videos.upload({ attributes })
264
265 await waitJobs(servers)
266
267 for (const server of servers) {
268 const { data } = await server.videos.list()
269
270 const video = data.find(v => v.name === attributes.name)
271 const videoDetails = await server.videos.get({ id: video.id })
272
273 expect(videoDetails.files).to.have.lengthOf(4)
274
275 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-240.mp4'))
276 const probe = await getAudioStream(path)
277
278 if (probe.audioStream) {
279 expect(probe.audioStream['codec_name']).to.be.equal('aac')
280 expect(probe.audioStream['bit_rate']).to.be.at.most(384 * 8000)
281 } else {
282 this.fail('Could not retrieve the audio stream on ' + probe.absolutePath)
283 }
284 }
285 })
286
287 it('Should transcode video with no audio and have no audio itself', async function () {
288 this.timeout(60_000)
289
290 const attributes = {
291 name: 'no_audio',
292 fixture: 'video_short_no_audio.mp4'
293 }
294 await servers[1].videos.upload({ attributes })
295
296 await waitJobs(servers)
297
298 for (const server of servers) {
299 const { data } = await server.videos.list()
300
301 const video = data.find(v => v.name === attributes.name)
302 const videoDetails = await server.videos.get({ id: video.id })
303
304 expect(videoDetails.files).to.have.lengthOf(4)
305 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-240.mp4'))
306 const probe = await getAudioStream(path)
307 expect(probe).to.not.have.property('audioStream')
308 }
309 })
310
311 it('Should leave the audio untouched, but properly transcode the video', async function () {
312 this.timeout(60_000)
313
314 const attributes = {
315 name: 'untouched_audio',
316 fixture: 'video_short.mp4'
317 }
318 await servers[1].videos.upload({ attributes })
319
320 await waitJobs(servers)
321
322 for (const server of servers) {
323 const { data } = await server.videos.list()
324
325 const video = data.find(v => v.name === attributes.name)
326 const videoDetails = await server.videos.get({ id: video.id })
327
328 expect(videoDetails.files).to.have.lengthOf(4)
329
330 const fixturePath = buildAbsoluteFixturePath(attributes.fixture)
331 const fixtureVideoProbe = await getAudioStream(fixturePath)
332 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-240.mp4'))
333
334 const videoProbe = await getAudioStream(path)
335
336 if (videoProbe.audioStream && fixtureVideoProbe.audioStream) {
337 const toOmit = [ 'max_bit_rate', 'duration', 'duration_ts', 'nb_frames', 'start_time', 'start_pts' ]
338 expect(omit(videoProbe.audioStream, toOmit)).to.be.deep.equal(omit(fixtureVideoProbe.audioStream, toOmit))
339 } else {
340 this.fail('Could not retrieve the audio stream on ' + videoProbe.absolutePath)
341 }
342 }
343 })
344 })
345
346 describe('Audio upload', function () {
347
348 function runSuite (mode: 'legacy' | 'resumable') {
349
350 before(async function () {
351 await servers[1].config.updateCustomSubConfig({
352 newConfig: {
353 transcoding: {
354 hls: { enabled: true },
355 webtorrent: { enabled: true },
356 resolutions: {
357 '0p': false,
358 '240p': false,
359 '360p': false,
360 '480p': false,
361 '720p': false,
362 '1080p': false,
363 '1440p': false,
364 '2160p': false
365 }
366 }
367 }
368 })
369 })
370
371 it('Should merge an audio file with the preview file', async function () {
372 this.timeout(60_000)
373
374 const attributes = { name: 'audio_with_preview', previewfile: 'preview.jpg', fixture: 'sample.ogg' }
375 await servers[1].videos.upload({ attributes, mode })
376
377 await waitJobs(servers)
378
379 for (const server of servers) {
380 const { data } = await server.videos.list()
381
382 const video = data.find(v => v.name === 'audio_with_preview')
383 const videoDetails = await server.videos.get({ id: video.id })
384
385 expect(videoDetails.files).to.have.lengthOf(1)
386
387 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, statusCodeExpected: HttpStatusCode.OK_200 })
388 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, statusCodeExpected: HttpStatusCode.OK_200 })
389
390 const magnetUri = videoDetails.files[0].magnetUri
391 expect(magnetUri).to.contain('.mp4')
392 }
393 })
394
395 it('Should upload an audio file and choose a default background image', async function () {
396 this.timeout(60_000)
397
398 const attributes = { name: 'audio_without_preview', fixture: 'sample.ogg' }
399 await servers[1].videos.upload({ attributes, mode })
400
401 await waitJobs(servers)
402
403 for (const server of servers) {
404 const { data } = await server.videos.list()
405
406 const video = data.find(v => v.name === 'audio_without_preview')
407 const videoDetails = await server.videos.get({ id: video.id })
408
409 expect(videoDetails.files).to.have.lengthOf(1)
410
411 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, statusCodeExpected: HttpStatusCode.OK_200 })
412 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, statusCodeExpected: HttpStatusCode.OK_200 })
413
414 const magnetUri = videoDetails.files[0].magnetUri
415 expect(magnetUri).to.contain('.mp4')
416 }
417 })
418
419 it('Should upload an audio file and create an audio version only', async function () {
420 this.timeout(60_000)
421
422 await servers[1].config.updateCustomSubConfig({
423 newConfig: {
424 transcoding: {
425 hls: { enabled: true },
426 webtorrent: { enabled: true },
427 resolutions: {
428 '0p': true,
429 '240p': false,
430 '360p': false
431 }
432 }
433 }
434 })
435
436 const attributes = { name: 'audio_with_preview', previewfile: 'preview.jpg', fixture: 'sample.ogg' }
437 const { id } = await servers[1].videos.upload({ attributes, mode })
438
439 await waitJobs(servers)
440
441 for (const server of servers) {
442 const videoDetails = await server.videos.get({ id })
443
444 for (const files of [ videoDetails.files, videoDetails.streamingPlaylists[0].files ]) {
445 expect(files).to.have.lengthOf(2)
446 expect(files.find(f => f.resolution.id === 0)).to.not.be.undefined
447 }
448 }
449
450 await updateConfigForTranscoding(servers[1])
451 })
452 }
453
454 describe('Legacy upload', function () {
455 runSuite('legacy')
456 })
457
458 describe('Resumable upload', function () {
459 runSuite('resumable')
460 })
461 })
462
463 describe('Framerate', function () {
464
465 it('Should transcode a 60 FPS video', async function () {
466 this.timeout(60_000)
467
468 const attributes = {
469 name: 'my super 30fps name for server 2',
470 description: 'my super 30fps description for server 2',
471 fixture: '60fps_720p_small.mp4'
472 }
473 await servers[1].videos.upload({ attributes })
474
475 await waitJobs(servers)
476
477 for (const server of servers) {
478 const { data } = await server.videos.list()
479
480 const video = data.find(v => v.name === attributes.name)
481 const videoDetails = await server.videos.get({ id: video.id })
482
483 expect(videoDetails.files).to.have.lengthOf(4)
484 expect(videoDetails.files[0].fps).to.be.above(58).and.below(62)
485 expect(videoDetails.files[1].fps).to.be.below(31)
486 expect(videoDetails.files[2].fps).to.be.below(31)
487 expect(videoDetails.files[3].fps).to.be.below(31)
488
489 for (const resolution of [ '240', '360', '480' ]) {
490 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-' + resolution + '.mp4'))
491 const fps = await getVideoFileFPS(path)
492
493 expect(fps).to.be.below(31)
494 }
495
496 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-720.mp4'))
497 const fps = await getVideoFileFPS(path)
498
499 expect(fps).to.be.above(58).and.below(62)
500 }
501 })
502
503 it('Should downscale to the closest divisor standard framerate', async function () {
504 this.timeout(200_000)
505
506 let tempFixturePath: string
507
508 {
509 tempFixturePath = await generateVideoWithFramerate(59)
510
511 const fps = await getVideoFileFPS(tempFixturePath)
512 expect(fps).to.be.equal(59)
513 }
514
515 const attributes = {
516 name: '59fps video',
517 description: '59fps video',
518 fixture: tempFixturePath
519 }
520
521 await servers[1].videos.upload({ attributes })
522
523 await waitJobs(servers)
524
525 for (const server of servers) {
526 const { data } = await server.videos.list()
527
528 const video = data.find(v => v.name === attributes.name)
529
530 {
531 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-240.mp4'))
532 const fps = await getVideoFileFPS(path)
533 expect(fps).to.be.equal(25)
534 }
535
536 {
537 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-720.mp4'))
538 const fps = await getVideoFileFPS(path)
539 expect(fps).to.be.equal(59)
540 }
541 }
542 })
543 })
544
545 describe('Bitrate control', function () {
546 it('Should respect maximum bitrate values', async function () {
547 this.timeout(160_000)
548
549 let tempFixturePath: string
550
551 {
552 tempFixturePath = await generateHighBitrateVideo()
553
554 const bitrate = await getVideoFileBitrate(tempFixturePath)
555 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
556 }
557
558 const attributes = {
559 name: 'high bitrate video',
560 description: 'high bitrate video',
561 fixture: tempFixturePath
562 }
563
564 await servers[1].videos.upload({ attributes })
565
566 await waitJobs(servers)
567
568 for (const server of servers) {
569 const { data } = await server.videos.list()
570
571 const video = data.find(v => v.name === attributes.name)
572
573 for (const resolution of [ '240', '360', '480', '720', '1080' ]) {
574 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-' + resolution + '.mp4'))
575
576 const bitrate = await getVideoFileBitrate(path)
577 const fps = await getVideoFileFPS(path)
578 const resolution2 = await getVideoFileResolution(path)
579
580 expect(resolution2.videoFileResolution.toString()).to.equal(resolution)
581 expect(bitrate).to.be.below(getMaxBitrate(resolution2.videoFileResolution, fps, VIDEO_TRANSCODING_FPS))
582 }
583 }
584 })
585
586 it('Should not transcode to an higher bitrate than the original file', async function () {
587 this.timeout(160_000)
588
589 const newConfig = {
590 transcoding: {
591 enabled: true,
592 resolutions: {
593 '240p': true,
594 '360p': true,
595 '480p': true,
596 '720p': true,
597 '1080p': true,
598 '1440p': true,
599 '2160p': true
600 },
601 webtorrent: { enabled: true },
602 hls: { enabled: true }
603 }
604 }
605 await servers[1].config.updateCustomSubConfig({ newConfig })
606
607 const attributes = {
608 name: 'low bitrate',
609 fixture: 'low-bitrate.mp4'
610 }
611
612 const { uuid } = await servers[1].videos.upload({ attributes })
613
614 await waitJobs(servers)
615
616 const resolutions = [ 240, 360, 480, 720, 1080 ]
617 for (const r of resolutions) {
618 const path = `videos/${uuid}-${r}.mp4`
619 const size = await servers[1].servers.getServerFileSize(path)
620 expect(size, `${path} not below ${60_000}`).to.be.below(60_000)
621 }
622 })
623 })
624
625 describe('FFprobe', function () {
626
627 it('Should provide valid ffprobe data', async function () {
628 this.timeout(160_000)
629
630 const videoUUID = (await servers[1].videos.quickUpload({ name: 'ffprobe data' })).uuid
631 await waitJobs(servers)
632
633 {
634 const path = servers[1].servers.buildDirectory(join('videos', videoUUID + '-240.mp4'))
635 const metadata = await getMetadataFromFile(path)
636
637 // expected format properties
638 for (const p of [
639 'tags.encoder',
640 'format_long_name',
641 'size',
642 'bit_rate'
643 ]) {
644 expect(metadata.format).to.have.nested.property(p)
645 }
646
647 // expected stream properties
648 for (const p of [
649 'codec_long_name',
650 'profile',
651 'width',
652 'height',
653 'display_aspect_ratio',
654 'avg_frame_rate',
655 'pix_fmt'
656 ]) {
657 expect(metadata.streams[0]).to.have.nested.property(p)
658 }
659
660 expect(metadata).to.not.have.nested.property('format.filename')
661 }
662
663 for (const server of servers) {
664 const videoDetails = await server.videos.get({ id: videoUUID })
665
666 const videoFiles = videoDetails.files
667 .concat(videoDetails.streamingPlaylists[0].files)
668 expect(videoFiles).to.have.lengthOf(8)
669
670 for (const file of videoFiles) {
671 expect(file.metadata).to.be.undefined
672 expect(file.metadataUrl).to.exist
673 expect(file.metadataUrl).to.contain(servers[1].url)
674 expect(file.metadataUrl).to.contain(videoUUID)
675
676 const metadata = await server.videos.getFileMetadata({ url: file.metadataUrl })
677 expect(metadata).to.have.nested.property('format.size')
678 }
679 }
680 })
681
682 it('Should correctly detect if quick transcode is possible', async function () {
683 this.timeout(10_000)
684
685 expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.mp4'))).to.be.true
686 expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.webm'))).to.be.false
687 })
688 })
689
690 describe('Transcoding job queue', function () {
691
692 it('Should have the appropriate priorities for transcoding jobs', async function () {
693 const body = await servers[1].jobs.getJobsList({
694 start: 0,
695 count: 100,
696 sort: '-createdAt',
697 jobType: 'video-transcoding'
698 })
699
700 const jobs = body.data
701 const transcodingJobs = jobs.filter(j => j.data.videoUUID === video4k)
702
703 expect(transcodingJobs).to.have.lengthOf(14)
704
705 const hlsJobs = transcodingJobs.filter(j => j.data.type === 'new-resolution-to-hls')
706 const webtorrentJobs = transcodingJobs.filter(j => j.data.type === 'new-resolution-to-webtorrent')
707 const optimizeJobs = transcodingJobs.filter(j => j.data.type === 'optimize-to-webtorrent')
708
709 expect(hlsJobs).to.have.lengthOf(7)
710 expect(webtorrentJobs).to.have.lengthOf(6)
711 expect(optimizeJobs).to.have.lengthOf(1)
712
713 for (const j of optimizeJobs.concat(hlsJobs.concat(webtorrentJobs))) {
714 expect(j.priority).to.be.greaterThan(100)
715 expect(j.priority).to.be.lessThan(150)
716 }
717 })
718 })
719
720 after(async function () {
721 await cleanupTests(servers)
722 })
723 })