1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
5 import { omit } from 'lodash'
6 import { getMaxBitrate, VideoDetails, VideoResolution, VideoState } from '../../../../shared/models/videos'
12 getVideoFileResolution,
14 } from '../../../helpers/ffmpeg-utils'
16 buildAbsoluteFixturePath,
19 flushAndRunMultipleServers,
20 generateHighBitrateVideo,
21 generateVideoWithFramerate,
24 getVideoFileMetadataUrl,
29 setAccessTokensToServers,
30 uploadVideo, uploadVideoAndGetId,
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'
39 const expect = chai.expect
41 describe('Test video transcoding', function () {
42 let servers: ServerInfo[] = []
44 before(async function () {
48 servers = await flushAndRunMultipleServers(2)
50 await setAccessTokensToServers(servers)
52 await doubleFollow(servers[0], servers[1])
55 it('Should not transcode video on server 1', async function () {
58 const videoAttributes = {
59 name: 'my super name for server 1',
60 description: 'my super description for server 1',
61 fixture: 'video_short.webm'
63 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
65 await waitJobs(servers)
67 for (const server of servers) {
68 const res = await getVideosList(server.url)
69 const video = res.body.data[0]
71 const res2 = await getVideo(server.url, video.id)
72 const videoDetails = res2.body
73 expect(videoDetails.files).to.have.lengthOf(1)
75 const magnetUri = videoDetails.files[0].magnetUri
76 expect(magnetUri).to.match(/\.webm/)
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$/)
85 it('Should transcode video on server 2', async function () {
88 const videoAttributes = {
89 name: 'my super name for server 2',
90 description: 'my super description for server 2',
91 fixture: 'video_short.webm'
93 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
95 await waitJobs(servers)
97 for (const server of servers) {
98 const res = await getVideosList(server.url)
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
104 expect(videoDetails.files).to.have.lengthOf(4)
106 const magnetUri = videoDetails.files[0].magnetUri
107 expect(magnetUri).to.match(/\.mp4/)
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$/)
116 it('Should transcode high bit rate mp3 to proper bit rate', async function () {
119 const videoAttributes = {
121 fixture: 'video_short_mp3_256k.mp4'
123 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
125 await waitJobs(servers)
127 for (const server of servers) {
128 const res = await getVideosList(server.url)
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
134 expect(videoDetails.files).to.have.lengthOf(4)
136 const path = join(root(), 'test' + servers[1].internalServerNumber, 'videos', video.uuid + '-240.mp4')
137 const probe = await audio.get(path)
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)
143 this.fail('Could not retrieve the audio stream on ' + probe.absolutePath)
148 it('Should transcode video with no audio and have no audio itself', async function () {
151 const videoAttributes = {
153 fixture: 'video_short_no_audio.mp4'
155 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
157 await waitJobs(servers)
159 for (const server of servers) {
160 const res = await getVideosList(server.url)
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
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')
173 it('Should leave the audio untouched, but properly transcode the video', async function () {
176 const videoAttributes = {
177 name: 'untouched_audio',
178 fixture: 'video_short.mp4'
180 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
182 await waitJobs(servers)
184 for (const server of servers) {
185 const res = await getVideosList(server.url)
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
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))
200 this.fail('Could not retrieve the audio stream on ' + videoProbe.absolutePath)
205 it('Should transcode a 60 FPS video', async function () {
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'
213 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
215 await waitJobs(servers)
217 for (const server of servers) {
218 const res = await getVideosList(server.url)
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
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)
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)
234 expect(fps).to.be.below(31)
237 const path = join(root(), 'test' + servers[1].internalServerNumber, 'videos', video.uuid + '-720.mp4')
238 const fps = await getVideoFileFPS(path)
240 expect(fps).to.be.above(58).and.below(62)
244 it('Should wait for transcoding before publishing the video', async function () {
248 // Upload the video, but wait transcoding
249 const videoAttributes = {
250 name: 'waiting video',
251 fixture: 'video_short1.webm',
252 waitTranscoding: true
254 const resVideo = await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
255 const videoId = resVideo.body.video.uuid
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
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
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
277 // Server 1 should not have the video yet
278 await getVideo(servers[0].url, videoId, 404)
281 await waitJobs(servers)
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
288 const res2 = await getVideo(server.url, videoToFind.id)
289 const videoDetails: VideoDetails = res2.body
291 expect(videoDetails.state.id).to.equal(VideoState.PUBLISHED)
292 expect(videoDetails.state.label).to.equal('Published')
293 expect(videoDetails.waitTranscoding).to.be.true
297 it('Should respect maximum bitrate values', async function () {
300 let tempFixturePath: string
303 tempFixturePath = await generateHighBitrateVideo()
305 const bitrate = await getVideoFileBitrate(tempFixturePath)
306 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
309 const videoAttributes = {
310 name: 'high bitrate video',
311 description: 'high bitrate video',
312 fixture: tempFixturePath
315 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
317 await waitJobs(servers)
319 for (const server of servers) {
320 const res = await getVideosList(server.url)
322 const video = res.body.data.find(v => v.name === videoAttributes.name)
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)
330 expect(resolution2.videoFileResolution.toString()).to.equal(resolution)
331 expect(bitrate).to.be.below(getMaxBitrate(resolution2.videoFileResolution, fps, VIDEO_TRANSCODING_FPS))
336 it('Should accept and transcode additional extensions', async function () {
339 let tempFixturePath: string
342 tempFixturePath = await generateHighBitrateVideo()
344 const bitrate = await getVideoFileBitrate(tempFixturePath)
345 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
348 for (const fixture of [ 'video_short.mkv', 'video_short.avi' ]) {
349 const videoAttributes = {
354 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
356 await waitJobs(servers)
358 for (const server of servers) {
359 const res = await getVideosList(server.url)
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
365 expect(videoDetails.files).to.have.lengthOf(4)
367 const magnetUri = videoDetails.files[0].magnetUri
368 expect(magnetUri).to.contain('.mp4')
373 it('Should correctly detect if quick transcode is possible', async function () {
376 expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.mp4'))).to.be.true
377 expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.webm'))).to.be.false
380 it('Should merge an audio file with the preview file', async function () {
383 const videoAttributesArg = { name: 'audio_with_preview', previewfile: 'preview.jpg', fixture: 'sample.ogg' }
384 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributesArg)
386 await waitJobs(servers)
388 for (const server of servers) {
389 const res = await getVideosList(server.url)
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
395 expect(videoDetails.files).to.have.lengthOf(1)
397 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, statusCodeExpected: 200 })
398 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, statusCodeExpected: 200 })
400 const magnetUri = videoDetails.files[0].magnetUri
401 expect(magnetUri).to.contain('.mp4')
405 it('Should upload an audio file and choose a default background image', async function () {
408 const videoAttributesArg = { name: 'audio_without_preview', fixture: 'sample.ogg' }
409 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributesArg)
411 await waitJobs(servers)
413 for (const server of servers) {
414 const res = await getVideosList(server.url)
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
420 expect(videoDetails.files).to.have.lengthOf(1)
422 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, statusCodeExpected: 200 })
423 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, statusCodeExpected: 200 })
425 const magnetUri = videoDetails.files[0].magnetUri
426 expect(magnetUri).to.contain('.mp4')
430 it('Should downscale to the closest divisor standard framerate', async function () {
433 let tempFixturePath: string
436 tempFixturePath = await generateVideoWithFramerate(59)
438 const fps = await getVideoFileFPS(tempFixturePath)
439 expect(fps).to.be.equal(59)
442 const videoAttributes = {
444 description: '59fps video',
445 fixture: tempFixturePath
448 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
450 await waitJobs(servers)
452 for (const server of servers) {
453 const res = await getVideosList(server.url)
455 const video = res.body.data.find(v => v.name === videoAttributes.name)
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)
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)
471 it('Should provide valid ffprobe data', async function () {
474 const videoUUID = (await uploadVideoAndGetId({ server: servers[1], videoName: 'ffprobe data' })).uuid
475 await waitJobs(servers)
478 const path = join(root(), 'test' + servers[1].internalServerNumber, 'videos', videoUUID + '-240.mp4')
479 const metadata = await getMetadataFromFile<VideoFileMetadata>(path)
481 // expected format properties
488 expect(metadata.format).to.have.nested.property(p)
491 // expected stream properties
497 'display_aspect_ratio',
501 expect(metadata.streams[0]).to.have.nested.property(p)
504 expect(metadata).to.not.have.nested.property('format.filename')
507 for (const server of servers) {
508 const res2 = await getVideo(server.url, videoUUID)
509 const videoDetails: VideoDetails = res2.body
511 const videoFiles = videoDetails.files
512 .concat(videoDetails.streamingPlaylists[0].files)
513 expect(videoFiles).to.have.lengthOf(8)
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)
521 const res3 = await getVideoFileMetadataUrl(file.metadataUrl)
522 const metadata: FfprobeData = res3.body
523 expect(metadata).to.have.nested.property('format.size')
528 after(async function () {
529 await cleanupTests(servers)