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'
10 buildAbsoluteFixturePath,
14 flushAndRunMultipleServers,
15 generateHighBitrateVideo,
16 generateVideoWithFramerate,
20 getVideoFileMetadataUrl,
24 setAccessTokensToServers,
25 updateCustomSubConfig,
30 } from '../../../../shared/extra-utils'
31 import { getMaxBitrate, VideoDetails, VideoResolution, VideoState } from '../../../../shared/models/videos'
38 getVideoFileResolution
39 } from '../../../helpers/ffprobe-utils'
40 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
42 const expect = chai.expect
44 describe('Test video transcoding', function () {
45 let servers: ServerInfo[] = []
47 before(async function () {
51 servers = await flushAndRunMultipleServers(2)
53 await setAccessTokensToServers(servers)
55 await doubleFollow(servers[0], servers[1])
58 it('Should not transcode video on server 1', async function () {
61 const videoAttributes = {
62 name: 'my super name for server 1',
63 description: 'my super description for server 1',
64 fixture: 'video_short.webm'
66 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
68 await waitJobs(servers)
70 for (const server of servers) {
71 const res = await getVideosList(server.url)
72 const video = res.body.data[0]
74 const res2 = await getVideo(server.url, video.id)
75 const videoDetails = res2.body
76 expect(videoDetails.files).to.have.lengthOf(1)
78 const magnetUri = videoDetails.files[0].magnetUri
79 expect(magnetUri).to.match(/\.webm/)
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$/)
88 it('Should transcode video on server 2', async function () {
91 const videoAttributes = {
92 name: 'my super name for server 2',
93 description: 'my super description for server 2',
94 fixture: 'video_short.webm'
96 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
98 await waitJobs(servers)
100 for (const server of servers) {
101 const res = await getVideosList(server.url)
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
107 expect(videoDetails.files).to.have.lengthOf(4)
109 const magnetUri = videoDetails.files[0].magnetUri
110 expect(magnetUri).to.match(/\.mp4/)
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$/)
119 it('Should transcode high bit rate mp3 to proper bit rate', async function () {
122 const videoAttributes = {
124 fixture: 'video_short_mp3_256k.mp4'
126 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
128 await waitJobs(servers)
130 for (const server of servers) {
131 const res = await getVideosList(server.url)
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
137 expect(videoDetails.files).to.have.lengthOf(4)
139 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-240.mp4'))
140 const probe = await getAudioStream(path)
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)
146 this.fail('Could not retrieve the audio stream on ' + probe.absolutePath)
151 it('Should transcode video with no audio and have no audio itself', async function () {
154 const videoAttributes = {
156 fixture: 'video_short_no_audio.mp4'
158 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
160 await waitJobs(servers)
162 for (const server of servers) {
163 const res = await getVideosList(server.url)
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
169 expect(videoDetails.files).to.have.lengthOf(4)
170 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-240.mp4'))
171 const probe = await getAudioStream(path)
172 expect(probe).to.not.have.property('audioStream')
176 it('Should leave the audio untouched, but properly transcode the video', async function () {
179 const videoAttributes = {
180 name: 'untouched_audio',
181 fixture: 'video_short.mp4'
183 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
185 await waitJobs(servers)
187 for (const server of servers) {
188 const res = await getVideosList(server.url)
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
194 expect(videoDetails.files).to.have.lengthOf(4)
196 const fixturePath = buildAbsoluteFixturePath(videoAttributes.fixture)
197 const fixtureVideoProbe = await getAudioStream(fixturePath)
198 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-240.mp4'))
200 const videoProbe = await getAudioStream(path)
202 if (videoProbe.audioStream && fixtureVideoProbe.audioStream) {
203 const toOmit = [ 'max_bit_rate', 'duration', 'duration_ts', 'nb_frames', 'start_time', 'start_pts' ]
204 expect(omit(videoProbe.audioStream, toOmit)).to.be.deep.equal(omit(fixtureVideoProbe.audioStream, toOmit))
206 this.fail('Could not retrieve the audio stream on ' + videoProbe.absolutePath)
211 it('Should transcode a 60 FPS video', async function () {
214 const videoAttributes = {
215 name: 'my super 30fps name for server 2',
216 description: 'my super 30fps description for server 2',
217 fixture: '60fps_720p_small.mp4'
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: VideoDetails = res2.body
230 expect(videoDetails.files).to.have.lengthOf(4)
231 expect(videoDetails.files[0].fps).to.be.above(58).and.below(62)
232 expect(videoDetails.files[1].fps).to.be.below(31)
233 expect(videoDetails.files[2].fps).to.be.below(31)
234 expect(videoDetails.files[3].fps).to.be.below(31)
236 for (const resolution of [ '240', '360', '480' ]) {
237 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-' + resolution + '.mp4'))
238 const fps = await getVideoFileFPS(path)
240 expect(fps).to.be.below(31)
243 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-720.mp4'))
244 const fps = await getVideoFileFPS(path)
246 expect(fps).to.be.above(58).and.below(62)
250 it('Should wait for transcoding before publishing the video', async function () {
251 this.timeout(160_000)
254 // Upload the video, but wait transcoding
255 const videoAttributes = {
256 name: 'waiting video',
257 fixture: 'video_short1.webm',
258 waitTranscoding: true
260 const resVideo = await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
261 const videoId = resVideo.body.video.uuid
263 // Should be in transcode state
264 const { body } = await getVideo(servers[1].url, videoId)
265 expect(body.name).to.equal('waiting video')
266 expect(body.state.id).to.equal(VideoState.TO_TRANSCODE)
267 expect(body.state.label).to.equal('To transcode')
268 expect(body.waitTranscoding).to.be.true
270 // Should have my video
271 const resMyVideos = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 10)
272 const videoToFindInMine = resMyVideos.body.data.find(v => v.name === videoAttributes.name)
273 expect(videoToFindInMine).not.to.be.undefined
274 expect(videoToFindInMine.state.id).to.equal(VideoState.TO_TRANSCODE)
275 expect(videoToFindInMine.state.label).to.equal('To transcode')
276 expect(videoToFindInMine.waitTranscoding).to.be.true
278 // Should not list this video
279 const resVideos = await getVideosList(servers[1].url)
280 const videoToFindInList = resVideos.body.data.find(v => v.name === videoAttributes.name)
281 expect(videoToFindInList).to.be.undefined
283 // Server 1 should not have the video yet
284 await getVideo(servers[0].url, videoId, HttpStatusCode.NOT_FOUND_404)
287 await waitJobs(servers)
289 for (const server of servers) {
290 const res = await getVideosList(server.url)
291 const videoToFind = res.body.data.find(v => v.name === 'waiting video')
292 expect(videoToFind).not.to.be.undefined
294 const res2 = await getVideo(server.url, videoToFind.id)
295 const videoDetails: VideoDetails = res2.body
297 expect(videoDetails.state.id).to.equal(VideoState.PUBLISHED)
298 expect(videoDetails.state.label).to.equal('Published')
299 expect(videoDetails.waitTranscoding).to.be.true
303 it('Should respect maximum bitrate values', async function () {
304 this.timeout(160_000)
306 let tempFixturePath: string
309 tempFixturePath = await generateHighBitrateVideo()
311 const bitrate = await getVideoFileBitrate(tempFixturePath)
312 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
315 const videoAttributes = {
316 name: 'high bitrate video',
317 description: 'high bitrate video',
318 fixture: tempFixturePath
321 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
323 await waitJobs(servers)
325 for (const server of servers) {
326 const res = await getVideosList(server.url)
328 const video = res.body.data.find(v => v.name === videoAttributes.name)
330 for (const resolution of [ '240', '360', '480', '720', '1080' ]) {
331 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-' + resolution + '.mp4'))
333 const bitrate = await getVideoFileBitrate(path)
334 const fps = await getVideoFileFPS(path)
335 const resolution2 = await getVideoFileResolution(path)
337 expect(resolution2.videoFileResolution.toString()).to.equal(resolution)
338 expect(bitrate).to.be.below(getMaxBitrate(resolution2.videoFileResolution, fps, VIDEO_TRANSCODING_FPS))
343 it('Should accept and transcode additional extensions', async function () {
344 this.timeout(300_000)
346 let tempFixturePath: string
349 tempFixturePath = await generateHighBitrateVideo()
351 const bitrate = await getVideoFileBitrate(tempFixturePath)
352 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
355 for (const fixture of [ 'video_short.mkv', 'video_short.avi' ]) {
356 const videoAttributes = {
361 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
363 await waitJobs(servers)
365 for (const server of servers) {
366 const res = await getVideosList(server.url)
368 const video = res.body.data.find(v => v.name === videoAttributes.name)
369 const res2 = await getVideo(server.url, video.id)
370 const videoDetails = res2.body
372 expect(videoDetails.files).to.have.lengthOf(4)
374 const magnetUri = videoDetails.files[0].magnetUri
375 expect(magnetUri).to.contain('.mp4')
380 it('Should correctly detect if quick transcode is possible', async function () {
383 expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.mp4'))).to.be.true
384 expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.webm'))).to.be.false
387 it('Should merge an audio file with the preview file', async function () {
390 const videoAttributesArg = { name: 'audio_with_preview', previewfile: 'preview.jpg', fixture: 'sample.ogg' }
391 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributesArg)
393 await waitJobs(servers)
395 for (const server of servers) {
396 const res = await getVideosList(server.url)
398 const video = res.body.data.find(v => v.name === 'audio_with_preview')
399 const res2 = await getVideo(server.url, video.id)
400 const videoDetails: VideoDetails = res2.body
402 expect(videoDetails.files).to.have.lengthOf(1)
404 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, statusCodeExpected: HttpStatusCode.OK_200 })
405 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, statusCodeExpected: HttpStatusCode.OK_200 })
407 const magnetUri = videoDetails.files[0].magnetUri
408 expect(magnetUri).to.contain('.mp4')
412 it('Should upload an audio file and choose a default background image', async function () {
415 const videoAttributesArg = { name: 'audio_without_preview', fixture: 'sample.ogg' }
416 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributesArg)
418 await waitJobs(servers)
420 for (const server of servers) {
421 const res = await getVideosList(server.url)
423 const video = res.body.data.find(v => v.name === 'audio_without_preview')
424 const res2 = await getVideo(server.url, video.id)
425 const videoDetails = res2.body
427 expect(videoDetails.files).to.have.lengthOf(1)
429 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, statusCodeExpected: HttpStatusCode.OK_200 })
430 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, statusCodeExpected: HttpStatusCode.OK_200 })
432 const magnetUri = videoDetails.files[0].magnetUri
433 expect(magnetUri).to.contain('.mp4')
437 it('Should downscale to the closest divisor standard framerate', async function () {
438 this.timeout(200_000)
440 let tempFixturePath: string
443 tempFixturePath = await generateVideoWithFramerate(59)
445 const fps = await getVideoFileFPS(tempFixturePath)
446 expect(fps).to.be.equal(59)
449 const videoAttributes = {
451 description: '59fps video',
452 fixture: tempFixturePath
455 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
457 await waitJobs(servers)
459 for (const server of servers) {
460 const res = await getVideosList(server.url)
462 const video = res.body.data.find(v => v.name === videoAttributes.name)
465 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-240.mp4'))
466 const fps = await getVideoFileFPS(path)
467 expect(fps).to.be.equal(25)
471 const path = buildServerDirectory(servers[1], join('videos', video.uuid + '-720.mp4'))
472 const fps = await getVideoFileFPS(path)
473 expect(fps).to.be.equal(59)
478 it('Should not transcode to an higher bitrate than the original file', async function () {
479 this.timeout(160_000)
493 webtorrent: { enabled: true },
494 hls: { enabled: true }
497 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, config)
499 const videoAttributes = {
501 fixture: 'low-bitrate.mp4'
504 const resUpload = await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
505 const videoUUID = resUpload.body.video.uuid
507 await waitJobs(servers)
509 const resolutions = [ 240, 360, 480, 720, 1080 ]
510 for (const r of resolutions) {
511 expect(await getServerFileSize(servers[1], `videos/${videoUUID}-${r}.mp4`)).to.be.below(60_000)
515 it('Should provide valid ffprobe data', async function () {
516 this.timeout(160_000)
518 const videoUUID = (await uploadVideoAndGetId({ server: servers[1], videoName: 'ffprobe data' })).uuid
519 await waitJobs(servers)
522 const path = buildServerDirectory(servers[1], join('videos', videoUUID + '-240.mp4'))
523 const metadata = await getMetadataFromFile(path)
525 // expected format properties
532 expect(metadata.format).to.have.nested.property(p)
535 // expected stream properties
541 'display_aspect_ratio',
545 expect(metadata.streams[0]).to.have.nested.property(p)
548 expect(metadata).to.not.have.nested.property('format.filename')
551 for (const server of servers) {
552 const res2 = await getVideo(server.url, videoUUID)
553 const videoDetails: VideoDetails = res2.body
555 const videoFiles = videoDetails.files
556 .concat(videoDetails.streamingPlaylists[0].files)
557 expect(videoFiles).to.have.lengthOf(8)
559 for (const file of videoFiles) {
560 expect(file.metadata).to.be.undefined
561 expect(file.metadataUrl).to.exist
562 expect(file.metadataUrl).to.contain(servers[1].url)
563 expect(file.metadataUrl).to.contain(videoUUID)
565 const res3 = await getVideoFileMetadataUrl(file.metadataUrl)
566 const metadata: FfprobeData = res3.body
567 expect(metadata).to.have.nested.property('format.size')
572 it('Should transcode a 4k video', async function () {
573 this.timeout(200_000)
575 const videoAttributes = {
577 fixture: 'video_short_4k.mp4'
580 const resUpload = await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
581 const videoUUID = resUpload.body.video.uuid
583 await waitJobs(servers)
585 const resolutions = [ 240, 360, 480, 720, 1080, 1440, 2160 ]
587 for (const server of servers) {
588 const res = await getVideo(server.url, videoUUID)
589 const videoDetails: VideoDetails = res.body
591 expect(videoDetails.files).to.have.lengthOf(resolutions.length)
593 for (const r of resolutions) {
594 expect(videoDetails.files.find(f => f.resolution.id === r)).to.not.be.undefined
595 expect(videoDetails.streamingPlaylists[0].files.find(f => f.resolution.id === r)).to.not.be.undefined
600 after(async function () {
601 await cleanupTests(servers)