]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/video-transcoder.ts
Split ffmpeg utils with ffprobe utils
[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
9 import { VIDEO_TRANSCODING_FPS } from '../../../../server/initializers/constants'
10 import {
11 buildAbsoluteFixturePath,
12 cleanupTests,
13 doubleFollow,
14 flushAndRunMultipleServers,
15 generateHighBitrateVideo,
16 generateVideoWithFramerate,
17 getMyVideos,
18 getServerFileSize,
19 getVideo,
20 getVideoFileMetadataUrl,
21 getVideosList,
22 makeGetRequest,
23 root,
24 ServerInfo,
25 setAccessTokensToServers,
26 updateCustomSubConfig,
27 uploadVideo,
28 uploadVideoAndGetId,
29 waitJobs,
30 webtorrentAdd
31 } from '../../../../shared/extra-utils'
32 import { getMaxBitrate, VideoDetails, VideoResolution, VideoState } from '../../../../shared/models/videos'
33 import {
34 canDoQuickTranscode,
35 getAudioStream,
36 getMetadataFromFile,
37 getVideoFileBitrate,
38 getVideoFileFPS,
39 getVideoFileResolution
40 } from '../../../helpers/ffprobe-utils'
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 = join(root(), 'test' + servers[1].internalServerNumber, '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 = join(root(), 'test' + servers[1].internalServerNumber, '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 const fixturePath = buildAbsoluteFixturePath(videoAttributes.fixture)
196 const fixtureVideoProbe = await getAudioStream(fixturePath)
197 const path = join(root(), 'test' + servers[1].internalServerNumber, 'videos', video.uuid + '-240.mp4')
198 const videoProbe = await getAudioStream(path)
199 if (videoProbe.audioStream && fixtureVideoProbe.audioStream) {
200 const toOmit = [ 'max_bit_rate', 'duration', 'duration_ts', 'nb_frames', 'start_time', 'start_pts' ]
201 expect(omit(videoProbe.audioStream, toOmit)).to.be.deep.equal(omit(fixtureVideoProbe.audioStream, toOmit))
202 } else {
203 this.fail('Could not retrieve the audio stream on ' + videoProbe.absolutePath)
204 }
205 }
206 })
207
208 it('Should transcode a 60 FPS video', async function () {
209 this.timeout(60000)
210
211 const videoAttributes = {
212 name: 'my super 30fps name for server 2',
213 description: 'my super 30fps description for server 2',
214 fixture: '60fps_720p_small.mp4'
215 }
216 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
217
218 await waitJobs(servers)
219
220 for (const server of servers) {
221 const res = await getVideosList(server.url)
222
223 const video = res.body.data.find(v => v.name === videoAttributes.name)
224 const res2 = await getVideo(server.url, video.id)
225 const videoDetails: VideoDetails = res2.body
226
227 expect(videoDetails.files).to.have.lengthOf(4)
228 expect(videoDetails.files[0].fps).to.be.above(58).and.below(62)
229 expect(videoDetails.files[1].fps).to.be.below(31)
230 expect(videoDetails.files[2].fps).to.be.below(31)
231 expect(videoDetails.files[3].fps).to.be.below(31)
232
233 for (const resolution of [ '240', '360', '480' ]) {
234 const path = join(root(), 'test' + servers[1].internalServerNumber, 'videos', video.uuid + '-' + resolution + '.mp4')
235 const fps = await getVideoFileFPS(path)
236
237 expect(fps).to.be.below(31)
238 }
239
240 const path = join(root(), 'test' + servers[1].internalServerNumber, 'videos', video.uuid + '-720.mp4')
241 const fps = await getVideoFileFPS(path)
242
243 expect(fps).to.be.above(58).and.below(62)
244 }
245 })
246
247 it('Should wait for transcoding before publishing the video', async function () {
248 this.timeout(160000)
249
250 {
251 // Upload the video, but wait transcoding
252 const videoAttributes = {
253 name: 'waiting video',
254 fixture: 'video_short1.webm',
255 waitTranscoding: true
256 }
257 const resVideo = await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
258 const videoId = resVideo.body.video.uuid
259
260 // Should be in transcode state
261 const { body } = await getVideo(servers[1].url, videoId)
262 expect(body.name).to.equal('waiting video')
263 expect(body.state.id).to.equal(VideoState.TO_TRANSCODE)
264 expect(body.state.label).to.equal('To transcode')
265 expect(body.waitTranscoding).to.be.true
266
267 // Should have my video
268 const resMyVideos = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 10)
269 const videoToFindInMine = resMyVideos.body.data.find(v => v.name === videoAttributes.name)
270 expect(videoToFindInMine).not.to.be.undefined
271 expect(videoToFindInMine.state.id).to.equal(VideoState.TO_TRANSCODE)
272 expect(videoToFindInMine.state.label).to.equal('To transcode')
273 expect(videoToFindInMine.waitTranscoding).to.be.true
274
275 // Should not list this video
276 const resVideos = await getVideosList(servers[1].url)
277 const videoToFindInList = resVideos.body.data.find(v => v.name === videoAttributes.name)
278 expect(videoToFindInList).to.be.undefined
279
280 // Server 1 should not have the video yet
281 await getVideo(servers[0].url, videoId, 404)
282 }
283
284 await waitJobs(servers)
285
286 for (const server of servers) {
287 const res = await getVideosList(server.url)
288 const videoToFind = res.body.data.find(v => v.name === 'waiting video')
289 expect(videoToFind).not.to.be.undefined
290
291 const res2 = await getVideo(server.url, videoToFind.id)
292 const videoDetails: VideoDetails = res2.body
293
294 expect(videoDetails.state.id).to.equal(VideoState.PUBLISHED)
295 expect(videoDetails.state.label).to.equal('Published')
296 expect(videoDetails.waitTranscoding).to.be.true
297 }
298 })
299
300 it('Should respect maximum bitrate values', async function () {
301 this.timeout(160000)
302
303 let tempFixturePath: string
304
305 {
306 tempFixturePath = await generateHighBitrateVideo()
307
308 const bitrate = await getVideoFileBitrate(tempFixturePath)
309 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
310 }
311
312 const videoAttributes = {
313 name: 'high bitrate video',
314 description: 'high bitrate video',
315 fixture: tempFixturePath
316 }
317
318 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
319
320 await waitJobs(servers)
321
322 for (const server of servers) {
323 const res = await getVideosList(server.url)
324
325 const video = res.body.data.find(v => v.name === videoAttributes.name)
326
327 for (const resolution of [ '240', '360', '480', '720', '1080' ]) {
328 const path = join(root(), 'test' + servers[1].internalServerNumber, 'videos', video.uuid + '-' + resolution + '.mp4')
329 const bitrate = await getVideoFileBitrate(path)
330 const fps = await getVideoFileFPS(path)
331 const resolution2 = await getVideoFileResolution(path)
332
333 expect(resolution2.videoFileResolution.toString()).to.equal(resolution)
334 expect(bitrate).to.be.below(getMaxBitrate(resolution2.videoFileResolution, fps, VIDEO_TRANSCODING_FPS))
335 }
336 }
337 })
338
339 it('Should accept and transcode additional extensions', async function () {
340 this.timeout(300000)
341
342 let tempFixturePath: string
343
344 {
345 tempFixturePath = await generateHighBitrateVideo()
346
347 const bitrate = await getVideoFileBitrate(tempFixturePath)
348 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
349 }
350
351 for (const fixture of [ 'video_short.mkv', 'video_short.avi' ]) {
352 const videoAttributes = {
353 name: fixture,
354 fixture
355 }
356
357 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
358
359 await waitJobs(servers)
360
361 for (const server of servers) {
362 const res = await getVideosList(server.url)
363
364 const video = res.body.data.find(v => v.name === videoAttributes.name)
365 const res2 = await getVideo(server.url, video.id)
366 const videoDetails = res2.body
367
368 expect(videoDetails.files).to.have.lengthOf(4)
369
370 const magnetUri = videoDetails.files[0].magnetUri
371 expect(magnetUri).to.contain('.mp4')
372 }
373 }
374 })
375
376 it('Should correctly detect if quick transcode is possible', async function () {
377 this.timeout(10000)
378
379 expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.mp4'))).to.be.true
380 expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.webm'))).to.be.false
381 })
382
383 it('Should merge an audio file with the preview file', async function () {
384 this.timeout(60000)
385
386 const videoAttributesArg = { name: 'audio_with_preview', previewfile: 'preview.jpg', fixture: 'sample.ogg' }
387 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributesArg)
388
389 await waitJobs(servers)
390
391 for (const server of servers) {
392 const res = await getVideosList(server.url)
393
394 const video = res.body.data.find(v => v.name === 'audio_with_preview')
395 const res2 = await getVideo(server.url, video.id)
396 const videoDetails: VideoDetails = res2.body
397
398 expect(videoDetails.files).to.have.lengthOf(1)
399
400 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, statusCodeExpected: 200 })
401 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, statusCodeExpected: 200 })
402
403 const magnetUri = videoDetails.files[0].magnetUri
404 expect(magnetUri).to.contain('.mp4')
405 }
406 })
407
408 it('Should upload an audio file and choose a default background image', async function () {
409 this.timeout(60000)
410
411 const videoAttributesArg = { name: 'audio_without_preview', fixture: 'sample.ogg' }
412 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributesArg)
413
414 await waitJobs(servers)
415
416 for (const server of servers) {
417 const res = await getVideosList(server.url)
418
419 const video = res.body.data.find(v => v.name === 'audio_without_preview')
420 const res2 = await getVideo(server.url, video.id)
421 const videoDetails = res2.body
422
423 expect(videoDetails.files).to.have.lengthOf(1)
424
425 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, statusCodeExpected: 200 })
426 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, statusCodeExpected: 200 })
427
428 const magnetUri = videoDetails.files[0].magnetUri
429 expect(magnetUri).to.contain('.mp4')
430 }
431 })
432
433 it('Should downscale to the closest divisor standard framerate', async function () {
434 this.timeout(160000)
435
436 let tempFixturePath: string
437
438 {
439 tempFixturePath = await generateVideoWithFramerate(59)
440
441 const fps = await getVideoFileFPS(tempFixturePath)
442 expect(fps).to.be.equal(59)
443 }
444
445 const videoAttributes = {
446 name: '59fps video',
447 description: '59fps video',
448 fixture: tempFixturePath
449 }
450
451 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
452
453 await waitJobs(servers)
454
455 for (const server of servers) {
456 const res = await getVideosList(server.url)
457
458 const video = res.body.data.find(v => v.name === videoAttributes.name)
459
460 {
461 const path = join(root(), 'test' + servers[1].internalServerNumber, 'videos', video.uuid + '-240.mp4')
462 const fps = await getVideoFileFPS(path)
463 expect(fps).to.be.equal(25)
464 }
465
466 {
467 const path = join(root(), 'test' + servers[1].internalServerNumber, 'videos', video.uuid + '-720.mp4')
468 const fps = await getVideoFileFPS(path)
469 expect(fps).to.be.equal(59)
470 }
471 }
472 })
473
474 it('Should not transcode to an higher bitrate than the original file', async function () {
475 this.timeout(160000)
476
477 const config = {
478 transcoding: {
479 enabled: true,
480 resolutions: {
481 '240p': true,
482 '360p': true,
483 '480p': true,
484 '720p': true,
485 '1080p': true
486 },
487 webtorrent: { enabled: true },
488 hls: { enabled: true }
489 }
490 }
491 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, config)
492
493 const videoAttributes = {
494 name: 'low bitrate',
495 fixture: 'low-bitrate.mp4'
496 }
497
498 const resUpload = await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
499 const videoUUID = resUpload.body.video.uuid
500
501 await waitJobs(servers)
502
503 const resolutions = [ 240, 360, 480, 720, 1080 ]
504 for (const r of resolutions) {
505 expect(await getServerFileSize(servers[1], `videos/${videoUUID}-${r}.mp4`)).to.be.below(60000)
506 }
507 })
508
509 it('Should provide valid ffprobe data', async function () {
510 this.timeout(160000)
511
512 const videoUUID = (await uploadVideoAndGetId({ server: servers[1], videoName: 'ffprobe data' })).uuid
513 await waitJobs(servers)
514
515 {
516 const path = join(root(), 'test' + servers[1].internalServerNumber, 'videos', videoUUID + '-240.mp4')
517 const metadata = await getMetadataFromFile(path)
518
519 // expected format properties
520 for (const p of [
521 'tags.encoder',
522 'format_long_name',
523 'size',
524 'bit_rate'
525 ]) {
526 expect(metadata.format).to.have.nested.property(p)
527 }
528
529 // expected stream properties
530 for (const p of [
531 'codec_long_name',
532 'profile',
533 'width',
534 'height',
535 'display_aspect_ratio',
536 'avg_frame_rate',
537 'pix_fmt'
538 ]) {
539 expect(metadata.streams[0]).to.have.nested.property(p)
540 }
541
542 expect(metadata).to.not.have.nested.property('format.filename')
543 }
544
545 for (const server of servers) {
546 const res2 = await getVideo(server.url, videoUUID)
547 const videoDetails: VideoDetails = res2.body
548
549 const videoFiles = videoDetails.files
550 .concat(videoDetails.streamingPlaylists[0].files)
551 expect(videoFiles).to.have.lengthOf(8)
552
553 for (const file of videoFiles) {
554 expect(file.metadata).to.be.undefined
555 expect(file.metadataUrl).to.exist
556 expect(file.metadataUrl).to.contain(servers[1].url)
557 expect(file.metadataUrl).to.contain(videoUUID)
558
559 const res3 = await getVideoFileMetadataUrl(file.metadataUrl)
560 const metadata: FfprobeData = res3.body
561 expect(metadata).to.have.nested.property('format.size')
562 }
563 }
564 })
565
566 after(async function () {
567 await cleanupTests(servers)
568 })
569 })