]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/video-transcoder.ts
Merge branch 'release/3.3.0' into develop
[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 {
8 buildAbsoluteFixturePath,
9 cleanupTests,
10 createMultipleServers,
11 doubleFollow,
12 generateHighBitrateVideo,
13 generateVideoWithFramerate,
14 makeGetRequest,
15 PeerTubeServer,
16 setAccessTokensToServers,
17 waitJobs,
18 webtorrentAdd
19 } from '@shared/extra-utils'
20 import { getMaxBitrate, HttpStatusCode, VideoResolution, VideoState } from '@shared/models'
21 import { VIDEO_TRANSCODING_FPS } from '../../../../server/initializers/constants'
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 let tempFixturePath: string
195
196 {
197 tempFixturePath = await generateHighBitrateVideo()
198
199 const bitrate = await getVideoFileBitrate(tempFixturePath)
200 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
201 }
202
203 for (const fixture of [ 'video_short.mkv', 'video_short.avi' ]) {
204 const attributes = {
205 name: fixture,
206 fixture
207 }
208
209 await servers[1].videos.upload({ attributes })
210
211 await waitJobs(servers)
212
213 for (const server of servers) {
214 const { data } = await server.videos.list()
215
216 const video = data.find(v => v.name === attributes.name)
217 const videoDetails = await server.videos.get({ id: video.id })
218 expect(videoDetails.files).to.have.lengthOf(4)
219
220 const magnetUri = videoDetails.files[0].magnetUri
221 expect(magnetUri).to.contain('.mp4')
222 }
223 }
224 })
225
226 it('Should transcode a 4k video', async function () {
227 this.timeout(200_000)
228
229 const attributes = {
230 name: '4k video',
231 fixture: 'video_short_4k.mp4'
232 }
233
234 const { uuid } = await servers[1].videos.upload({ attributes })
235 video4k = uuid
236
237 await waitJobs(servers)
238
239 const resolutions = [ 240, 360, 480, 720, 1080, 1440, 2160 ]
240
241 for (const server of servers) {
242 const videoDetails = await server.videos.get({ id: video4k })
243 expect(videoDetails.files).to.have.lengthOf(resolutions.length)
244
245 for (const r of resolutions) {
246 expect(videoDetails.files.find(f => f.resolution.id === r)).to.not.be.undefined
247 expect(videoDetails.streamingPlaylists[0].files.find(f => f.resolution.id === r)).to.not.be.undefined
248 }
249 }
250 })
251 })
252
253 describe('Audio transcoding', function () {
254
255 it('Should transcode high bit rate mp3 to proper bit rate', async function () {
256 this.timeout(60_000)
257
258 const attributes = {
259 name: 'mp3_256k',
260 fixture: 'video_short_mp3_256k.mp4'
261 }
262 await servers[1].videos.upload({ attributes })
263
264 await waitJobs(servers)
265
266 for (const server of servers) {
267 const { data } = await server.videos.list()
268
269 const video = data.find(v => v.name === attributes.name)
270 const videoDetails = await server.videos.get({ id: video.id })
271
272 expect(videoDetails.files).to.have.lengthOf(4)
273
274 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-240.mp4'))
275 const probe = await getAudioStream(path)
276
277 if (probe.audioStream) {
278 expect(probe.audioStream['codec_name']).to.be.equal('aac')
279 expect(probe.audioStream['bit_rate']).to.be.at.most(384 * 8000)
280 } else {
281 this.fail('Could not retrieve the audio stream on ' + probe.absolutePath)
282 }
283 }
284 })
285
286 it('Should transcode video with no audio and have no audio itself', async function () {
287 this.timeout(60_000)
288
289 const attributes = {
290 name: 'no_audio',
291 fixture: 'video_short_no_audio.mp4'
292 }
293 await servers[1].videos.upload({ attributes })
294
295 await waitJobs(servers)
296
297 for (const server of servers) {
298 const { data } = await server.videos.list()
299
300 const video = data.find(v => v.name === attributes.name)
301 const videoDetails = await server.videos.get({ id: video.id })
302
303 expect(videoDetails.files).to.have.lengthOf(4)
304 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-240.mp4'))
305 const probe = await getAudioStream(path)
306 expect(probe).to.not.have.property('audioStream')
307 }
308 })
309
310 it('Should leave the audio untouched, but properly transcode the video', async function () {
311 this.timeout(60_000)
312
313 const attributes = {
314 name: 'untouched_audio',
315 fixture: 'video_short.mp4'
316 }
317 await servers[1].videos.upload({ attributes })
318
319 await waitJobs(servers)
320
321 for (const server of servers) {
322 const { data } = await server.videos.list()
323
324 const video = data.find(v => v.name === attributes.name)
325 const videoDetails = await server.videos.get({ id: video.id })
326
327 expect(videoDetails.files).to.have.lengthOf(4)
328
329 const fixturePath = buildAbsoluteFixturePath(attributes.fixture)
330 const fixtureVideoProbe = await getAudioStream(fixturePath)
331 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-240.mp4'))
332
333 const videoProbe = await getAudioStream(path)
334
335 if (videoProbe.audioStream && fixtureVideoProbe.audioStream) {
336 const toOmit = [ 'max_bit_rate', 'duration', 'duration_ts', 'nb_frames', 'start_time', 'start_pts' ]
337 expect(omit(videoProbe.audioStream, toOmit)).to.be.deep.equal(omit(fixtureVideoProbe.audioStream, toOmit))
338 } else {
339 this.fail('Could not retrieve the audio stream on ' + videoProbe.absolutePath)
340 }
341 }
342 })
343 })
344
345 describe('Audio upload', function () {
346
347 function runSuite (mode: 'legacy' | 'resumable') {
348
349 before(async function () {
350 await servers[1].config.updateCustomSubConfig({
351 newConfig: {
352 transcoding: {
353 hls: { enabled: true },
354 webtorrent: { enabled: true },
355 resolutions: {
356 '0p': false,
357 '240p': false,
358 '360p': false,
359 '480p': false,
360 '720p': false,
361 '1080p': false,
362 '1440p': false,
363 '2160p': false
364 }
365 }
366 }
367 })
368 })
369
370 it('Should merge an audio file with the preview file', async function () {
371 this.timeout(60_000)
372
373 const attributes = { name: 'audio_with_preview', previewfile: 'preview.jpg', fixture: 'sample.ogg' }
374 await servers[1].videos.upload({ attributes, mode })
375
376 await waitJobs(servers)
377
378 for (const server of servers) {
379 const { data } = await server.videos.list()
380
381 const video = data.find(v => v.name === 'audio_with_preview')
382 const videoDetails = await server.videos.get({ id: video.id })
383
384 expect(videoDetails.files).to.have.lengthOf(1)
385
386 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 })
387 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, expectedStatus: HttpStatusCode.OK_200 })
388
389 const magnetUri = videoDetails.files[0].magnetUri
390 expect(magnetUri).to.contain('.mp4')
391 }
392 })
393
394 it('Should upload an audio file and choose a default background image', async function () {
395 this.timeout(60_000)
396
397 const attributes = { name: 'audio_without_preview', fixture: 'sample.ogg' }
398 await servers[1].videos.upload({ attributes, mode })
399
400 await waitJobs(servers)
401
402 for (const server of servers) {
403 const { data } = await server.videos.list()
404
405 const video = data.find(v => v.name === 'audio_without_preview')
406 const videoDetails = await server.videos.get({ id: video.id })
407
408 expect(videoDetails.files).to.have.lengthOf(1)
409
410 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 })
411 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, expectedStatus: HttpStatusCode.OK_200 })
412
413 const magnetUri = videoDetails.files[0].magnetUri
414 expect(magnetUri).to.contain('.mp4')
415 }
416 })
417
418 it('Should upload an audio file and create an audio version only', async function () {
419 this.timeout(60_000)
420
421 await servers[1].config.updateCustomSubConfig({
422 newConfig: {
423 transcoding: {
424 hls: { enabled: true },
425 webtorrent: { enabled: true },
426 resolutions: {
427 '0p': true,
428 '240p': false,
429 '360p': false
430 }
431 }
432 }
433 })
434
435 const attributes = { name: 'audio_with_preview', previewfile: 'preview.jpg', fixture: 'sample.ogg' }
436 const { id } = await servers[1].videos.upload({ attributes, mode })
437
438 await waitJobs(servers)
439
440 for (const server of servers) {
441 const videoDetails = await server.videos.get({ id })
442
443 for (const files of [ videoDetails.files, videoDetails.streamingPlaylists[0].files ]) {
444 expect(files).to.have.lengthOf(2)
445 expect(files.find(f => f.resolution.id === 0)).to.not.be.undefined
446 }
447 }
448
449 await updateConfigForTranscoding(servers[1])
450 })
451 }
452
453 describe('Legacy upload', function () {
454 runSuite('legacy')
455 })
456
457 describe('Resumable upload', function () {
458 runSuite('resumable')
459 })
460 })
461
462 describe('Framerate', function () {
463
464 it('Should transcode a 60 FPS video', async function () {
465 this.timeout(60_000)
466
467 const attributes = {
468 name: 'my super 30fps name for server 2',
469 description: 'my super 30fps description for server 2',
470 fixture: '60fps_720p_small.mp4'
471 }
472 await servers[1].videos.upload({ attributes })
473
474 await waitJobs(servers)
475
476 for (const server of servers) {
477 const { data } = await server.videos.list()
478
479 const video = data.find(v => v.name === attributes.name)
480 const videoDetails = await server.videos.get({ id: video.id })
481
482 expect(videoDetails.files).to.have.lengthOf(4)
483 expect(videoDetails.files[0].fps).to.be.above(58).and.below(62)
484 expect(videoDetails.files[1].fps).to.be.below(31)
485 expect(videoDetails.files[2].fps).to.be.below(31)
486 expect(videoDetails.files[3].fps).to.be.below(31)
487
488 for (const resolution of [ '240', '360', '480' ]) {
489 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-' + resolution + '.mp4'))
490 const fps = await getVideoFileFPS(path)
491
492 expect(fps).to.be.below(31)
493 }
494
495 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-720.mp4'))
496 const fps = await getVideoFileFPS(path)
497
498 expect(fps).to.be.above(58).and.below(62)
499 }
500 })
501
502 it('Should downscale to the closest divisor standard framerate', async function () {
503 this.timeout(200_000)
504
505 let tempFixturePath: string
506
507 {
508 tempFixturePath = await generateVideoWithFramerate(59)
509
510 const fps = await getVideoFileFPS(tempFixturePath)
511 expect(fps).to.be.equal(59)
512 }
513
514 const attributes = {
515 name: '59fps video',
516 description: '59fps video',
517 fixture: tempFixturePath
518 }
519
520 await servers[1].videos.upload({ attributes })
521
522 await waitJobs(servers)
523
524 for (const server of servers) {
525 const { data } = await server.videos.list()
526
527 const video = data.find(v => v.name === attributes.name)
528
529 {
530 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-240.mp4'))
531 const fps = await getVideoFileFPS(path)
532 expect(fps).to.be.equal(25)
533 }
534
535 {
536 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-720.mp4'))
537 const fps = await getVideoFileFPS(path)
538 expect(fps).to.be.equal(59)
539 }
540 }
541 })
542 })
543
544 describe('Bitrate control', function () {
545 it('Should respect maximum bitrate values', async function () {
546 this.timeout(160_000)
547
548 let tempFixturePath: string
549
550 {
551 tempFixturePath = await generateHighBitrateVideo()
552
553 const bitrate = await getVideoFileBitrate(tempFixturePath)
554 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
555 }
556
557 const attributes = {
558 name: 'high bitrate video',
559 description: 'high bitrate video',
560 fixture: tempFixturePath
561 }
562
563 await servers[1].videos.upload({ attributes })
564
565 await waitJobs(servers)
566
567 for (const server of servers) {
568 const { data } = await server.videos.list()
569
570 const video = data.find(v => v.name === attributes.name)
571
572 for (const resolution of [ '240', '360', '480', '720', '1080' ]) {
573 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-' + resolution + '.mp4'))
574
575 const bitrate = await getVideoFileBitrate(path)
576 const fps = await getVideoFileFPS(path)
577 const resolution2 = await getVideoFileResolution(path)
578
579 expect(resolution2.videoFileResolution.toString()).to.equal(resolution)
580 expect(bitrate).to.be.below(getMaxBitrate(resolution2.videoFileResolution, fps, VIDEO_TRANSCODING_FPS))
581 }
582 }
583 })
584
585 it('Should not transcode to an higher bitrate than the original file', async function () {
586 this.timeout(160_000)
587
588 const newConfig = {
589 transcoding: {
590 enabled: true,
591 resolutions: {
592 '240p': true,
593 '360p': true,
594 '480p': true,
595 '720p': true,
596 '1080p': true,
597 '1440p': true,
598 '2160p': true
599 },
600 webtorrent: { enabled: true },
601 hls: { enabled: true }
602 }
603 }
604 await servers[1].config.updateCustomSubConfig({ newConfig })
605
606 const attributes = {
607 name: 'low bitrate',
608 fixture: 'low-bitrate.mp4'
609 }
610
611 const { uuid } = await servers[1].videos.upload({ attributes })
612
613 await waitJobs(servers)
614
615 const resolutions = [ 240, 360, 480, 720, 1080 ]
616 for (const r of resolutions) {
617 const path = `videos/${uuid}-${r}.mp4`
618 const size = await servers[1].servers.getServerFileSize(path)
619 expect(size, `${path} not below ${60_000}`).to.be.below(60_000)
620 }
621 })
622 })
623
624 describe('FFprobe', function () {
625
626 it('Should provide valid ffprobe data', async function () {
627 this.timeout(160_000)
628
629 const videoUUID = (await servers[1].videos.quickUpload({ name: 'ffprobe data' })).uuid
630 await waitJobs(servers)
631
632 {
633 const path = servers[1].servers.buildDirectory(join('videos', videoUUID + '-240.mp4'))
634 const metadata = await getMetadataFromFile(path)
635
636 // expected format properties
637 for (const p of [
638 'tags.encoder',
639 'format_long_name',
640 'size',
641 'bit_rate'
642 ]) {
643 expect(metadata.format).to.have.nested.property(p)
644 }
645
646 // expected stream properties
647 for (const p of [
648 'codec_long_name',
649 'profile',
650 'width',
651 'height',
652 'display_aspect_ratio',
653 'avg_frame_rate',
654 'pix_fmt'
655 ]) {
656 expect(metadata.streams[0]).to.have.nested.property(p)
657 }
658
659 expect(metadata).to.not.have.nested.property('format.filename')
660 }
661
662 for (const server of servers) {
663 const videoDetails = await server.videos.get({ id: videoUUID })
664
665 const videoFiles = videoDetails.files
666 .concat(videoDetails.streamingPlaylists[0].files)
667 expect(videoFiles).to.have.lengthOf(8)
668
669 for (const file of videoFiles) {
670 expect(file.metadata).to.be.undefined
671 expect(file.metadataUrl).to.exist
672 expect(file.metadataUrl).to.contain(servers[1].url)
673 expect(file.metadataUrl).to.contain(videoUUID)
674
675 const metadata = await server.videos.getFileMetadata({ url: file.metadataUrl })
676 expect(metadata).to.have.nested.property('format.size')
677 }
678 }
679 })
680
681 it('Should correctly detect if quick transcode is possible', async function () {
682 this.timeout(10_000)
683
684 expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.mp4'))).to.be.true
685 expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.webm'))).to.be.false
686 })
687 })
688
689 describe('Transcoding job queue', function () {
690
691 it('Should have the appropriate priorities for transcoding jobs', async function () {
692 const body = await servers[1].jobs.getJobsList({
693 start: 0,
694 count: 100,
695 sort: '-createdAt',
696 jobType: 'video-transcoding'
697 })
698
699 const jobs = body.data
700 const transcodingJobs = jobs.filter(j => j.data.videoUUID === video4k)
701
702 expect(transcodingJobs).to.have.lengthOf(14)
703
704 const hlsJobs = transcodingJobs.filter(j => j.data.type === 'new-resolution-to-hls')
705 const webtorrentJobs = transcodingJobs.filter(j => j.data.type === 'new-resolution-to-webtorrent')
706 const optimizeJobs = transcodingJobs.filter(j => j.data.type === 'optimize-to-webtorrent')
707
708 expect(hlsJobs).to.have.lengthOf(7)
709 expect(webtorrentJobs).to.have.lengthOf(6)
710 expect(optimizeJobs).to.have.lengthOf(1)
711
712 for (const j of optimizeJobs.concat(hlsJobs.concat(webtorrentJobs))) {
713 expect(j.priority).to.be.greaterThan(100)
714 expect(j.priority).to.be.lessThan(150)
715 }
716 })
717 })
718
719 after(async function () {
720 await cleanupTests(servers)
721 })
722 })