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 function updateConfigForTranscoding (server: ServerInfo) {
47 return updateCustomSubConfig(server.url, server.accessToken, {
50 allowAdditionalExtensions: true,
51 allowAudioFiles: true,
52 hls: { enabled: true },
53 webtorrent: { enabled: true },
68 describe('Test video transcoding', function () {
69 let servers: ServerInfo[] = []
72 before(async function () {
76 servers = await flushAndRunMultipleServers(2)
78 await setAccessTokensToServers(servers)
80 await doubleFollow(servers[0], servers[1])
82 await updateConfigForTranscoding(servers[1])
85 describe('Basic transcoding (or not)', function () {
87 it('Should not transcode video on server 1', async function () {
90 const videoAttributes = {
91 name: 'my super name for server 1',
92 description: 'my super description for server 1',
93 fixture: 'video_short.webm'
95 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
97 await waitJobs(servers)
99 for (const server of servers) {
100 const res = await getVideosList(server.url)
101 const video = res.body.data[0]
103 const res2 = await getVideo(server.url, video.id)
104 const videoDetails = res2.body
105 expect(videoDetails.files).to.have.lengthOf(1)
107 const magnetUri = videoDetails.files[0].magnetUri
108 expect(magnetUri).to.match(/\.webm/)
110 const torrent = await webtorrentAdd(magnetUri, true)
111 expect(torrent.files).to.be.an('array')
112 expect(torrent.files.length).to.equal(1)
113 expect(torrent.files[0].path).match(/\.webm$/)
117 it('Should transcode video on server 2', async function () {
118 this.timeout(120_000)
120 const videoAttributes = {
121 name: 'my super name for server 2',
122 description: 'my super description for server 2',
123 fixture: 'video_short.webm'
125 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
127 await waitJobs(servers)
129 for (const server of servers) {
130 const res = await getVideosList(server.url)
132 const video = res.body.data.find(v => v.name === videoAttributes.name)
133 const res2 = await getVideo(server.url, video.id)
134 const videoDetails = res2.body
136 expect(videoDetails.files).to.have.lengthOf(4)
138 const magnetUri = videoDetails.files[0].magnetUri
139 expect(magnetUri).to.match(/\.mp4/)
141 const torrent = await webtorrentAdd(magnetUri, true)
142 expect(torrent.files).to.be.an('array')
143 expect(torrent.files.length).to.equal(1)
144 expect(torrent.files[0].path).match(/\.mp4$/)
148 it('Should wait for transcoding before publishing the video', async function () {
149 this.timeout(160_000)
152 // Upload the video, but wait transcoding
153 const videoAttributes = {
154 name: 'waiting video',
155 fixture: 'video_short1.webm',
156 waitTranscoding: true
158 const resVideo = await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
159 const videoId = resVideo.body.video.uuid
161 // Should be in transcode state
162 const { body } = await getVideo(servers[1].url, videoId)
163 expect(body.name).to.equal('waiting video')
164 expect(body.state.id).to.equal(VideoState.TO_TRANSCODE)
165 expect(body.state.label).to.equal('To transcode')
166 expect(body.waitTranscoding).to.be.true
168 // Should have my video
169 const resMyVideos = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 10)
170 const videoToFindInMine = resMyVideos.body.data.find(v => v.name === videoAttributes.name)
171 expect(videoToFindInMine).not.to.be.undefined
172 expect(videoToFindInMine.state.id).to.equal(VideoState.TO_TRANSCODE)
173 expect(videoToFindInMine.state.label).to.equal('To transcode')
174 expect(videoToFindInMine.waitTranscoding).to.be.true
176 // Should not list this video
177 const resVideos = await getVideosList(servers[1].url)
178 const videoToFindInList = resVideos.body.data.find(v => v.name === videoAttributes.name)
179 expect(videoToFindInList).to.be.undefined
181 // Server 1 should not have the video yet
182 await getVideo(servers[0].url, videoId, HttpStatusCode.NOT_FOUND_404)
185 await waitJobs(servers)
187 for (const server of servers) {
188 const res = await getVideosList(server.url)
189 const videoToFind = res.body.data.find(v => v.name === 'waiting video')
190 expect(videoToFind).not.to.be.undefined
192 const res2 = await getVideo(server.url, videoToFind.id)
193 const videoDetails: VideoDetails = res2.body
195 expect(videoDetails.state.id).to.equal(VideoState.PUBLISHED)
196 expect(videoDetails.state.label).to.equal('Published')
197 expect(videoDetails.waitTranscoding).to.be.true
201 it('Should accept and transcode additional extensions', async function () {
202 this.timeout(300_000)
204 let tempFixturePath: string
207 tempFixturePath = await generateHighBitrateVideo()
209 const bitrate = await getVideoFileBitrate(tempFixturePath)
210 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
213 for (const fixture of [ 'video_short.mkv', 'video_short.avi' ]) {
214 const videoAttributes = {
219 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
221 await waitJobs(servers)
223 for (const server of servers) {
224 const res = await getVideosList(server.url)
226 const video = res.body.data.find(v => v.name === videoAttributes.name)
227 const res2 = await getVideo(server.url, video.id)
228 const videoDetails = res2.body
230 expect(videoDetails.files).to.have.lengthOf(4)
232 const magnetUri = videoDetails.files[0].magnetUri
233 expect(magnetUri).to.contain('.mp4')
238 it('Should transcode a 4k video', async function () {
239 this.timeout(200_000)
241 const videoAttributes = {
243 fixture: 'video_short_4k.mp4'
246 const resUpload = await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
247 video4k = resUpload.body.video.uuid
249 await waitJobs(servers)
251 const resolutions = [ 240, 360, 480, 720, 1080, 1440, 2160 ]
253 for (const server of servers) {
254 const res = await getVideo(server.url, video4k)
255 const videoDetails: VideoDetails = res.body
257 expect(videoDetails.files).to.have.lengthOf(resolutions.length)
259 for (const r of resolutions) {
260 expect(videoDetails.files.find(f => f.resolution.id === r)).to.not.be.undefined
261 expect(videoDetails.streamingPlaylists[0].files.find(f => f.resolution.id === r)).to.not.be.undefined
267 describe('Audio transcoding', function () {
269 it('Should transcode high bit rate mp3 to proper bit rate', async function () {
272 const videoAttributes = {
274 fixture: 'video_short_mp3_256k.mp4'
276 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
278 await waitJobs(servers)
280 for (const server of servers) {
281 const res = await getVideosList(server.url)
283 const video = res.body.data.find(v => v.name === videoAttributes.name)
284 const res2 = await getVideo(server.url, video.id)
285 const videoDetails: VideoDetails = res2.body
287 expect(videoDetails.files).to.have.lengthOf(4)
289 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-240.mp4'))
290 const probe = await getAudioStream(path)
292 if (probe.audioStream) {
293 expect(probe.audioStream['codec_name']).to.be.equal('aac')
294 expect(probe.audioStream['bit_rate']).to.be.at.most(384 * 8000)
296 this.fail('Could not retrieve the audio stream on ' + probe.absolutePath)
301 it('Should transcode video with no audio and have no audio itself', async function () {
304 const videoAttributes = {
306 fixture: 'video_short_no_audio.mp4'
308 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
310 await waitJobs(servers)
312 for (const server of servers) {
313 const res = await getVideosList(server.url)
315 const video = res.body.data.find(v => v.name === videoAttributes.name)
316 const res2 = await getVideo(server.url, video.id)
317 const videoDetails: VideoDetails = res2.body
319 expect(videoDetails.files).to.have.lengthOf(4)
320 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-240.mp4'))
321 const probe = await getAudioStream(path)
322 expect(probe).to.not.have.property('audioStream')
326 it('Should leave the audio untouched, but properly transcode the video', async function () {
329 const videoAttributes = {
330 name: 'untouched_audio',
331 fixture: 'video_short.mp4'
333 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
335 await waitJobs(servers)
337 for (const server of servers) {
338 const res = await getVideosList(server.url)
340 const video = res.body.data.find(v => v.name === videoAttributes.name)
341 const res2 = await getVideo(server.url, video.id)
342 const videoDetails: VideoDetails = res2.body
344 expect(videoDetails.files).to.have.lengthOf(4)
346 const fixturePath = buildAbsoluteFixturePath(videoAttributes.fixture)
347 const fixtureVideoProbe = await getAudioStream(fixturePath)
348 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-240.mp4'))
350 const videoProbe = await getAudioStream(path)
352 if (videoProbe.audioStream && fixtureVideoProbe.audioStream) {
353 const toOmit = [ 'max_bit_rate', 'duration', 'duration_ts', 'nb_frames', 'start_time', 'start_pts' ]
354 expect(omit(videoProbe.audioStream, toOmit)).to.be.deep.equal(omit(fixtureVideoProbe.audioStream, toOmit))
356 this.fail('Could not retrieve the audio stream on ' + videoProbe.absolutePath)
362 describe('Audio upload', function () {
364 function runSuite (mode: 'legacy' | 'resumable') {
366 before(async function () {
367 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, {
369 hls: { enabled: true },
370 webtorrent: { enabled: true },
385 it('Should merge an audio file with the preview file', async function () {
388 const videoAttributesArg = { name: 'audio_with_preview', previewfile: 'preview.jpg', fixture: 'sample.ogg' }
389 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributesArg, HttpStatusCode.OK_200, mode)
391 await waitJobs(servers)
393 for (const server of servers) {
394 const res = await getVideosList(server.url)
396 const video = res.body.data.find(v => v.name === 'audio_with_preview')
397 const res2 = await getVideo(server.url, video.id)
398 const videoDetails: VideoDetails = res2.body
400 expect(videoDetails.files).to.have.lengthOf(1)
402 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, statusCodeExpected: HttpStatusCode.OK_200 })
403 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, statusCodeExpected: HttpStatusCode.OK_200 })
405 const magnetUri = videoDetails.files[0].magnetUri
406 expect(magnetUri).to.contain('.mp4')
410 it('Should upload an audio file and choose a default background image', async function () {
413 const videoAttributesArg = { name: 'audio_without_preview', fixture: 'sample.ogg' }
414 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributesArg, HttpStatusCode.OK_200, mode)
416 await waitJobs(servers)
418 for (const server of servers) {
419 const res = await getVideosList(server.url)
421 const video = res.body.data.find(v => v.name === 'audio_without_preview')
422 const res2 = await getVideo(server.url, video.id)
423 const videoDetails = res2.body
425 expect(videoDetails.files).to.have.lengthOf(1)
427 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, statusCodeExpected: HttpStatusCode.OK_200 })
428 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, statusCodeExpected: HttpStatusCode.OK_200 })
430 const magnetUri = videoDetails.files[0].magnetUri
431 expect(magnetUri).to.contain('.mp4')
435 it('Should upload an audio file and create an audio version only', async function () {
438 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, {
440 hls: { enabled: true },
441 webtorrent: { enabled: true },
450 const videoAttributesArg = { name: 'audio_with_preview', previewfile: 'preview.jpg', fixture: 'sample.ogg' }
451 const resVideo = await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributesArg, HttpStatusCode.OK_200, mode)
453 await waitJobs(servers)
455 for (const server of servers) {
456 const res2 = await getVideo(server.url, resVideo.body.video.id)
457 const videoDetails: VideoDetails = res2.body
459 for (const files of [ videoDetails.files, videoDetails.streamingPlaylists[0].files ]) {
460 expect(files).to.have.lengthOf(2)
461 expect(files.find(f => f.resolution.id === 0)).to.not.be.undefined
465 await updateConfigForTranscoding(servers[1])
469 describe('Legacy upload', function () {
473 describe('Resumable upload', function () {
474 runSuite('resumable')
478 describe('Framerate', function () {
480 it('Should transcode a 60 FPS video', async function () {
483 const videoAttributes = {
484 name: 'my super 30fps name for server 2',
485 description: 'my super 30fps description for server 2',
486 fixture: '60fps_720p_small.mp4'
488 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
490 await waitJobs(servers)
492 for (const server of servers) {
493 const res = await getVideosList(server.url)
495 const video = res.body.data.find(v => v.name === videoAttributes.name)
496 const res2 = await getVideo(server.url, video.id)
497 const videoDetails: VideoDetails = res2.body
499 expect(videoDetails.files).to.have.lengthOf(4)
500 expect(videoDetails.files[0].fps).to.be.above(58).and.below(62)
501 expect(videoDetails.files[1].fps).to.be.below(31)
502 expect(videoDetails.files[2].fps).to.be.below(31)
503 expect(videoDetails.files[3].fps).to.be.below(31)
505 for (const resolution of [ '240', '360', '480' ]) {
506 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-' + resolution + '.mp4'))
507 const fps = await getVideoFileFPS(path)
509 expect(fps).to.be.below(31)
512 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-720.mp4'))
513 const fps = await getVideoFileFPS(path)
515 expect(fps).to.be.above(58).and.below(62)
519 it('Should downscale to the closest divisor standard framerate', async function () {
520 this.timeout(200_000)
522 let tempFixturePath: string
525 tempFixturePath = await generateVideoWithFramerate(59)
527 const fps = await getVideoFileFPS(tempFixturePath)
528 expect(fps).to.be.equal(59)
531 const videoAttributes = {
533 description: '59fps video',
534 fixture: tempFixturePath
537 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
539 await waitJobs(servers)
541 for (const server of servers) {
542 const res = await getVideosList(server.url)
544 const video = res.body.data.find(v => v.name === videoAttributes.name)
547 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-240.mp4'))
548 const fps = await getVideoFileFPS(path)
549 expect(fps).to.be.equal(25)
553 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-720.mp4'))
554 const fps = await getVideoFileFPS(path)
555 expect(fps).to.be.equal(59)
561 describe('Bitrate control', function () {
562 it('Should respect maximum bitrate values', async function () {
563 this.timeout(160_000)
565 let tempFixturePath: string
568 tempFixturePath = await generateHighBitrateVideo()
570 const bitrate = await getVideoFileBitrate(tempFixturePath)
571 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
574 const videoAttributes = {
575 name: 'high bitrate video',
576 description: 'high bitrate video',
577 fixture: tempFixturePath
580 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
582 await waitJobs(servers)
584 for (const server of servers) {
585 const res = await getVideosList(server.url)
587 const video = res.body.data.find(v => v.name === videoAttributes.name)
589 for (const resolution of [ '240', '360', '480', '720', '1080' ]) {
590 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-' + resolution + '.mp4'))
592 const bitrate = await getVideoFileBitrate(path)
593 const fps = await getVideoFileFPS(path)
594 const resolution2 = await getVideoFileResolution(path)
596 expect(resolution2.videoFileResolution.toString()).to.equal(resolution)
597 expect(bitrate).to.be.below(getMaxBitrate(resolution2.videoFileResolution, fps, VIDEO_TRANSCODING_FPS))
602 it('Should not transcode to an higher bitrate than the original file', async function () {
603 this.timeout(160_000)
617 webtorrent: { enabled: true },
618 hls: { enabled: true }
621 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, config)
623 const videoAttributes = {
625 fixture: 'low-bitrate.mp4'
628 const resUpload = await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
629 const videoUUID = resUpload.body.video.uuid
631 await waitJobs(servers)
633 const resolutions = [ 240, 360, 480, 720, 1080 ]
634 for (const r of resolutions) {
635 const path = `videos/${videoUUID}-${r}.mp4`
636 const size = await getServerFileSize(servers[1], path)
637 expect(size, `${path} not below ${60_000}`).to.be.below(60_000)
642 describe('FFprobe', function () {
644 it('Should provide valid ffprobe data', async function () {
645 this.timeout(160_000)
647 const videoUUID = (await uploadVideoAndGetId({ server: servers[1], videoName: 'ffprobe data' })).uuid
648 await waitJobs(servers)
651 const path = buildServerDirectory(servers[1], join('videos', videoUUID + '-240.mp4'))
652 const metadata = await getMetadataFromFile(path)
654 // expected format properties
661 expect(metadata.format).to.have.nested.property(p)
664 // expected stream properties
670 'display_aspect_ratio',
674 expect(metadata.streams[0]).to.have.nested.property(p)
677 expect(metadata).to.not.have.nested.property('format.filename')
680 for (const server of servers) {
681 const res2 = await getVideo(server.url, videoUUID)
682 const videoDetails: VideoDetails = res2.body
684 const videoFiles = videoDetails.files
685 .concat(videoDetails.streamingPlaylists[0].files)
686 expect(videoFiles).to.have.lengthOf(8)
688 for (const file of videoFiles) {
689 expect(file.metadata).to.be.undefined
690 expect(file.metadataUrl).to.exist
691 expect(file.metadataUrl).to.contain(servers[1].url)
692 expect(file.metadataUrl).to.contain(videoUUID)
694 const res3 = await getVideoFileMetadataUrl(file.metadataUrl)
695 const metadata: FfprobeData = res3.body
696 expect(metadata).to.have.nested.property('format.size')
701 it('Should correctly detect if quick transcode is possible', async function () {
704 expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.mp4'))).to.be.true
705 expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.webm'))).to.be.false
709 describe('Transcoding job queue', function () {
711 it('Should have the appropriate priorities for transcoding jobs', async function () {
712 const res = await getJobsListPaginationAndSort({
714 accessToken: servers[1].accessToken,
718 jobType: 'video-transcoding'
721 const jobs = res.body.data as Job[]
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)