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 { VIDEO_TRANSCODING_FPS } from '../../../../server/initializers/constants'
9 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
11 buildAbsoluteFixturePath,
15 flushAndRunMultipleServers,
16 generateHighBitrateVideo,
17 generateVideoWithFramerate,
21 getVideoFileMetadataUrl,
25 setAccessTokensToServers,
30 } from '../../../../shared/extra-utils'
31 import { getMaxBitrate, VideoDetails, VideoResolution, VideoState } from '../../../../shared/models/videos'
38 getVideoFileResolution
39 } from '../../../helpers/ffprobe-utils'
41 const expect = chai.expect
43 function updateConfigForTranscoding (server: ServerInfo) {
44 return server.configCommand.updateCustomSubConfig({
48 allowAdditionalExtensions: true,
49 allowAudioFiles: true,
50 hls: { enabled: true },
51 webtorrent: { enabled: true },
67 describe('Test video transcoding', function () {
68 let servers: ServerInfo[] = []
71 before(async function () {
75 servers = await flushAndRunMultipleServers(2)
77 await setAccessTokensToServers(servers)
79 await doubleFollow(servers[0], servers[1])
81 await updateConfigForTranscoding(servers[1])
84 describe('Basic transcoding (or not)', function () {
86 it('Should not transcode video on server 1', async function () {
89 const videoAttributes = {
90 name: 'my super name for server 1',
91 description: 'my super description for server 1',
92 fixture: 'video_short.webm'
94 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
96 await waitJobs(servers)
98 for (const server of servers) {
99 const res = await getVideosList(server.url)
100 const video = res.body.data[0]
102 const res2 = await getVideo(server.url, video.id)
103 const videoDetails = res2.body
104 expect(videoDetails.files).to.have.lengthOf(1)
106 const magnetUri = videoDetails.files[0].magnetUri
107 expect(magnetUri).to.match(/\.webm/)
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(/\.webm$/)
116 it('Should transcode video on server 2', async function () {
117 this.timeout(120_000)
119 const videoAttributes = {
120 name: 'my super name for server 2',
121 description: 'my super description for server 2',
122 fixture: 'video_short.webm'
124 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
126 await waitJobs(servers)
128 for (const server of servers) {
129 const res = await getVideosList(server.url)
131 const video = res.body.data.find(v => v.name === videoAttributes.name)
132 const res2 = await getVideo(server.url, video.id)
133 const videoDetails = res2.body
135 expect(videoDetails.files).to.have.lengthOf(4)
137 const magnetUri = videoDetails.files[0].magnetUri
138 expect(magnetUri).to.match(/\.mp4/)
140 const torrent = await webtorrentAdd(magnetUri, true)
141 expect(torrent.files).to.be.an('array')
142 expect(torrent.files.length).to.equal(1)
143 expect(torrent.files[0].path).match(/\.mp4$/)
147 it('Should wait for transcoding before publishing the video', async function () {
148 this.timeout(160_000)
151 // Upload the video, but wait transcoding
152 const videoAttributes = {
153 name: 'waiting video',
154 fixture: 'video_short1.webm',
155 waitTranscoding: true
157 const resVideo = await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
158 const videoId = resVideo.body.video.uuid
160 // Should be in transcode state
161 const { body } = await getVideo(servers[1].url, videoId)
162 expect(body.name).to.equal('waiting video')
163 expect(body.state.id).to.equal(VideoState.TO_TRANSCODE)
164 expect(body.state.label).to.equal('To transcode')
165 expect(body.waitTranscoding).to.be.true
167 // Should have my video
168 const resMyVideos = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 10)
169 const videoToFindInMine = resMyVideos.body.data.find(v => v.name === videoAttributes.name)
170 expect(videoToFindInMine).not.to.be.undefined
171 expect(videoToFindInMine.state.id).to.equal(VideoState.TO_TRANSCODE)
172 expect(videoToFindInMine.state.label).to.equal('To transcode')
173 expect(videoToFindInMine.waitTranscoding).to.be.true
175 // Should not list this video
176 const resVideos = await getVideosList(servers[1].url)
177 const videoToFindInList = resVideos.body.data.find(v => v.name === videoAttributes.name)
178 expect(videoToFindInList).to.be.undefined
180 // Server 1 should not have the video yet
181 await getVideo(servers[0].url, videoId, HttpStatusCode.NOT_FOUND_404)
184 await waitJobs(servers)
186 for (const server of servers) {
187 const res = await getVideosList(server.url)
188 const videoToFind = res.body.data.find(v => v.name === 'waiting video')
189 expect(videoToFind).not.to.be.undefined
191 const res2 = await getVideo(server.url, videoToFind.id)
192 const videoDetails: VideoDetails = res2.body
194 expect(videoDetails.state.id).to.equal(VideoState.PUBLISHED)
195 expect(videoDetails.state.label).to.equal('Published')
196 expect(videoDetails.waitTranscoding).to.be.true
200 it('Should accept and transcode additional extensions', async function () {
201 this.timeout(300_000)
203 let tempFixturePath: string
206 tempFixturePath = await generateHighBitrateVideo()
208 const bitrate = await getVideoFileBitrate(tempFixturePath)
209 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
212 for (const fixture of [ 'video_short.mkv', 'video_short.avi' ]) {
213 const videoAttributes = {
218 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
220 await waitJobs(servers)
222 for (const server of servers) {
223 const res = await getVideosList(server.url)
225 const video = res.body.data.find(v => v.name === videoAttributes.name)
226 const res2 = await getVideo(server.url, video.id)
227 const videoDetails = res2.body
229 expect(videoDetails.files).to.have.lengthOf(4)
231 const magnetUri = videoDetails.files[0].magnetUri
232 expect(magnetUri).to.contain('.mp4')
237 it('Should transcode a 4k video', async function () {
238 this.timeout(200_000)
240 const videoAttributes = {
242 fixture: 'video_short_4k.mp4'
245 const resUpload = await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
246 video4k = resUpload.body.video.uuid
248 await waitJobs(servers)
250 const resolutions = [ 240, 360, 480, 720, 1080, 1440, 2160 ]
252 for (const server of servers) {
253 const res = await getVideo(server.url, video4k)
254 const videoDetails: VideoDetails = res.body
256 expect(videoDetails.files).to.have.lengthOf(resolutions.length)
258 for (const r of resolutions) {
259 expect(videoDetails.files.find(f => f.resolution.id === r)).to.not.be.undefined
260 expect(videoDetails.streamingPlaylists[0].files.find(f => f.resolution.id === r)).to.not.be.undefined
266 describe('Audio transcoding', function () {
268 it('Should transcode high bit rate mp3 to proper bit rate', async function () {
271 const videoAttributes = {
273 fixture: 'video_short_mp3_256k.mp4'
275 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
277 await waitJobs(servers)
279 for (const server of servers) {
280 const res = await getVideosList(server.url)
282 const video = res.body.data.find(v => v.name === videoAttributes.name)
283 const res2 = await getVideo(server.url, video.id)
284 const videoDetails: VideoDetails = res2.body
286 expect(videoDetails.files).to.have.lengthOf(4)
288 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-240.mp4'))
289 const probe = await getAudioStream(path)
291 if (probe.audioStream) {
292 expect(probe.audioStream['codec_name']).to.be.equal('aac')
293 expect(probe.audioStream['bit_rate']).to.be.at.most(384 * 8000)
295 this.fail('Could not retrieve the audio stream on ' + probe.absolutePath)
300 it('Should transcode video with no audio and have no audio itself', async function () {
303 const videoAttributes = {
305 fixture: 'video_short_no_audio.mp4'
307 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
309 await waitJobs(servers)
311 for (const server of servers) {
312 const res = await getVideosList(server.url)
314 const video = res.body.data.find(v => v.name === videoAttributes.name)
315 const res2 = await getVideo(server.url, video.id)
316 const videoDetails: VideoDetails = res2.body
318 expect(videoDetails.files).to.have.lengthOf(4)
319 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-240.mp4'))
320 const probe = await getAudioStream(path)
321 expect(probe).to.not.have.property('audioStream')
325 it('Should leave the audio untouched, but properly transcode the video', async function () {
328 const videoAttributes = {
329 name: 'untouched_audio',
330 fixture: 'video_short.mp4'
332 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
334 await waitJobs(servers)
336 for (const server of servers) {
337 const res = await getVideosList(server.url)
339 const video = res.body.data.find(v => v.name === videoAttributes.name)
340 const res2 = await getVideo(server.url, video.id)
341 const videoDetails: VideoDetails = res2.body
343 expect(videoDetails.files).to.have.lengthOf(4)
345 const fixturePath = buildAbsoluteFixturePath(videoAttributes.fixture)
346 const fixtureVideoProbe = await getAudioStream(fixturePath)
347 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-240.mp4'))
349 const videoProbe = await getAudioStream(path)
351 if (videoProbe.audioStream && fixtureVideoProbe.audioStream) {
352 const toOmit = [ 'max_bit_rate', 'duration', 'duration_ts', 'nb_frames', 'start_time', 'start_pts' ]
353 expect(omit(videoProbe.audioStream, toOmit)).to.be.deep.equal(omit(fixtureVideoProbe.audioStream, toOmit))
355 this.fail('Could not retrieve the audio stream on ' + videoProbe.absolutePath)
361 describe('Audio upload', function () {
363 function runSuite (mode: 'legacy' | 'resumable') {
365 before(async function () {
366 await servers[1].configCommand.updateCustomSubConfig({
369 hls: { enabled: true },
370 webtorrent: { enabled: true },
386 it('Should merge an audio file with the preview file', async function () {
389 const videoAttributesArg = { name: 'audio_with_preview', previewfile: 'preview.jpg', fixture: 'sample.ogg' }
390 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributesArg, HttpStatusCode.OK_200, mode)
392 await waitJobs(servers)
394 for (const server of servers) {
395 const res = await getVideosList(server.url)
397 const video = res.body.data.find(v => v.name === 'audio_with_preview')
398 const res2 = await getVideo(server.url, video.id)
399 const videoDetails: VideoDetails = res2.body
401 expect(videoDetails.files).to.have.lengthOf(1)
403 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, statusCodeExpected: HttpStatusCode.OK_200 })
404 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, statusCodeExpected: HttpStatusCode.OK_200 })
406 const magnetUri = videoDetails.files[0].magnetUri
407 expect(magnetUri).to.contain('.mp4')
411 it('Should upload an audio file and choose a default background image', async function () {
414 const videoAttributesArg = { name: 'audio_without_preview', fixture: 'sample.ogg' }
415 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributesArg, HttpStatusCode.OK_200, mode)
417 await waitJobs(servers)
419 for (const server of servers) {
420 const res = await getVideosList(server.url)
422 const video = res.body.data.find(v => v.name === 'audio_without_preview')
423 const res2 = await getVideo(server.url, video.id)
424 const videoDetails = res2.body
426 expect(videoDetails.files).to.have.lengthOf(1)
428 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, statusCodeExpected: HttpStatusCode.OK_200 })
429 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, statusCodeExpected: HttpStatusCode.OK_200 })
431 const magnetUri = videoDetails.files[0].magnetUri
432 expect(magnetUri).to.contain('.mp4')
436 it('Should upload an audio file and create an audio version only', async function () {
439 await servers[1].configCommand.updateCustomSubConfig({
442 hls: { enabled: true },
443 webtorrent: { enabled: true },
453 const videoAttributesArg = { name: 'audio_with_preview', previewfile: 'preview.jpg', fixture: 'sample.ogg' }
454 const resVideo = await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributesArg, HttpStatusCode.OK_200, mode)
456 await waitJobs(servers)
458 for (const server of servers) {
459 const res2 = await getVideo(server.url, resVideo.body.video.id)
460 const videoDetails: VideoDetails = res2.body
462 for (const files of [ videoDetails.files, videoDetails.streamingPlaylists[0].files ]) {
463 expect(files).to.have.lengthOf(2)
464 expect(files.find(f => f.resolution.id === 0)).to.not.be.undefined
468 await updateConfigForTranscoding(servers[1])
472 describe('Legacy upload', function () {
476 describe('Resumable upload', function () {
477 runSuite('resumable')
481 describe('Framerate', function () {
483 it('Should transcode a 60 FPS video', async function () {
486 const videoAttributes = {
487 name: 'my super 30fps name for server 2',
488 description: 'my super 30fps description for server 2',
489 fixture: '60fps_720p_small.mp4'
491 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
493 await waitJobs(servers)
495 for (const server of servers) {
496 const res = await getVideosList(server.url)
498 const video = res.body.data.find(v => v.name === videoAttributes.name)
499 const res2 = await getVideo(server.url, video.id)
500 const videoDetails: VideoDetails = res2.body
502 expect(videoDetails.files).to.have.lengthOf(4)
503 expect(videoDetails.files[0].fps).to.be.above(58).and.below(62)
504 expect(videoDetails.files[1].fps).to.be.below(31)
505 expect(videoDetails.files[2].fps).to.be.below(31)
506 expect(videoDetails.files[3].fps).to.be.below(31)
508 for (const resolution of [ '240', '360', '480' ]) {
509 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-' + resolution + '.mp4'))
510 const fps = await getVideoFileFPS(path)
512 expect(fps).to.be.below(31)
515 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-720.mp4'))
516 const fps = await getVideoFileFPS(path)
518 expect(fps).to.be.above(58).and.below(62)
522 it('Should downscale to the closest divisor standard framerate', async function () {
523 this.timeout(200_000)
525 let tempFixturePath: string
528 tempFixturePath = await generateVideoWithFramerate(59)
530 const fps = await getVideoFileFPS(tempFixturePath)
531 expect(fps).to.be.equal(59)
534 const videoAttributes = {
536 description: '59fps video',
537 fixture: tempFixturePath
540 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
542 await waitJobs(servers)
544 for (const server of servers) {
545 const res = await getVideosList(server.url)
547 const video = res.body.data.find(v => v.name === videoAttributes.name)
550 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-240.mp4'))
551 const fps = await getVideoFileFPS(path)
552 expect(fps).to.be.equal(25)
556 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-720.mp4'))
557 const fps = await getVideoFileFPS(path)
558 expect(fps).to.be.equal(59)
564 describe('Bitrate control', function () {
565 it('Should respect maximum bitrate values', async function () {
566 this.timeout(160_000)
568 let tempFixturePath: string
571 tempFixturePath = await generateHighBitrateVideo()
573 const bitrate = await getVideoFileBitrate(tempFixturePath)
574 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
577 const videoAttributes = {
578 name: 'high bitrate video',
579 description: 'high bitrate video',
580 fixture: tempFixturePath
583 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
585 await waitJobs(servers)
587 for (const server of servers) {
588 const res = await getVideosList(server.url)
590 const video = res.body.data.find(v => v.name === videoAttributes.name)
592 for (const resolution of [ '240', '360', '480', '720', '1080' ]) {
593 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-' + resolution + '.mp4'))
595 const bitrate = await getVideoFileBitrate(path)
596 const fps = await getVideoFileFPS(path)
597 const resolution2 = await getVideoFileResolution(path)
599 expect(resolution2.videoFileResolution.toString()).to.equal(resolution)
600 expect(bitrate).to.be.below(getMaxBitrate(resolution2.videoFileResolution, fps, VIDEO_TRANSCODING_FPS))
605 it('Should not transcode to an higher bitrate than the original file', async function () {
606 this.timeout(160_000)
620 webtorrent: { enabled: true },
621 hls: { enabled: true }
624 await servers[1].configCommand.updateCustomSubConfig({ newConfig })
626 const videoAttributes = {
628 fixture: 'low-bitrate.mp4'
631 const resUpload = await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
632 const videoUUID = resUpload.body.video.uuid
634 await waitJobs(servers)
636 const resolutions = [ 240, 360, 480, 720, 1080 ]
637 for (const r of resolutions) {
638 const path = `videos/${videoUUID}-${r}.mp4`
639 const size = await getServerFileSize(servers[1], path)
640 expect(size, `${path} not below ${60_000}`).to.be.below(60_000)
645 describe('FFprobe', function () {
647 it('Should provide valid ffprobe data', async function () {
648 this.timeout(160_000)
650 const videoUUID = (await uploadVideoAndGetId({ server: servers[1], videoName: 'ffprobe data' })).uuid
651 await waitJobs(servers)
654 const path = buildServerDirectory(servers[1], join('videos', videoUUID + '-240.mp4'))
655 const metadata = await getMetadataFromFile(path)
657 // expected format properties
664 expect(metadata.format).to.have.nested.property(p)
667 // expected stream properties
673 'display_aspect_ratio',
677 expect(metadata.streams[0]).to.have.nested.property(p)
680 expect(metadata).to.not.have.nested.property('format.filename')
683 for (const server of servers) {
684 const res2 = await getVideo(server.url, videoUUID)
685 const videoDetails: VideoDetails = res2.body
687 const videoFiles = videoDetails.files
688 .concat(videoDetails.streamingPlaylists[0].files)
689 expect(videoFiles).to.have.lengthOf(8)
691 for (const file of videoFiles) {
692 expect(file.metadata).to.be.undefined
693 expect(file.metadataUrl).to.exist
694 expect(file.metadataUrl).to.contain(servers[1].url)
695 expect(file.metadataUrl).to.contain(videoUUID)
697 const res3 = await getVideoFileMetadataUrl(file.metadataUrl)
698 const metadata: FfprobeData = res3.body
699 expect(metadata).to.have.nested.property('format.size')
704 it('Should correctly detect if quick transcode is possible', async function () {
707 expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.mp4'))).to.be.true
708 expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.webm'))).to.be.false
712 describe('Transcoding job queue', function () {
714 it('Should have the appropriate priorities for transcoding jobs', async function () {
715 const body = await servers[1].jobsCommand.getJobsList({
719 jobType: 'video-transcoding'
722 const jobs = body.data
723 const transcodingJobs = jobs.filter(j => j.data.videoUUID === video4k)
725 expect(transcodingJobs).to.have.lengthOf(14)
727 const hlsJobs = transcodingJobs.filter(j => j.data.type === 'new-resolution-to-hls')
728 const webtorrentJobs = transcodingJobs.filter(j => j.data.type === 'new-resolution-to-webtorrent')
729 const optimizeJobs = transcodingJobs.filter(j => j.data.type === 'optimize-to-webtorrent')
731 expect(hlsJobs).to.have.lengthOf(7)
732 expect(webtorrentJobs).to.have.lengthOf(6)
733 expect(optimizeJobs).to.have.lengthOf(1)
735 for (const j of optimizeJobs.concat(hlsJobs.concat(webtorrentJobs))) {
736 expect(j.priority).to.be.greaterThan(100)
737 expect(j.priority).to.be.lessThan(150)
742 after(async function () {
743 await cleanupTests(servers)