1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { FfprobeData } from 'fluent-ffmpeg'
6 import { omit } from 'lodash'
7 import { join } from 'path'
8 import { Job } from '@shared/models'
9 import { VIDEO_TRANSCODING_FPS } from '../../../../server/initializers/constants'
10 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
12 buildAbsoluteFixturePath,
16 flushAndRunMultipleServers,
17 generateHighBitrateVideo,
18 generateVideoWithFramerate,
19 getJobsListPaginationAndSort,
23 getVideoFileMetadataUrl,
27 setAccessTokensToServers,
28 updateCustomSubConfig,
33 } from '../../../../shared/extra-utils'
34 import { getMaxBitrate, VideoDetails, VideoResolution, VideoState } from '../../../../shared/models/videos'
41 getVideoFileResolution
42 } from '../../../helpers/ffprobe-utils'
44 const expect = chai.expect
46 describe('Test video transcoding', function () {
47 let servers: ServerInfo[] = []
50 before(async function () {
54 servers = await flushAndRunMultipleServers(2)
56 await setAccessTokensToServers(servers)
58 await doubleFollow(servers[0], servers[1])
61 it('Should not transcode video on server 1', async function () {
64 const videoAttributes = {
65 name: 'my super name for server 1',
66 description: 'my super description for server 1',
67 fixture: 'video_short.webm'
69 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
71 await waitJobs(servers)
73 for (const server of servers) {
74 const res = await getVideosList(server.url)
75 const video = res.body.data[0]
77 const res2 = await getVideo(server.url, video.id)
78 const videoDetails = res2.body
79 expect(videoDetails.files).to.have.lengthOf(1)
81 const magnetUri = videoDetails.files[0].magnetUri
82 expect(magnetUri).to.match(/\.webm/)
84 const torrent = await webtorrentAdd(magnetUri, true)
85 expect(torrent.files).to.be.an('array')
86 expect(torrent.files.length).to.equal(1)
87 expect(torrent.files[0].path).match(/\.webm$/)
91 it('Should transcode video on server 2', async function () {
94 const videoAttributes = {
95 name: 'my super name for server 2',
96 description: 'my super description for server 2',
97 fixture: 'video_short.webm'
99 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
101 await waitJobs(servers)
103 for (const server of servers) {
104 const res = await getVideosList(server.url)
106 const video = res.body.data.find(v => v.name === videoAttributes.name)
107 const res2 = await getVideo(server.url, video.id)
108 const videoDetails = res2.body
110 expect(videoDetails.files).to.have.lengthOf(4)
112 const magnetUri = videoDetails.files[0].magnetUri
113 expect(magnetUri).to.match(/\.mp4/)
115 const torrent = await webtorrentAdd(magnetUri, true)
116 expect(torrent.files).to.be.an('array')
117 expect(torrent.files.length).to.equal(1)
118 expect(torrent.files[0].path).match(/\.mp4$/)
122 it('Should transcode high bit rate mp3 to proper bit rate', async function () {
125 const videoAttributes = {
127 fixture: 'video_short_mp3_256k.mp4'
129 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
131 await waitJobs(servers)
133 for (const server of servers) {
134 const res = await getVideosList(server.url)
136 const video = res.body.data.find(v => v.name === videoAttributes.name)
137 const res2 = await getVideo(server.url, video.id)
138 const videoDetails: VideoDetails = res2.body
140 expect(videoDetails.files).to.have.lengthOf(4)
142 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-240.mp4'))
143 const probe = await getAudioStream(path)
145 if (probe.audioStream) {
146 expect(probe.audioStream['codec_name']).to.be.equal('aac')
147 expect(probe.audioStream['bit_rate']).to.be.at.most(384 * 8000)
149 this.fail('Could not retrieve the audio stream on ' + probe.absolutePath)
154 it('Should transcode video with no audio and have no audio itself', async function () {
157 const videoAttributes = {
159 fixture: 'video_short_no_audio.mp4'
161 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
163 await waitJobs(servers)
165 for (const server of servers) {
166 const res = await getVideosList(server.url)
168 const video = res.body.data.find(v => v.name === videoAttributes.name)
169 const res2 = await getVideo(server.url, video.id)
170 const videoDetails: VideoDetails = res2.body
172 expect(videoDetails.files).to.have.lengthOf(4)
173 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-240.mp4'))
174 const probe = await getAudioStream(path)
175 expect(probe).to.not.have.property('audioStream')
179 it('Should leave the audio untouched, but properly transcode the video', async function () {
182 const videoAttributes = {
183 name: 'untouched_audio',
184 fixture: 'video_short.mp4'
186 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
188 await waitJobs(servers)
190 for (const server of servers) {
191 const res = await getVideosList(server.url)
193 const video = res.body.data.find(v => v.name === videoAttributes.name)
194 const res2 = await getVideo(server.url, video.id)
195 const videoDetails: VideoDetails = res2.body
197 expect(videoDetails.files).to.have.lengthOf(4)
199 const fixturePath = buildAbsoluteFixturePath(videoAttributes.fixture)
200 const fixtureVideoProbe = await getAudioStream(fixturePath)
201 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-240.mp4'))
203 const videoProbe = await getAudioStream(path)
205 if (videoProbe.audioStream && fixtureVideoProbe.audioStream) {
206 const toOmit = [ 'max_bit_rate', 'duration', 'duration_ts', 'nb_frames', 'start_time', 'start_pts' ]
207 expect(omit(videoProbe.audioStream, toOmit)).to.be.deep.equal(omit(fixtureVideoProbe.audioStream, toOmit))
209 this.fail('Could not retrieve the audio stream on ' + videoProbe.absolutePath)
214 it('Should transcode a 60 FPS video', async function () {
217 const videoAttributes = {
218 name: 'my super 30fps name for server 2',
219 description: 'my super 30fps description for server 2',
220 fixture: '60fps_720p_small.mp4'
222 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
224 await waitJobs(servers)
226 for (const server of servers) {
227 const res = await getVideosList(server.url)
229 const video = res.body.data.find(v => v.name === videoAttributes.name)
230 const res2 = await getVideo(server.url, video.id)
231 const videoDetails: VideoDetails = res2.body
233 expect(videoDetails.files).to.have.lengthOf(4)
234 expect(videoDetails.files[0].fps).to.be.above(58).and.below(62)
235 expect(videoDetails.files[1].fps).to.be.below(31)
236 expect(videoDetails.files[2].fps).to.be.below(31)
237 expect(videoDetails.files[3].fps).to.be.below(31)
239 for (const resolution of [ '240', '360', '480' ]) {
240 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-' + resolution + '.mp4'))
241 const fps = await getVideoFileFPS(path)
243 expect(fps).to.be.below(31)
246 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-720.mp4'))
247 const fps = await getVideoFileFPS(path)
249 expect(fps).to.be.above(58).and.below(62)
253 it('Should wait for transcoding before publishing the video', async function () {
254 this.timeout(160_000)
257 // Upload the video, but wait transcoding
258 const videoAttributes = {
259 name: 'waiting video',
260 fixture: 'video_short1.webm',
261 waitTranscoding: true
263 const resVideo = await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
264 const videoId = resVideo.body.video.uuid
266 // Should be in transcode state
267 const { body } = await getVideo(servers[1].url, videoId)
268 expect(body.name).to.equal('waiting video')
269 expect(body.state.id).to.equal(VideoState.TO_TRANSCODE)
270 expect(body.state.label).to.equal('To transcode')
271 expect(body.waitTranscoding).to.be.true
273 // Should have my video
274 const resMyVideos = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 10)
275 const videoToFindInMine = resMyVideos.body.data.find(v => v.name === videoAttributes.name)
276 expect(videoToFindInMine).not.to.be.undefined
277 expect(videoToFindInMine.state.id).to.equal(VideoState.TO_TRANSCODE)
278 expect(videoToFindInMine.state.label).to.equal('To transcode')
279 expect(videoToFindInMine.waitTranscoding).to.be.true
281 // Should not list this video
282 const resVideos = await getVideosList(servers[1].url)
283 const videoToFindInList = resVideos.body.data.find(v => v.name === videoAttributes.name)
284 expect(videoToFindInList).to.be.undefined
286 // Server 1 should not have the video yet
287 await getVideo(servers[0].url, videoId, HttpStatusCode.NOT_FOUND_404)
290 await waitJobs(servers)
292 for (const server of servers) {
293 const res = await getVideosList(server.url)
294 const videoToFind = res.body.data.find(v => v.name === 'waiting video')
295 expect(videoToFind).not.to.be.undefined
297 const res2 = await getVideo(server.url, videoToFind.id)
298 const videoDetails: VideoDetails = res2.body
300 expect(videoDetails.state.id).to.equal(VideoState.PUBLISHED)
301 expect(videoDetails.state.label).to.equal('Published')
302 expect(videoDetails.waitTranscoding).to.be.true
306 it('Should respect maximum bitrate values', async function () {
307 this.timeout(160_000)
309 let tempFixturePath: string
312 tempFixturePath = await generateHighBitrateVideo()
314 const bitrate = await getVideoFileBitrate(tempFixturePath)
315 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
318 const videoAttributes = {
319 name: 'high bitrate video',
320 description: 'high bitrate video',
321 fixture: tempFixturePath
324 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
326 await waitJobs(servers)
328 for (const server of servers) {
329 const res = await getVideosList(server.url)
331 const video = res.body.data.find(v => v.name === videoAttributes.name)
333 for (const resolution of [ '240', '360', '480', '720', '1080' ]) {
334 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-' + resolution + '.mp4'))
336 const bitrate = await getVideoFileBitrate(path)
337 const fps = await getVideoFileFPS(path)
338 const resolution2 = await getVideoFileResolution(path)
340 expect(resolution2.videoFileResolution.toString()).to.equal(resolution)
341 expect(bitrate).to.be.below(getMaxBitrate(resolution2.videoFileResolution, fps, VIDEO_TRANSCODING_FPS))
346 it('Should accept and transcode additional extensions', async function () {
347 this.timeout(300_000)
349 let tempFixturePath: string
352 tempFixturePath = await generateHighBitrateVideo()
354 const bitrate = await getVideoFileBitrate(tempFixturePath)
355 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
358 for (const fixture of [ 'video_short.mkv', 'video_short.avi' ]) {
359 const videoAttributes = {
364 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
366 await waitJobs(servers)
368 for (const server of servers) {
369 const res = await getVideosList(server.url)
371 const video = res.body.data.find(v => v.name === videoAttributes.name)
372 const res2 = await getVideo(server.url, video.id)
373 const videoDetails = res2.body
375 expect(videoDetails.files).to.have.lengthOf(4)
377 const magnetUri = videoDetails.files[0].magnetUri
378 expect(magnetUri).to.contain('.mp4')
383 it('Should correctly detect if quick transcode is possible', async function () {
386 expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.mp4'))).to.be.true
387 expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.webm'))).to.be.false
390 it('Should merge an audio file with the preview file', async function () {
393 const videoAttributesArg = { name: 'audio_with_preview', previewfile: 'preview.jpg', fixture: 'sample.ogg' }
394 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributesArg)
396 await waitJobs(servers)
398 for (const server of servers) {
399 const res = await getVideosList(server.url)
401 const video = res.body.data.find(v => v.name === 'audio_with_preview')
402 const res2 = await getVideo(server.url, video.id)
403 const videoDetails: VideoDetails = res2.body
405 expect(videoDetails.files).to.have.lengthOf(1)
407 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, statusCodeExpected: HttpStatusCode.OK_200 })
408 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, statusCodeExpected: HttpStatusCode.OK_200 })
410 const magnetUri = videoDetails.files[0].magnetUri
411 expect(magnetUri).to.contain('.mp4')
415 it('Should upload an audio file and choose a default background image', async function () {
418 const videoAttributesArg = { name: 'audio_without_preview', fixture: 'sample.ogg' }
419 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributesArg)
421 await waitJobs(servers)
423 for (const server of servers) {
424 const res = await getVideosList(server.url)
426 const video = res.body.data.find(v => v.name === 'audio_without_preview')
427 const res2 = await getVideo(server.url, video.id)
428 const videoDetails = res2.body
430 expect(videoDetails.files).to.have.lengthOf(1)
432 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, statusCodeExpected: HttpStatusCode.OK_200 })
433 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, statusCodeExpected: HttpStatusCode.OK_200 })
435 const magnetUri = videoDetails.files[0].magnetUri
436 expect(magnetUri).to.contain('.mp4')
440 it('Should downscale to the closest divisor standard framerate', async function () {
441 this.timeout(200_000)
443 let tempFixturePath: string
446 tempFixturePath = await generateVideoWithFramerate(59)
448 const fps = await getVideoFileFPS(tempFixturePath)
449 expect(fps).to.be.equal(59)
452 const videoAttributes = {
454 description: '59fps video',
455 fixture: tempFixturePath
458 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
460 await waitJobs(servers)
462 for (const server of servers) {
463 const res = await getVideosList(server.url)
465 const video = res.body.data.find(v => v.name === videoAttributes.name)
468 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-240.mp4'))
469 const fps = await getVideoFileFPS(path)
470 expect(fps).to.be.equal(25)
474 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-720.mp4'))
475 const fps = await getVideoFileFPS(path)
476 expect(fps).to.be.equal(59)
481 it('Should not transcode to an higher bitrate than the original file', async function () {
482 this.timeout(160_000)
496 webtorrent: { enabled: true },
497 hls: { enabled: true }
500 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, config)
502 const videoAttributes = {
504 fixture: 'low-bitrate.mp4'
507 const resUpload = await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
508 const videoUUID = resUpload.body.video.uuid
510 await waitJobs(servers)
512 const resolutions = [ 240, 360, 480, 720, 1080 ]
513 for (const r of resolutions) {
514 expect(await getServerFileSize(servers[1], `videos/${videoUUID}-${r}.mp4`)).to.be.below(60_000)
518 it('Should provide valid ffprobe data', async function () {
519 this.timeout(160_000)
521 const videoUUID = (await uploadVideoAndGetId({ server: servers[1], videoName: 'ffprobe data' })).uuid
522 await waitJobs(servers)
525 const path = buildServerDirectory(servers[1], join('videos', videoUUID + '-240.mp4'))
526 const metadata = await getMetadataFromFile(path)
528 // expected format properties
535 expect(metadata.format).to.have.nested.property(p)
538 // expected stream properties
544 'display_aspect_ratio',
548 expect(metadata.streams[0]).to.have.nested.property(p)
551 expect(metadata).to.not.have.nested.property('format.filename')
554 for (const server of servers) {
555 const res2 = await getVideo(server.url, videoUUID)
556 const videoDetails: VideoDetails = res2.body
558 const videoFiles = videoDetails.files
559 .concat(videoDetails.streamingPlaylists[0].files)
560 expect(videoFiles).to.have.lengthOf(8)
562 for (const file of videoFiles) {
563 expect(file.metadata).to.be.undefined
564 expect(file.metadataUrl).to.exist
565 expect(file.metadataUrl).to.contain(servers[1].url)
566 expect(file.metadataUrl).to.contain(videoUUID)
568 const res3 = await getVideoFileMetadataUrl(file.metadataUrl)
569 const metadata: FfprobeData = res3.body
570 expect(metadata).to.have.nested.property('format.size')
575 it('Should transcode a 4k video', async function () {
576 this.timeout(200_000)
578 const videoAttributes = {
580 fixture: 'video_short_4k.mp4'
583 const resUpload = await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
584 video4k = resUpload.body.video.uuid
586 await waitJobs(servers)
588 const resolutions = [ 240, 360, 480, 720, 1080, 1440, 2160 ]
590 for (const server of servers) {
591 const res = await getVideo(server.url, video4k)
592 const videoDetails: VideoDetails = res.body
594 expect(videoDetails.files).to.have.lengthOf(resolutions.length)
596 for (const r of resolutions) {
597 expect(videoDetails.files.find(f => f.resolution.id === r)).to.not.be.undefined
598 expect(videoDetails.streamingPlaylists[0].files.find(f => f.resolution.id === r)).to.not.be.undefined
603 it('Should have the appropriate priorities for transcoding jobs', async function () {
604 const res = await getJobsListPaginationAndSort({
606 accessToken: servers[1].accessToken,
610 jobType: 'video-transcoding'
613 const jobs = res.body.data as Job[]
615 const transcodingJobs = jobs.filter(j => j.data.videoUUID === video4k)
617 expect(transcodingJobs).to.have.lengthOf(14)
619 const hlsJobs = transcodingJobs.filter(j => j.data.type === 'new-resolution-to-hls')
620 const webtorrentJobs = transcodingJobs.filter(j => j.data.type === 'new-resolution-to-webtorrent')
621 const optimizeJobs = transcodingJobs.filter(j => j.data.type === 'optimize-to-webtorrent')
623 expect(hlsJobs).to.have.lengthOf(7)
624 expect(webtorrentJobs).to.have.lengthOf(6)
625 expect(optimizeJobs).to.have.lengthOf(1)
627 for (const j of optimizeJobs) {
628 expect(j.priority).to.be.greaterThan(11)
629 expect(j.priority).to.be.lessThan(50)
632 for (const j of hlsJobs.concat(webtorrentJobs)) {
633 expect(j.priority).to.be.greaterThan(100)
634 expect(j.priority).to.be.lessThan(150)
638 after(async function () {
639 await cleanupTests(servers)