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 { join } from 'path'
7 import { HttpStatusCode } from '@shared/core-utils'
9 buildAbsoluteFixturePath,
12 createMultipleServers,
13 generateHighBitrateVideo,
14 generateVideoWithFramerate,
17 setAccessTokensToServers,
20 } from '@shared/extra-utils'
21 import { getMaxBitrate, VideoResolution, VideoState } from '@shared/models'
22 import { VIDEO_TRANSCODING_FPS } from '../../../../server/initializers/constants'
29 getVideoFileResolution
30 } from '../../../helpers/ffprobe-utils'
32 const expect = chai.expect
34 function updateConfigForTranscoding (server: PeerTubeServer) {
35 return server.config.updateCustomSubConfig({
39 allowAdditionalExtensions: true,
40 allowAudioFiles: true,
41 hls: { enabled: true },
42 webtorrent: { enabled: true },
58 describe('Test video transcoding', function () {
59 let servers: PeerTubeServer[] = []
62 before(async function () {
66 servers = await createMultipleServers(2)
68 await setAccessTokensToServers(servers)
70 await doubleFollow(servers[0], servers[1])
72 await updateConfigForTranscoding(servers[1])
75 describe('Basic transcoding (or not)', function () {
77 it('Should not transcode video on server 1', async function () {
81 name: 'my super name for server 1',
82 description: 'my super description for server 1',
83 fixture: 'video_short.webm'
85 await servers[0].videos.upload({ attributes })
87 await waitJobs(servers)
89 for (const server of servers) {
90 const { data } = await server.videos.list()
93 const videoDetails = await server.videos.get({ id: video.id })
94 expect(videoDetails.files).to.have.lengthOf(1)
96 const magnetUri = videoDetails.files[0].magnetUri
97 expect(magnetUri).to.match(/\.webm/)
99 const torrent = await webtorrentAdd(magnetUri, true)
100 expect(torrent.files).to.be.an('array')
101 expect(torrent.files.length).to.equal(1)
102 expect(torrent.files[0].path).match(/\.webm$/)
106 it('Should transcode video on server 2', async function () {
107 this.timeout(120_000)
110 name: 'my super name for server 2',
111 description: 'my super description for server 2',
112 fixture: 'video_short.webm'
114 await servers[1].videos.upload({ attributes })
116 await waitJobs(servers)
118 for (const server of servers) {
119 const { data } = await server.videos.list()
121 const video = data.find(v => v.name === attributes.name)
122 const videoDetails = await server.videos.get({ id: video.id })
124 expect(videoDetails.files).to.have.lengthOf(4)
126 const magnetUri = videoDetails.files[0].magnetUri
127 expect(magnetUri).to.match(/\.mp4/)
129 const torrent = await webtorrentAdd(magnetUri, true)
130 expect(torrent.files).to.be.an('array')
131 expect(torrent.files.length).to.equal(1)
132 expect(torrent.files[0].path).match(/\.mp4$/)
136 it('Should wait for transcoding before publishing the video', async function () {
137 this.timeout(160_000)
140 // Upload the video, but wait transcoding
142 name: 'waiting video',
143 fixture: 'video_short1.webm',
144 waitTranscoding: true
146 const { uuid } = await servers[1].videos.upload({ attributes })
149 // Should be in transcode state
150 const body = await servers[1].videos.get({ id: videoId })
151 expect(body.name).to.equal('waiting video')
152 expect(body.state.id).to.equal(VideoState.TO_TRANSCODE)
153 expect(body.state.label).to.equal('To transcode')
154 expect(body.waitTranscoding).to.be.true
157 // Should have my video
158 const { data } = await servers[1].videos.listMyVideos()
159 const videoToFindInMine = data.find(v => v.name === attributes.name)
160 expect(videoToFindInMine).not.to.be.undefined
161 expect(videoToFindInMine.state.id).to.equal(VideoState.TO_TRANSCODE)
162 expect(videoToFindInMine.state.label).to.equal('To transcode')
163 expect(videoToFindInMine.waitTranscoding).to.be.true
167 // Should not list this video
168 const { data } = await servers[1].videos.list()
169 const videoToFindInList = data.find(v => v.name === attributes.name)
170 expect(videoToFindInList).to.be.undefined
173 // Server 1 should not have the video yet
174 await servers[0].videos.get({ id: videoId, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
177 await waitJobs(servers)
179 for (const server of servers) {
180 const { data } = await server.videos.list()
181 const videoToFind = data.find(v => v.name === 'waiting video')
182 expect(videoToFind).not.to.be.undefined
184 const videoDetails = await server.videos.get({ id: videoToFind.id })
186 expect(videoDetails.state.id).to.equal(VideoState.PUBLISHED)
187 expect(videoDetails.state.label).to.equal('Published')
188 expect(videoDetails.waitTranscoding).to.be.true
192 it('Should accept and transcode additional extensions', async function () {
193 this.timeout(300_000)
195 let tempFixturePath: string
198 tempFixturePath = await generateHighBitrateVideo()
200 const bitrate = await getVideoFileBitrate(tempFixturePath)
201 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
204 for (const fixture of [ 'video_short.mkv', 'video_short.avi' ]) {
210 await servers[1].videos.upload({ attributes })
212 await waitJobs(servers)
214 for (const server of servers) {
215 const { data } = await server.videos.list()
217 const video = data.find(v => v.name === attributes.name)
218 const videoDetails = await server.videos.get({ id: video.id })
219 expect(videoDetails.files).to.have.lengthOf(4)
221 const magnetUri = videoDetails.files[0].magnetUri
222 expect(magnetUri).to.contain('.mp4')
227 it('Should transcode a 4k video', async function () {
228 this.timeout(200_000)
232 fixture: 'video_short_4k.mp4'
235 const { uuid } = await servers[1].videos.upload({ attributes })
238 await waitJobs(servers)
240 const resolutions = [ 240, 360, 480, 720, 1080, 1440, 2160 ]
242 for (const server of servers) {
243 const videoDetails = await server.videos.get({ id: video4k })
244 expect(videoDetails.files).to.have.lengthOf(resolutions.length)
246 for (const r of resolutions) {
247 expect(videoDetails.files.find(f => f.resolution.id === r)).to.not.be.undefined
248 expect(videoDetails.streamingPlaylists[0].files.find(f => f.resolution.id === r)).to.not.be.undefined
254 describe('Audio transcoding', function () {
256 it('Should transcode high bit rate mp3 to proper bit rate', async function () {
261 fixture: 'video_short_mp3_256k.mp4'
263 await servers[1].videos.upload({ attributes })
265 await waitJobs(servers)
267 for (const server of servers) {
268 const { data } = await server.videos.list()
270 const video = data.find(v => v.name === attributes.name)
271 const videoDetails = await server.videos.get({ id: video.id })
273 expect(videoDetails.files).to.have.lengthOf(4)
275 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-240.mp4'))
276 const probe = await getAudioStream(path)
278 if (probe.audioStream) {
279 expect(probe.audioStream['codec_name']).to.be.equal('aac')
280 expect(probe.audioStream['bit_rate']).to.be.at.most(384 * 8000)
282 this.fail('Could not retrieve the audio stream on ' + probe.absolutePath)
287 it('Should transcode video with no audio and have no audio itself', async function () {
292 fixture: 'video_short_no_audio.mp4'
294 await servers[1].videos.upload({ attributes })
296 await waitJobs(servers)
298 for (const server of servers) {
299 const { data } = await server.videos.list()
301 const video = data.find(v => v.name === attributes.name)
302 const videoDetails = await server.videos.get({ id: video.id })
304 expect(videoDetails.files).to.have.lengthOf(4)
305 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-240.mp4'))
306 const probe = await getAudioStream(path)
307 expect(probe).to.not.have.property('audioStream')
311 it('Should leave the audio untouched, but properly transcode the video', async function () {
315 name: 'untouched_audio',
316 fixture: 'video_short.mp4'
318 await servers[1].videos.upload({ attributes })
320 await waitJobs(servers)
322 for (const server of servers) {
323 const { data } = await server.videos.list()
325 const video = data.find(v => v.name === attributes.name)
326 const videoDetails = await server.videos.get({ id: video.id })
328 expect(videoDetails.files).to.have.lengthOf(4)
330 const fixturePath = buildAbsoluteFixturePath(attributes.fixture)
331 const fixtureVideoProbe = await getAudioStream(fixturePath)
332 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-240.mp4'))
334 const videoProbe = await getAudioStream(path)
336 if (videoProbe.audioStream && fixtureVideoProbe.audioStream) {
337 const toOmit = [ 'max_bit_rate', 'duration', 'duration_ts', 'nb_frames', 'start_time', 'start_pts' ]
338 expect(omit(videoProbe.audioStream, toOmit)).to.be.deep.equal(omit(fixtureVideoProbe.audioStream, toOmit))
340 this.fail('Could not retrieve the audio stream on ' + videoProbe.absolutePath)
346 describe('Audio upload', function () {
348 function runSuite (mode: 'legacy' | 'resumable') {
350 before(async function () {
351 await servers[1].config.updateCustomSubConfig({
354 hls: { enabled: true },
355 webtorrent: { enabled: true },
371 it('Should merge an audio file with the preview file', async function () {
374 const attributes = { name: 'audio_with_preview', previewfile: 'preview.jpg', fixture: 'sample.ogg' }
375 await servers[1].videos.upload({ attributes, mode })
377 await waitJobs(servers)
379 for (const server of servers) {
380 const { data } = await server.videos.list()
382 const video = data.find(v => v.name === 'audio_with_preview')
383 const videoDetails = await server.videos.get({ id: video.id })
385 expect(videoDetails.files).to.have.lengthOf(1)
387 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, statusCodeExpected: HttpStatusCode.OK_200 })
388 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, statusCodeExpected: HttpStatusCode.OK_200 })
390 const magnetUri = videoDetails.files[0].magnetUri
391 expect(magnetUri).to.contain('.mp4')
395 it('Should upload an audio file and choose a default background image', async function () {
398 const attributes = { name: 'audio_without_preview', fixture: 'sample.ogg' }
399 await servers[1].videos.upload({ attributes, mode })
401 await waitJobs(servers)
403 for (const server of servers) {
404 const { data } = await server.videos.list()
406 const video = data.find(v => v.name === 'audio_without_preview')
407 const videoDetails = await server.videos.get({ id: video.id })
409 expect(videoDetails.files).to.have.lengthOf(1)
411 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, statusCodeExpected: HttpStatusCode.OK_200 })
412 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, statusCodeExpected: HttpStatusCode.OK_200 })
414 const magnetUri = videoDetails.files[0].magnetUri
415 expect(magnetUri).to.contain('.mp4')
419 it('Should upload an audio file and create an audio version only', async function () {
422 await servers[1].config.updateCustomSubConfig({
425 hls: { enabled: true },
426 webtorrent: { enabled: true },
436 const attributes = { name: 'audio_with_preview', previewfile: 'preview.jpg', fixture: 'sample.ogg' }
437 const { id } = await servers[1].videos.upload({ attributes, mode })
439 await waitJobs(servers)
441 for (const server of servers) {
442 const videoDetails = await server.videos.get({ id })
444 for (const files of [ videoDetails.files, videoDetails.streamingPlaylists[0].files ]) {
445 expect(files).to.have.lengthOf(2)
446 expect(files.find(f => f.resolution.id === 0)).to.not.be.undefined
450 await updateConfigForTranscoding(servers[1])
454 describe('Legacy upload', function () {
458 describe('Resumable upload', function () {
459 runSuite('resumable')
463 describe('Framerate', function () {
465 it('Should transcode a 60 FPS video', async function () {
469 name: 'my super 30fps name for server 2',
470 description: 'my super 30fps description for server 2',
471 fixture: '60fps_720p_small.mp4'
473 await servers[1].videos.upload({ attributes })
475 await waitJobs(servers)
477 for (const server of servers) {
478 const { data } = await server.videos.list()
480 const video = data.find(v => v.name === attributes.name)
481 const videoDetails = await server.videos.get({ id: video.id })
483 expect(videoDetails.files).to.have.lengthOf(4)
484 expect(videoDetails.files[0].fps).to.be.above(58).and.below(62)
485 expect(videoDetails.files[1].fps).to.be.below(31)
486 expect(videoDetails.files[2].fps).to.be.below(31)
487 expect(videoDetails.files[3].fps).to.be.below(31)
489 for (const resolution of [ '240', '360', '480' ]) {
490 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-' + resolution + '.mp4'))
491 const fps = await getVideoFileFPS(path)
493 expect(fps).to.be.below(31)
496 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-720.mp4'))
497 const fps = await getVideoFileFPS(path)
499 expect(fps).to.be.above(58).and.below(62)
503 it('Should downscale to the closest divisor standard framerate', async function () {
504 this.timeout(200_000)
506 let tempFixturePath: string
509 tempFixturePath = await generateVideoWithFramerate(59)
511 const fps = await getVideoFileFPS(tempFixturePath)
512 expect(fps).to.be.equal(59)
517 description: '59fps video',
518 fixture: tempFixturePath
521 await servers[1].videos.upload({ attributes })
523 await waitJobs(servers)
525 for (const server of servers) {
526 const { data } = await server.videos.list()
528 const video = data.find(v => v.name === attributes.name)
531 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-240.mp4'))
532 const fps = await getVideoFileFPS(path)
533 expect(fps).to.be.equal(25)
537 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-720.mp4'))
538 const fps = await getVideoFileFPS(path)
539 expect(fps).to.be.equal(59)
545 describe('Bitrate control', function () {
546 it('Should respect maximum bitrate values', async function () {
547 this.timeout(160_000)
549 let tempFixturePath: string
552 tempFixturePath = await generateHighBitrateVideo()
554 const bitrate = await getVideoFileBitrate(tempFixturePath)
555 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
559 name: 'high bitrate video',
560 description: 'high bitrate video',
561 fixture: tempFixturePath
564 await servers[1].videos.upload({ attributes })
566 await waitJobs(servers)
568 for (const server of servers) {
569 const { data } = await server.videos.list()
571 const video = data.find(v => v.name === attributes.name)
573 for (const resolution of [ '240', '360', '480', '720', '1080' ]) {
574 const path = servers[1].servers.buildDirectory(join('videos', video.uuid + '-' + resolution + '.mp4'))
576 const bitrate = await getVideoFileBitrate(path)
577 const fps = await getVideoFileFPS(path)
578 const resolution2 = await getVideoFileResolution(path)
580 expect(resolution2.videoFileResolution.toString()).to.equal(resolution)
581 expect(bitrate).to.be.below(getMaxBitrate(resolution2.videoFileResolution, fps, VIDEO_TRANSCODING_FPS))
586 it('Should not transcode to an higher bitrate than the original file', async function () {
587 this.timeout(160_000)
601 webtorrent: { enabled: true },
602 hls: { enabled: true }
605 await servers[1].config.updateCustomSubConfig({ newConfig })
609 fixture: 'low-bitrate.mp4'
612 const { uuid } = await servers[1].videos.upload({ attributes })
614 await waitJobs(servers)
616 const resolutions = [ 240, 360, 480, 720, 1080 ]
617 for (const r of resolutions) {
618 const path = `videos/${uuid}-${r}.mp4`
619 const size = await servers[1].servers.getServerFileSize(path)
620 expect(size, `${path} not below ${60_000}`).to.be.below(60_000)
625 describe('FFprobe', function () {
627 it('Should provide valid ffprobe data', async function () {
628 this.timeout(160_000)
630 const videoUUID = (await servers[1].videos.quickUpload({ name: 'ffprobe data' })).uuid
631 await waitJobs(servers)
634 const path = servers[1].servers.buildDirectory(join('videos', videoUUID + '-240.mp4'))
635 const metadata = await getMetadataFromFile(path)
637 // expected format properties
644 expect(metadata.format).to.have.nested.property(p)
647 // expected stream properties
653 'display_aspect_ratio',
657 expect(metadata.streams[0]).to.have.nested.property(p)
660 expect(metadata).to.not.have.nested.property('format.filename')
663 for (const server of servers) {
664 const videoDetails = await server.videos.get({ id: videoUUID })
666 const videoFiles = videoDetails.files
667 .concat(videoDetails.streamingPlaylists[0].files)
668 expect(videoFiles).to.have.lengthOf(8)
670 for (const file of videoFiles) {
671 expect(file.metadata).to.be.undefined
672 expect(file.metadataUrl).to.exist
673 expect(file.metadataUrl).to.contain(servers[1].url)
674 expect(file.metadataUrl).to.contain(videoUUID)
676 const metadata = await server.videos.getFileMetadata({ url: file.metadataUrl })
677 expect(metadata).to.have.nested.property('format.size')
682 it('Should correctly detect if quick transcode is possible', async function () {
685 expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.mp4'))).to.be.true
686 expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.webm'))).to.be.false
690 describe('Transcoding job queue', function () {
692 it('Should have the appropriate priorities for transcoding jobs', async function () {
693 const body = await servers[1].jobs.getJobsList({
697 jobType: 'video-transcoding'
700 const jobs = body.data
701 const transcodingJobs = jobs.filter(j => j.data.videoUUID === video4k)
703 expect(transcodingJobs).to.have.lengthOf(14)
705 const hlsJobs = transcodingJobs.filter(j => j.data.type === 'new-resolution-to-hls')
706 const webtorrentJobs = transcodingJobs.filter(j => j.data.type === 'new-resolution-to-webtorrent')
707 const optimizeJobs = transcodingJobs.filter(j => j.data.type === 'optimize-to-webtorrent')
709 expect(hlsJobs).to.have.lengthOf(7)
710 expect(webtorrentJobs).to.have.lengthOf(6)
711 expect(optimizeJobs).to.have.lengthOf(1)
713 for (const j of optimizeJobs.concat(hlsJobs.concat(webtorrentJobs))) {
714 expect(j.priority).to.be.greaterThan(100)
715 expect(j.priority).to.be.lessThan(150)
720 after(async function () {
721 await cleanupTests(servers)