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