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