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