]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
0e1dc3e7 2
a7ba16b6 3import 'mocha'
daf6e480
C
4import * as chai from 'chai'
5import { FfprobeData } from 'fluent-ffmpeg'
7160878c 6import { omit } from 'lodash'
daf6e480 7import { join } from 'path'
daf6e480 8import { VIDEO_TRANSCODING_FPS } from '../../../../server/initializers/constants'
0e1dc3e7 9import {
7243f84d 10 buildAbsoluteFixturePath,
ca5c612b 11 buildServerDirectory,
7243f84d 12 cleanupTests,
2186386c
C
13 doubleFollow,
14 flushAndRunMultipleServers,
d175a6f7 15 generateHighBitrateVideo,
837666fe 16 generateVideoWithFramerate,
2186386c 17 getMyVideos,
d218e7de 18 getServerFileSize,
2186386c 19 getVideo,
8319d6ae 20 getVideoFileMetadataUrl,
2186386c 21 getVideosList,
b345a804 22 makeGetRequest,
2186386c
C
23 ServerInfo,
24 setAccessTokensToServers,
d218e7de 25 updateCustomSubConfig,
daf6e480
C
26 uploadVideo,
27 uploadVideoAndGetId,
1600235a 28 waitJobs,
d175a6f7 29 webtorrentAdd
94565d52 30} from '../../../../shared/extra-utils'
daf6e480
C
31import { getMaxBitrate, VideoDetails, VideoResolution, VideoState } from '../../../../shared/models/videos'
32import {
33 canDoQuickTranscode,
34 getAudioStream,
35 getMetadataFromFile,
36 getVideoFileBitrate,
37 getVideoFileFPS,
38 getVideoFileResolution
39} from '../../../helpers/ffprobe-utils'
2d53be02 40import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
a7ba16b6
C
41
42const expect = chai.expect
0e1dc3e7
C
43
44describe('Test video transcoding', function () {
45 let servers: ServerInfo[] = []
46
47 before(async function () {
e212f887 48 this.timeout(30000)
0e1dc3e7
C
49
50 // Run servers
51 servers = await flushAndRunMultipleServers(2)
52
53 await setAccessTokensToServers(servers)
b2977eec
C
54
55 await doubleFollow(servers[0], servers[1])
0e1dc3e7
C
56 })
57
58 it('Should not transcode video on server 1', async function () {
59 this.timeout(60000)
60
61 const videoAttributes = {
975e6e0e
C
62 name: 'my super name for server 1',
63 description: 'my super description for server 1',
0e1dc3e7
C
64 fixture: 'video_short.webm'
65 }
66 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
67
3cd0734f 68 await waitJobs(servers)
0e1dc3e7 69
b2977eec
C
70 for (const server of servers) {
71 const res = await getVideosList(server.url)
a1587156 72 const video = res.body.data[0]
40298b02 73
b2977eec
C
74 const res2 = await getVideo(server.url, video.id)
75 const videoDetails = res2.body
76 expect(videoDetails.files).to.have.lengthOf(1)
5f04dd2f 77
a1587156 78 const magnetUri = videoDetails.files[0].magnetUri
b2977eec 79 expect(magnetUri).to.match(/\.webm/)
0e1dc3e7 80
b2977eec
C
81 const torrent = await webtorrentAdd(magnetUri, true)
82 expect(torrent.files).to.be.an('array')
83 expect(torrent.files.length).to.equal(1)
a1587156 84 expect(torrent.files[0].path).match(/\.webm$/)
b2977eec 85 }
0e1dc3e7
C
86 })
87
88 it('Should transcode video on server 2', async function () {
c6c0fa6c 89 this.timeout(120000)
0e1dc3e7
C
90
91 const videoAttributes = {
975e6e0e
C
92 name: 'my super name for server 2',
93 description: 'my super description for server 2',
0e1dc3e7
C
94 fixture: 'video_short.webm'
95 }
96 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
97
3cd0734f 98 await waitJobs(servers)
0e1dc3e7 99
b2977eec
C
100 for (const server of servers) {
101 const res = await getVideosList(server.url)
0e1dc3e7 102
b2977eec
C
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
5f04dd2f 106
b2977eec 107 expect(videoDetails.files).to.have.lengthOf(4)
40298b02 108
a1587156 109 const magnetUri = videoDetails.files[0].magnetUri
b2977eec 110 expect(magnetUri).to.match(/\.mp4/)
0e1dc3e7 111
b2977eec
C
112 const torrent = await webtorrentAdd(magnetUri, true)
113 expect(torrent.files).to.be.an('array')
114 expect(torrent.files.length).to.equal(1)
a1587156 115 expect(torrent.files[0].path).match(/\.mp4$/)
b2977eec 116 }
0e1dc3e7
C
117 })
118
7160878c
RK
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
b2977eec
C
130 for (const server of servers) {
131 const res = await getVideosList(server.url)
7160878c 132
b2977eec
C
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
7160878c 136
b2977eec 137 expect(videoDetails.files).to.have.lengthOf(4)
7160878c 138
ca5c612b 139 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-240.mp4'))
daf6e480 140 const probe = await getAudioStream(path)
7160878c 141
b2977eec 142 if (probe.audioStream) {
a1587156
C
143 expect(probe.audioStream['codec_name']).to.be.equal('aac')
144 expect(probe.audioStream['bit_rate']).to.be.at.most(384 * 8000)
b2977eec
C
145 } else {
146 this.fail('Could not retrieve the audio stream on ' + probe.absolutePath)
147 }
7160878c
RK
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
b2977eec
C
162 for (const server of servers) {
163 const res = await getVideosList(server.url)
7160878c 164
b2977eec
C
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
7160878c 168
b2977eec 169 expect(videoDetails.files).to.have.lengthOf(4)
ca5c612b 170 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-240.mp4'))
daf6e480 171 const probe = await getAudioStream(path)
b2977eec
C
172 expect(probe).to.not.have.property('audioStream')
173 }
7160878c
RK
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
b2977eec
C
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)
ca5c612b 195
b2977eec 196 const fixturePath = buildAbsoluteFixturePath(videoAttributes.fixture)
daf6e480 197 const fixtureVideoProbe = await getAudioStream(fixturePath)
ca5c612b
C
198 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-240.mp4'))
199
daf6e480 200 const videoProbe = await getAudioStream(path)
ca5c612b 201
b2977eec
C
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 }
7160878c
RK
208 }
209 })
210
3a6f351b 211 it('Should transcode a 60 FPS video', async function () {
73c69591
C
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',
3a6f351b 217 fixture: '60fps_720p_small.mp4'
73c69591
C
218 }
219 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
220
3cd0734f 221 await waitJobs(servers)
73c69591 222
b2977eec
C
223 for (const server of servers) {
224 const res = await getVideosList(server.url)
73c69591 225
b2977eec
C
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
73c69591 229
b2977eec 230 expect(videoDetails.files).to.have.lengthOf(4)
a1587156
C
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)
73c69591 235
b2977eec 236 for (const resolution of [ '240', '360', '480' ]) {
ca5c612b 237 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-' + resolution + '.mp4'))
b2977eec 238 const fps = await getVideoFileFPS(path)
73c69591 239
b2977eec
C
240 expect(fps).to.be.below(31)
241 }
3a6f351b 242
ca5c612b 243 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-720.mp4'))
b2977eec 244 const fps = await getVideoFileFPS(path)
3a6f351b 245
b2977eec
C
246 expect(fps).to.be.above(58).and.below(62)
247 }
73c69591
C
248 })
249
edb4ffc7 250 it('Should wait for transcoding before publishing the video', async function () {
07491f4b 251 this.timeout(160000)
2186386c 252
2186386c
C
253 {
254 // Upload the video, but wait transcoding
255 const videoAttributes = {
256 name: 'waiting video',
257 fixture: 'video_short1.webm',
258 waitTranscoding: true
259 }
a1587156 260 const resVideo = await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
2186386c
C
261 const videoId = resVideo.body.video.uuid
262
263 // Should be in transcode state
a1587156 264 const { body } = await getVideo(servers[1].url, videoId)
2186386c
C
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)
7160878c 272 const videoToFindInMine = resMyVideos.body.data.find(v => v.name === videoAttributes.name)
2186386c
C
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)
7160878c 280 const videoToFindInList = resVideos.body.data.find(v => v.name === videoAttributes.name)
2186386c
C
281 expect(videoToFindInList).to.be.undefined
282
283 // Server 1 should not have the video yet
2d53be02 284 await getVideo(servers[0].url, videoId, HttpStatusCode.NOT_FOUND_404)
2186386c
C
285 }
286
3cd0734f 287 await waitJobs(servers)
2186386c
C
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
edb4ffc7
FA
303 it('Should respect maximum bitrate values', async function () {
304 this.timeout(160000)
305
74cd011b
C
306 let tempFixturePath: string
307
edb4ffc7 308 {
74cd011b 309 tempFixturePath = await generateHighBitrateVideo()
edb4ffc7
FA
310
311 const bitrate = await getVideoFileBitrate(tempFixturePath)
941c5eac 312 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
c1c86c15 313 }
edb4ffc7 314
c1c86c15
C
315 const videoAttributes = {
316 name: 'high bitrate video',
317 description: 'high bitrate video',
318 fixture: tempFixturePath
319 }
edb4ffc7 320
c1c86c15 321 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
edb4ffc7 322
c1c86c15 323 await waitJobs(servers)
edb4ffc7 324
c1c86c15
C
325 for (const server of servers) {
326 const res = await getVideosList(server.url)
edb4ffc7 327
c1c86c15 328 const video = res.body.data.find(v => v.name === videoAttributes.name)
edb4ffc7 329
a1587156 330 for (const resolution of [ '240', '360', '480', '720', '1080' ]) {
ca5c612b
C
331 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-' + resolution + '.mp4'))
332
c1c86c15
C
333 const bitrate = await getVideoFileBitrate(path)
334 const fps = await getVideoFileFPS(path)
335 const resolution2 = await getVideoFileResolution(path)
edb4ffc7 336
c1c86c15
C
337 expect(resolution2.videoFileResolution.toString()).to.equal(resolution)
338 expect(bitrate).to.be.below(getMaxBitrate(resolution2.videoFileResolution, fps, VIDEO_TRANSCODING_FPS))
edb4ffc7
FA
339 }
340 }
341 })
342
14e2014a
C
343 it('Should accept and transcode additional extensions', async function () {
344 this.timeout(300000)
345
7ed2c1a4
FA
346 let tempFixturePath: string
347
348 {
349 tempFixturePath = await generateHighBitrateVideo()
350
351 const bitrate = await getVideoFileBitrate(tempFixturePath)
941c5eac 352 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
7ed2c1a4
FA
353 }
354
14e2014a
C
355 for (const fixture of [ 'video_short.mkv', 'video_short.avi' ]) {
356 const videoAttributes = {
357 name: fixture,
358 fixture
359 }
360
a1587156 361 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
14e2014a
C
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
a1587156 374 const magnetUri = videoDetails.files[0].magnetUri
14e2014a
C
375 expect(magnetUri).to.contain('.mp4')
376 }
377 }
378 })
379
7ed2c1a4
FA
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
b345a804
C
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' }
a1587156 391 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributesArg)
b345a804
C
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
2d53be02
RK
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 })
b345a804 406
a1587156 407 const magnetUri = videoDetails.files[0].magnetUri
b345a804
C
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' }
a1587156 416 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributesArg)
b345a804
C
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
2d53be02
RK
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 })
b345a804 431
a1587156 432 const magnetUri = videoDetails.files[0].magnetUri
b345a804
C
433 expect(magnetUri).to.contain('.mp4')
434 }
435 })
436
837666fe 437 it('Should downscale to the closest divisor standard framerate', async function () {
9e3e4adc 438 this.timeout(200000)
837666fe
RK
439
440 let tempFixturePath: string
441
442 {
c7f36e4f 443 tempFixturePath = await generateVideoWithFramerate(59)
837666fe
RK
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)
837666fe 463
c7f36e4f 464 {
ca5c612b 465 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-240.mp4'))
c7f36e4f
C
466 const fps = await getVideoFileFPS(path)
467 expect(fps).to.be.equal(25)
468 }
469
470 {
ca5c612b 471 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-720.mp4'))
c7f36e4f
C
472 const fps = await getVideoFileFPS(path)
473 expect(fps).to.be.equal(59)
474 }
837666fe
RK
475 }
476 })
477
d218e7de
C
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) {
8f3ad708 509 expect(await getServerFileSize(servers[1], `videos/${videoUUID}-${r}.mp4`)).to.be.below(60000)
d218e7de
C
510 }
511 })
512
8319d6ae
RK
513 it('Should provide valid ffprobe data', async function () {
514 this.timeout(160000)
515
7b81edc8 516 const videoUUID = (await uploadVideoAndGetId({ server: servers[1], videoName: 'ffprobe data' })).uuid
8319d6ae
RK
517 await waitJobs(servers)
518
7b81edc8 519 {
ca5c612b 520 const path = buildServerDirectory(servers[1], join('videos', videoUUID + '-240.mp4'))
daf6e480 521 const metadata = await getMetadataFromFile(path)
8319d6ae 522
7b81edc8
C
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 }
8319d6ae 532
7b81edc8 533 // expected stream properties
8319d6ae 534 for (const p of [
7b81edc8
C
535 'codec_long_name',
536 'profile',
537 'width',
538 'height',
539 'display_aspect_ratio',
540 'avg_frame_rate',
541 'pix_fmt'
8319d6ae 542 ]) {
7b81edc8 543 expect(metadata.streams[0]).to.have.nested.property(p)
8319d6ae 544 }
7b81edc8 545
8319d6ae
RK
546 expect(metadata).to.not.have.nested.property('format.filename')
547 }
548
549 for (const server of servers) {
7b81edc8
C
550 const res2 = await getVideo(server.url, videoUUID)
551 const videoDetails: VideoDetails = res2.body
8319d6ae
RK
552
553 const videoFiles = videoDetails.files
7b81edc8
C
554 .concat(videoDetails.streamingPlaylists[0].files)
555 expect(videoFiles).to.have.lengthOf(8)
556
557 for (const file of videoFiles) {
8319d6ae 558 expect(file.metadata).to.be.undefined
7b81edc8 559 expect(file.metadataUrl).to.exist
8319d6ae 560 expect(file.metadataUrl).to.contain(servers[1].url)
7b81edc8 561 expect(file.metadataUrl).to.contain(videoUUID)
8319d6ae
RK
562
563 const res3 = await getVideoFileMetadataUrl(file.metadataUrl)
564 const metadata: FfprobeData = res3.body
565 expect(metadata).to.have.nested.property('format.size')
8319d6ae
RK
566 }
567 }
568 })
569
7c3b7976
C
570 after(async function () {
571 await cleanupTests(servers)
0e1dc3e7
C
572 })
573})