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