1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { omit } from 'lodash'
6 import { getMaxBitrate, getMinLimitBitrate } from '@shared/core-utils'
8 buildAbsoluteFixturePath,
10 createMultipleServers,
12 generateHighBitrateVideo,
13 generateVideoWithFramerate,
16 setAccessTokensToServers,
19 } from '@shared/extra-utils'
20 import { HttpStatusCode, VideoState } from '@shared/models'
27 getVideoFileResolution
28 } from '../../../helpers/ffprobe-utils'
30 const expect = chai.expect
32 function updateConfigForTranscoding (server: PeerTubeServer) {
33 return server.config.updateCustomSubConfig({
37 allowAdditionalExtensions: true,
38 allowAudioFiles: true,
39 hls: { enabled: true },
40 webtorrent: { enabled: true },
57 describe('Test video transcoding', function () {
58 let servers: PeerTubeServer[] = []
61 before(async function () {
65 servers = await createMultipleServers(2)
67 await setAccessTokensToServers(servers)
69 await doubleFollow(servers[0], servers[1])
71 await updateConfigForTranscoding(servers[1])
74 describe('Basic transcoding (or not)', function () {
76 it('Should not transcode video on server 1', async function () {
80 name: 'my super name for server 1',
81 description: 'my super description for server 1',
82 fixture: 'video_short.webm'
84 await servers[0].videos.upload({ attributes })
86 await waitJobs(servers)
88 for (const server of servers) {
89 const { data } = await server.videos.list()
92 const videoDetails = await server.videos.get({ id: video.id })
93 expect(videoDetails.files).to.have.lengthOf(1)
95 const magnetUri = videoDetails.files[0].magnetUri
96 expect(magnetUri).to.match(/\.webm/)
98 const torrent = await webtorrentAdd(magnetUri, true)
99 expect(torrent.files).to.be.an('array')
100 expect(torrent.files.length).to.equal(1)
101 expect(torrent.files[0].path).match(/\.webm$/)
105 it('Should transcode video on server 2', async function () {
106 this.timeout(120_000)
109 name: 'my super name for server 2',
110 description: 'my super description for server 2',
111 fixture: 'video_short.webm'
113 await servers[1].videos.upload({ attributes })
115 await waitJobs(servers)
117 for (const server of servers) {
118 const { data } = await server.videos.list()
120 const video = data.find(v => v.name === attributes.name)
121 const videoDetails = await server.videos.get({ id: video.id })
123 expect(videoDetails.files).to.have.lengthOf(5)
125 const magnetUri = videoDetails.files[0].magnetUri
126 expect(magnetUri).to.match(/\.mp4/)
128 const torrent = await webtorrentAdd(magnetUri, true)
129 expect(torrent.files).to.be.an('array')
130 expect(torrent.files.length).to.equal(1)
131 expect(torrent.files[0].path).match(/\.mp4$/)
135 it('Should wait for transcoding before publishing the video', async function () {
136 this.timeout(160_000)
139 // Upload the video, but wait transcoding
141 name: 'waiting video',
142 fixture: 'video_short1.webm',
143 waitTranscoding: true
145 const { uuid } = await servers[1].videos.upload({ attributes })
148 // Should be in transcode state
149 const body = await servers[1].videos.get({ id: videoId })
150 expect(body.name).to.equal('waiting video')
151 expect(body.state.id).to.equal(VideoState.TO_TRANSCODE)
152 expect(body.state.label).to.equal('To transcode')
153 expect(body.waitTranscoding).to.be.true
156 // Should have my video
157 const { data } = await servers[1].videos.listMyVideos()
158 const videoToFindInMine = data.find(v => v.name === attributes.name)
159 expect(videoToFindInMine).not.to.be.undefined
160 expect(videoToFindInMine.state.id).to.equal(VideoState.TO_TRANSCODE)
161 expect(videoToFindInMine.state.label).to.equal('To transcode')
162 expect(videoToFindInMine.waitTranscoding).to.be.true
166 // Should not list this video
167 const { data } = await servers[1].videos.list()
168 const videoToFindInList = data.find(v => v.name === attributes.name)
169 expect(videoToFindInList).to.be.undefined
172 // Server 1 should not have the video yet
173 await servers[0].videos.get({ id: videoId, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
176 await waitJobs(servers)
178 for (const server of servers) {
179 const { data } = await server.videos.list()
180 const videoToFind = data.find(v => v.name === 'waiting video')
181 expect(videoToFind).not.to.be.undefined
183 const videoDetails = await server.videos.get({ id: videoToFind.id })
185 expect(videoDetails.state.id).to.equal(VideoState.PUBLISHED)
186 expect(videoDetails.state.label).to.equal('Published')
187 expect(videoDetails.waitTranscoding).to.be.true
191 it('Should accept and transcode additional extensions', async function () {
192 this.timeout(300_000)
194 for (const fixture of [ 'video_short.mkv', 'video_short.avi' ]) {
200 await servers[1].videos.upload({ attributes })
202 await waitJobs(servers)
204 for (const server of servers) {
205 const { data } = await server.videos.list()
207 const video = data.find(v => v.name === attributes.name)
208 const videoDetails = await server.videos.get({ id: video.id })
209 expect(videoDetails.files).to.have.lengthOf(5)
211 const magnetUri = videoDetails.files[0].magnetUri
212 expect(magnetUri).to.contain('.mp4')
217 it('Should transcode a 4k video', async function () {
218 this.timeout(200_000)
222 fixture: 'video_short_4k.mp4'
225 const { uuid } = await servers[1].videos.upload({ attributes })
228 await waitJobs(servers)
230 const resolutions = [ 144, 240, 360, 480, 720, 1080, 1440, 2160 ]
232 for (const server of servers) {
233 const videoDetails = await server.videos.get({ id: video4k })
234 expect(videoDetails.files).to.have.lengthOf(resolutions.length)
236 for (const r of resolutions) {
237 expect(videoDetails.files.find(f => f.resolution.id === r)).to.not.be.undefined
238 expect(videoDetails.streamingPlaylists[0].files.find(f => f.resolution.id === r)).to.not.be.undefined
244 describe('Audio transcoding', function () {
246 it('Should transcode high bit rate mp3 to proper bit rate', async function () {
251 fixture: 'video_short_mp3_256k.mp4'
253 await servers[1].videos.upload({ attributes })
255 await waitJobs(servers)
257 for (const server of servers) {
258 const { data } = await server.videos.list()
260 const video = data.find(v => v.name === attributes.name)
261 const videoDetails = await server.videos.get({ id: video.id })
263 expect(videoDetails.files).to.have.lengthOf(5)
265 const file = videoDetails.files.find(f => f.resolution.id === 240)
266 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
267 const probe = await getAudioStream(path)
269 if (probe.audioStream) {
270 expect(probe.audioStream['codec_name']).to.be.equal('aac')
271 expect(probe.audioStream['bit_rate']).to.be.at.most(384 * 8000)
273 this.fail('Could not retrieve the audio stream on ' + probe.absolutePath)
278 it('Should transcode video with no audio and have no audio itself', async function () {
283 fixture: 'video_short_no_audio.mp4'
285 await servers[1].videos.upload({ attributes })
287 await waitJobs(servers)
289 for (const server of servers) {
290 const { data } = await server.videos.list()
292 const video = data.find(v => v.name === attributes.name)
293 const videoDetails = await server.videos.get({ id: video.id })
295 const file = videoDetails.files.find(f => f.resolution.id === 240)
296 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
298 const probe = await getAudioStream(path)
299 expect(probe).to.not.have.property('audioStream')
303 it('Should leave the audio untouched, but properly transcode the video', async function () {
307 name: 'untouched_audio',
308 fixture: 'video_short.mp4'
310 await servers[1].videos.upload({ attributes })
312 await waitJobs(servers)
314 for (const server of servers) {
315 const { data } = await server.videos.list()
317 const video = data.find(v => v.name === attributes.name)
318 const videoDetails = await server.videos.get({ id: video.id })
320 expect(videoDetails.files).to.have.lengthOf(5)
322 const fixturePath = buildAbsoluteFixturePath(attributes.fixture)
323 const fixtureVideoProbe = await getAudioStream(fixturePath)
325 const file = videoDetails.files.find(f => f.resolution.id === 240)
326 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
328 const videoProbe = await getAudioStream(path)
330 if (videoProbe.audioStream && fixtureVideoProbe.audioStream) {
331 const toOmit = [ 'max_bit_rate', 'duration', 'duration_ts', 'nb_frames', 'start_time', 'start_pts' ]
332 expect(omit(videoProbe.audioStream, toOmit)).to.be.deep.equal(omit(fixtureVideoProbe.audioStream, toOmit))
334 this.fail('Could not retrieve the audio stream on ' + videoProbe.absolutePath)
340 describe('Audio upload', function () {
342 function runSuite (mode: 'legacy' | 'resumable') {
344 before(async function () {
345 await servers[1].config.updateCustomSubConfig({
348 hls: { enabled: true },
349 webtorrent: { enabled: true },
366 it('Should merge an audio file with the preview file', async function () {
369 const attributes = { name: 'audio_with_preview', previewfile: 'preview.jpg', fixture: 'sample.ogg' }
370 await servers[1].videos.upload({ attributes, mode })
372 await waitJobs(servers)
374 for (const server of servers) {
375 const { data } = await server.videos.list()
377 const video = data.find(v => v.name === 'audio_with_preview')
378 const videoDetails = await server.videos.get({ id: video.id })
380 expect(videoDetails.files).to.have.lengthOf(1)
382 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 })
383 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, expectedStatus: HttpStatusCode.OK_200 })
385 const magnetUri = videoDetails.files[0].magnetUri
386 expect(magnetUri).to.contain('.mp4')
390 it('Should upload an audio file and choose a default background image', async function () {
393 const attributes = { name: 'audio_without_preview', fixture: 'sample.ogg' }
394 await servers[1].videos.upload({ attributes, mode })
396 await waitJobs(servers)
398 for (const server of servers) {
399 const { data } = await server.videos.list()
401 const video = data.find(v => v.name === 'audio_without_preview')
402 const videoDetails = await server.videos.get({ id: video.id })
404 expect(videoDetails.files).to.have.lengthOf(1)
406 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 })
407 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, expectedStatus: HttpStatusCode.OK_200 })
409 const magnetUri = videoDetails.files[0].magnetUri
410 expect(magnetUri).to.contain('.mp4')
414 it('Should upload an audio file and create an audio version only', async function () {
417 await servers[1].config.updateCustomSubConfig({
420 hls: { enabled: true },
421 webtorrent: { enabled: true },
432 const attributes = { name: 'audio_with_preview', previewfile: 'preview.jpg', fixture: 'sample.ogg' }
433 const { id } = await servers[1].videos.upload({ attributes, mode })
435 await waitJobs(servers)
437 for (const server of servers) {
438 const videoDetails = await server.videos.get({ id })
440 for (const files of [ videoDetails.files, videoDetails.streamingPlaylists[0].files ]) {
441 expect(files).to.have.lengthOf(2)
442 expect(files.find(f => f.resolution.id === 0)).to.not.be.undefined
446 await updateConfigForTranscoding(servers[1])
450 describe('Legacy upload', function () {
454 describe('Resumable upload', function () {
455 runSuite('resumable')
459 describe('Framerate', function () {
461 it('Should transcode a 60 FPS video', async function () {
465 name: 'my super 30fps name for server 2',
466 description: 'my super 30fps description for server 2',
467 fixture: '60fps_720p_small.mp4'
469 await servers[1].videos.upload({ attributes })
471 await waitJobs(servers)
473 for (const server of servers) {
474 const { data } = await server.videos.list()
476 const video = data.find(v => v.name === attributes.name)
477 const videoDetails = await server.videos.get({ id: video.id })
479 expect(videoDetails.files).to.have.lengthOf(5)
480 expect(videoDetails.files[0].fps).to.be.above(58).and.below(62)
481 expect(videoDetails.files[1].fps).to.be.below(31)
482 expect(videoDetails.files[2].fps).to.be.below(31)
483 expect(videoDetails.files[3].fps).to.be.below(31)
484 expect(videoDetails.files[4].fps).to.be.below(31)
486 for (const resolution of [ 144, 240, 360, 480 ]) {
487 const file = videoDetails.files.find(f => f.resolution.id === resolution)
488 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
489 const fps = await getVideoFileFPS(path)
491 expect(fps).to.be.below(31)
494 const file = videoDetails.files.find(f => f.resolution.id === 720)
495 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
496 const fps = await getVideoFileFPS(path)
498 expect(fps).to.be.above(58).and.below(62)
502 it('Should downscale to the closest divisor standard framerate', async function () {
503 this.timeout(200_000)
505 let tempFixturePath: string
508 tempFixturePath = await generateVideoWithFramerate(59)
510 const fps = await getVideoFileFPS(tempFixturePath)
511 expect(fps).to.be.equal(59)
516 description: '59fps video',
517 fixture: tempFixturePath
520 await servers[1].videos.upload({ attributes })
522 await waitJobs(servers)
524 for (const server of servers) {
525 const { data } = await server.videos.list()
527 const { id } = data.find(v => v.name === attributes.name)
528 const video = await server.videos.get({ id })
531 const file = video.files.find(f => f.resolution.id === 240)
532 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
533 const fps = await getVideoFileFPS(path)
534 expect(fps).to.be.equal(25)
538 const file = video.files.find(f => f.resolution.id === 720)
539 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
540 const fps = await getVideoFileFPS(path)
541 expect(fps).to.be.equal(59)
547 describe('Bitrate control', function () {
549 it('Should respect maximum bitrate values', async function () {
550 this.timeout(160_000)
552 const tempFixturePath = await generateHighBitrateVideo()
555 name: 'high bitrate video',
556 description: 'high bitrate video',
557 fixture: tempFixturePath
560 await servers[1].videos.upload({ attributes })
562 await waitJobs(servers)
564 for (const server of servers) {
565 const { data } = await server.videos.list()
567 const { id } = data.find(v => v.name === attributes.name)
568 const video = await server.videos.get({ id })
570 for (const resolution of [ 240, 360, 480, 720, 1080 ]) {
571 const file = video.files.find(f => f.resolution.id === resolution)
572 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
574 const bitrate = await getVideoFileBitrate(path)
575 const fps = await getVideoFileFPS(path)
576 const dataResolution = await getVideoFileResolution(path)
578 expect(resolution).to.equal(resolution)
580 const maxBitrate = getMaxBitrate({ ...dataResolution, fps })
581 expect(bitrate).to.be.below(maxBitrate)
586 it('Should not transcode to an higher bitrate than the original file but above our low limit', async function () {
587 this.timeout(160_000)
602 webtorrent: { enabled: true },
603 hls: { enabled: true }
606 await servers[1].config.updateCustomSubConfig({ newConfig })
610 fixture: 'low-bitrate.mp4'
613 const { id } = await servers[1].videos.upload({ attributes })
615 await waitJobs(servers)
617 const video = await servers[1].videos.get({ id })
619 const resolutions = [ 240, 360, 480, 720, 1080 ]
620 for (const r of resolutions) {
621 const file = video.files.find(f => f.resolution.id === r)
623 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
624 const bitrate = await getVideoFileBitrate(path)
626 const inputBitrate = 60_000
627 const limit = getMinLimitBitrate({ fps: 10, ratio: 1, resolution: r })
628 let belowValue = Math.max(inputBitrate, limit)
629 belowValue += belowValue * 0.20 // Apply 20% margin because bitrate control is not very precise
631 expect(bitrate, `${path} not below ${limit}`).to.be.below(belowValue)
636 describe('FFprobe', function () {
638 it('Should provide valid ffprobe data', async function () {
639 this.timeout(160_000)
641 const videoUUID = (await servers[1].videos.quickUpload({ name: 'ffprobe data' })).uuid
642 await waitJobs(servers)
645 const video = await servers[1].videos.get({ id: videoUUID })
646 const file = video.files.find(f => f.resolution.id === 240)
647 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
648 const metadata = await getMetadataFromFile(path)
650 // expected format properties
657 expect(metadata.format).to.have.nested.property(p)
660 // expected stream properties
666 'display_aspect_ratio',
670 expect(metadata.streams[0]).to.have.nested.property(p)
673 expect(metadata).to.not.have.nested.property('format.filename')
676 for (const server of servers) {
677 const videoDetails = await server.videos.get({ id: videoUUID })
679 const videoFiles = videoDetails.files
680 .concat(videoDetails.streamingPlaylists[0].files)
681 expect(videoFiles).to.have.lengthOf(10)
683 for (const file of videoFiles) {
684 expect(file.metadata).to.be.undefined
685 expect(file.metadataUrl).to.exist
686 expect(file.metadataUrl).to.contain(servers[1].url)
687 expect(file.metadataUrl).to.contain(videoUUID)
689 const metadata = await server.videos.getFileMetadata({ url: file.metadataUrl })
690 expect(metadata).to.have.nested.property('format.size')
695 it('Should correctly detect if quick transcode is possible', async function () {
698 expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.mp4'))).to.be.true
699 expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.webm'))).to.be.false
703 describe('Transcoding job queue', function () {
705 it('Should have the appropriate priorities for transcoding jobs', async function () {
706 const body = await servers[1].jobs.list({
710 jobType: 'video-transcoding'
713 const jobs = body.data
714 const transcodingJobs = jobs.filter(j => j.data.videoUUID === video4k)
716 expect(transcodingJobs).to.have.lengthOf(16)
718 const hlsJobs = transcodingJobs.filter(j => j.data.type === 'new-resolution-to-hls')
719 const webtorrentJobs = transcodingJobs.filter(j => j.data.type === 'new-resolution-to-webtorrent')
720 const optimizeJobs = transcodingJobs.filter(j => j.data.type === 'optimize-to-webtorrent')
722 expect(hlsJobs).to.have.lengthOf(8)
723 expect(webtorrentJobs).to.have.lengthOf(7)
724 expect(optimizeJobs).to.have.lengthOf(1)
726 for (const j of optimizeJobs.concat(hlsJobs.concat(webtorrentJobs))) {
727 expect(j.priority).to.be.greaterThan(100)
728 expect(j.priority).to.be.lessThan(150)
733 after(async function () {
734 await cleanupTests(servers)