]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/video-transcoder.ts
Live streaming implementation first step
[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 * as chai from 'chai'
4 import 'mocha'
5 import { omit } from 'lodash'
6 import { getMaxBitrate, VideoDetails, VideoResolution, VideoState } from '../../../../shared/models/videos'
7 import {
8 audio,
9 canDoQuickTranscode,
10 getVideoFileBitrate,
11 getVideoFileFPS,
12 getVideoFileResolution,
13 getMetadataFromFile
14 } from '../../../helpers/ffmpeg-utils'
15 import {
16 buildAbsoluteFixturePath,
17 cleanupTests,
18 doubleFollow,
19 flushAndRunMultipleServers,
20 generateHighBitrateVideo,
21 generateVideoWithFramerate,
22 getMyVideos,
23 getVideo,
24 getVideoFileMetadataUrl,
25 getVideosList,
26 makeGetRequest,
27 root,
28 ServerInfo,
29 setAccessTokensToServers,
30 uploadVideo, uploadVideoAndGetId,
31 waitJobs,
32 webtorrentAdd
33 } from '../../../../shared/extra-utils'
34 import { join } from 'path'
35 import { VIDEO_TRANSCODING_FPS } from '../../../../server/initializers/constants'
36 import { FfprobeData } from 'fluent-ffmpeg'
37 import { VideoFileMetadata } from '@shared/models/videos/video-file-metadata'
38
39 const expect = chai.expect
40
41 describe('Test video transcoding', function () {
42 let servers: ServerInfo[] = []
43
44 before(async function () {
45 this.timeout(30000)
46
47 // Run servers
48 servers = await flushAndRunMultipleServers(2)
49
50 await setAccessTokensToServers(servers)
51
52 await doubleFollow(servers[0], servers[1])
53 })
54
55 it('Should not transcode video on server 1', async function () {
56 this.timeout(60000)
57
58 const videoAttributes = {
59 name: 'my super name for server 1',
60 description: 'my super description for server 1',
61 fixture: 'video_short.webm'
62 }
63 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
64
65 await waitJobs(servers)
66
67 for (const server of servers) {
68 const res = await getVideosList(server.url)
69 const video = res.body.data[0]
70
71 const res2 = await getVideo(server.url, video.id)
72 const videoDetails = res2.body
73 expect(videoDetails.files).to.have.lengthOf(1)
74
75 const magnetUri = videoDetails.files[0].magnetUri
76 expect(magnetUri).to.match(/\.webm/)
77
78 const torrent = await webtorrentAdd(magnetUri, true)
79 expect(torrent.files).to.be.an('array')
80 expect(torrent.files.length).to.equal(1)
81 expect(torrent.files[0].path).match(/\.webm$/)
82 }
83 })
84
85 it('Should transcode video on server 2', async function () {
86 this.timeout(120000)
87
88 const videoAttributes = {
89 name: 'my super name for server 2',
90 description: 'my super description for server 2',
91 fixture: 'video_short.webm'
92 }
93 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
94
95 await waitJobs(servers)
96
97 for (const server of servers) {
98 const res = await getVideosList(server.url)
99
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
103
104 expect(videoDetails.files).to.have.lengthOf(4)
105
106 const magnetUri = videoDetails.files[0].magnetUri
107 expect(magnetUri).to.match(/\.mp4/)
108
109 const torrent = await webtorrentAdd(magnetUri, true)
110 expect(torrent.files).to.be.an('array')
111 expect(torrent.files.length).to.equal(1)
112 expect(torrent.files[0].path).match(/\.mp4$/)
113 }
114 })
115
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
127 for (const server of servers) {
128 const res = await getVideosList(server.url)
129
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
133
134 expect(videoDetails.files).to.have.lengthOf(4)
135
136 const path = join(root(), 'test' + servers[1].internalServerNumber, 'videos', video.uuid + '-240.mp4')
137 const probe = await audio.get(path)
138
139 if (probe.audioStream) {
140 expect(probe.audioStream['codec_name']).to.be.equal('aac')
141 expect(probe.audioStream['bit_rate']).to.be.at.most(384 * 8000)
142 } else {
143 this.fail('Could not retrieve the audio stream on ' + probe.absolutePath)
144 }
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
159 for (const server of servers) {
160 const res = await getVideosList(server.url)
161
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
165
166 expect(videoDetails.files).to.have.lengthOf(4)
167 const path = join(root(), 'test' + servers[1].internalServerNumber, 'videos', video.uuid + '-240.mp4')
168 const probe = await audio.get(path)
169 expect(probe).to.not.have.property('audioStream')
170 }
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
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)
193 const fixtureVideoProbe = await audio.get(fixturePath)
194 const path = join(root(), 'test' + servers[1].internalServerNumber, 'videos', video.uuid + '-240.mp4')
195 const videoProbe = await audio.get(path)
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 }
202 }
203 })
204
205 it('Should transcode a 60 FPS video', async function () {
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',
211 fixture: '60fps_720p_small.mp4'
212 }
213 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
214
215 await waitJobs(servers)
216
217 for (const server of servers) {
218 const res = await getVideosList(server.url)
219
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
223
224 expect(videoDetails.files).to.have.lengthOf(4)
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)
229
230 for (const resolution of [ '240', '360', '480' ]) {
231 const path = join(root(), 'test' + servers[1].internalServerNumber, 'videos', video.uuid + '-' + resolution + '.mp4')
232 const fps = await getVideoFileFPS(path)
233
234 expect(fps).to.be.below(31)
235 }
236
237 const path = join(root(), 'test' + servers[1].internalServerNumber, 'videos', video.uuid + '-720.mp4')
238 const fps = await getVideoFileFPS(path)
239
240 expect(fps).to.be.above(58).and.below(62)
241 }
242 })
243
244 it('Should wait for transcoding before publishing the video', async function () {
245 this.timeout(160000)
246
247 {
248 // Upload the video, but wait transcoding
249 const videoAttributes = {
250 name: 'waiting video',
251 fixture: 'video_short1.webm',
252 waitTranscoding: true
253 }
254 const resVideo = await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
255 const videoId = resVideo.body.video.uuid
256
257 // Should be in transcode state
258 const { body } = await getVideo(servers[1].url, videoId)
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)
266 const videoToFindInMine = resMyVideos.body.data.find(v => v.name === videoAttributes.name)
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)
274 const videoToFindInList = resVideos.body.data.find(v => v.name === videoAttributes.name)
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
281 await waitJobs(servers)
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
297 it('Should respect maximum bitrate values', async function () {
298 this.timeout(160000)
299
300 let tempFixturePath: string
301
302 {
303 tempFixturePath = await generateHighBitrateVideo()
304
305 const bitrate = await getVideoFileBitrate(tempFixturePath)
306 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
307 }
308
309 const videoAttributes = {
310 name: 'high bitrate video',
311 description: 'high bitrate video',
312 fixture: tempFixturePath
313 }
314
315 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
316
317 await waitJobs(servers)
318
319 for (const server of servers) {
320 const res = await getVideosList(server.url)
321
322 const video = res.body.data.find(v => v.name === videoAttributes.name)
323
324 for (const resolution of [ '240', '360', '480', '720', '1080' ]) {
325 const path = join(root(), 'test' + servers[1].internalServerNumber, 'videos', video.uuid + '-' + resolution + '.mp4')
326 const bitrate = await getVideoFileBitrate(path)
327 const fps = await getVideoFileFPS(path)
328 const resolution2 = await getVideoFileResolution(path)
329
330 expect(resolution2.videoFileResolution.toString()).to.equal(resolution)
331 expect(bitrate).to.be.below(getMaxBitrate(resolution2.videoFileResolution, fps, VIDEO_TRANSCODING_FPS))
332 }
333 }
334 })
335
336 it('Should accept and transcode additional extensions', async function () {
337 this.timeout(300000)
338
339 let tempFixturePath: string
340
341 {
342 tempFixturePath = await generateHighBitrateVideo()
343
344 const bitrate = await getVideoFileBitrate(tempFixturePath)
345 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
346 }
347
348 for (const fixture of [ 'video_short.mkv', 'video_short.avi' ]) {
349 const videoAttributes = {
350 name: fixture,
351 fixture
352 }
353
354 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
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
367 const magnetUri = videoDetails.files[0].magnetUri
368 expect(magnetUri).to.contain('.mp4')
369 }
370 }
371 })
372
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
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' }
384 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributesArg)
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
400 const magnetUri = videoDetails.files[0].magnetUri
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' }
409 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributesArg)
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
425 const magnetUri = videoDetails.files[0].magnetUri
426 expect(magnetUri).to.contain('.mp4')
427 }
428 })
429
430 it('Should downscale to the closest divisor standard framerate', async function () {
431 this.timeout(160000)
432
433 let tempFixturePath: string
434
435 {
436 tempFixturePath = await generateVideoWithFramerate(59)
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)
456
457 {
458 const path = join(root(), 'test' + servers[1].internalServerNumber, 'videos', video.uuid + '-240.mp4')
459 const fps = await getVideoFileFPS(path)
460 expect(fps).to.be.equal(25)
461 }
462
463 {
464 const path = join(root(), 'test' + servers[1].internalServerNumber, 'videos', video.uuid + '-720.mp4')
465 const fps = await getVideoFileFPS(path)
466 expect(fps).to.be.equal(59)
467 }
468 }
469 })
470
471 it('Should provide valid ffprobe data', async function () {
472 this.timeout(160000)
473
474 const videoUUID = (await uploadVideoAndGetId({ server: servers[1], videoName: 'ffprobe data' })).uuid
475 await waitJobs(servers)
476
477 {
478 const path = join(root(), 'test' + servers[1].internalServerNumber, 'videos', videoUUID + '-240.mp4')
479 const metadata = await getMetadataFromFile<VideoFileMetadata>(path)
480
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 }
490
491 // expected stream properties
492 for (const p of [
493 'codec_long_name',
494 'profile',
495 'width',
496 'height',
497 'display_aspect_ratio',
498 'avg_frame_rate',
499 'pix_fmt'
500 ]) {
501 expect(metadata.streams[0]).to.have.nested.property(p)
502 }
503
504 expect(metadata).to.not.have.nested.property('format.filename')
505 }
506
507 for (const server of servers) {
508 const res2 = await getVideo(server.url, videoUUID)
509 const videoDetails: VideoDetails = res2.body
510
511 const videoFiles = videoDetails.files
512 .concat(videoDetails.streamingPlaylists[0].files)
513 expect(videoFiles).to.have.lengthOf(8)
514
515 for (const file of videoFiles) {
516 expect(file.metadata).to.be.undefined
517 expect(file.metadataUrl).to.exist
518 expect(file.metadataUrl).to.contain(servers[1].url)
519 expect(file.metadataUrl).to.contain(videoUUID)
520
521 const res3 = await getVideoFileMetadataUrl(file.metadataUrl)
522 const metadata: FfprobeData = res3.body
523 expect(metadata).to.have.nested.property('format.size')
524 }
525 }
526 })
527
528 after(async function () {
529 await cleanupTests(servers)
530 })
531 })