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