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