1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { omit } from 'lodash'
7 buildAbsoluteFixturePath,
11 generateHighBitrateVideo,
12 generateVideoWithFramerate,
16 setAccessTokensToServers,
19 } from '@shared/extra-utils'
20 import { getMaxBitrate, HttpStatusCode, VideoResolution, VideoState } from '@shared/models'
21 import { VIDEO_TRANSCODING_FPS } from '../../../../server/initializers/constants'
28 getVideoFileResolution
29 } from '../../../helpers/ffprobe-utils'
31 const expect = chai.expect
33 function updateConfigForTranscoding (server: PeerTubeServer) {
34 return server.config.updateCustomSubConfig({
38 allowAdditionalExtensions: true,
39 allowAudioFiles: true,
40 hls: { enabled: true },
41 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(4)
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 let tempFixturePath: string
197 tempFixturePath = await generateHighBitrateVideo()
199 const bitrate = await getVideoFileBitrate(tempFixturePath)
200 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
203 for (const fixture of [ 'video_short.mkv', 'video_short.avi' ]) {
209 await servers[1].videos.upload({ attributes })
211 await waitJobs(servers)
213 for (const server of servers) {
214 const { data } = await server.videos.list()
216 const video = data.find(v => v.name === attributes.name)
217 const videoDetails = await server.videos.get({ id: video.id })
218 expect(videoDetails.files).to.have.lengthOf(4)
220 const magnetUri = videoDetails.files[0].magnetUri
221 expect(magnetUri).to.contain('.mp4')
226 it('Should transcode a 4k video', async function () {
227 this.timeout(200_000)
231 fixture: 'video_short_4k.mp4'
234 const { uuid } = await servers[1].videos.upload({ attributes })
237 await waitJobs(servers)
239 const resolutions = [ 240, 360, 480, 720, 1080, 1440, 2160 ]
241 for (const server of servers) {
242 const videoDetails = await server.videos.get({ id: video4k })
243 expect(videoDetails.files).to.have.lengthOf(resolutions.length)
245 for (const r of resolutions) {
246 expect(videoDetails.files.find(f => f.resolution.id === r)).to.not.be.undefined
247 expect(videoDetails.streamingPlaylists[0].files.find(f => f.resolution.id === r)).to.not.be.undefined
253 describe('Audio transcoding', function () {
255 it('Should transcode high bit rate mp3 to proper bit rate', async function () {
260 fixture: 'video_short_mp3_256k.mp4'
262 await servers[1].videos.upload({ attributes })
264 await waitJobs(servers)
266 for (const server of servers) {
267 const { data } = await server.videos.list()
269 const video = data.find(v => v.name === attributes.name)
270 const videoDetails = await server.videos.get({ id: video.id })
272 expect(videoDetails.files).to.have.lengthOf(4)
274 const file = videoDetails.files.find(f => f.resolution.id === 240)
275 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
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 const file = videoDetails.files.find(f => f.resolution.id === 240)
305 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
307 const probe = await getAudioStream(path)
308 expect(probe).to.not.have.property('audioStream')
312 it('Should leave the audio untouched, but properly transcode the video', async function () {
316 name: 'untouched_audio',
317 fixture: 'video_short.mp4'
319 await servers[1].videos.upload({ attributes })
321 await waitJobs(servers)
323 for (const server of servers) {
324 const { data } = await server.videos.list()
326 const video = data.find(v => v.name === attributes.name)
327 const videoDetails = await server.videos.get({ id: video.id })
329 expect(videoDetails.files).to.have.lengthOf(4)
331 const fixturePath = buildAbsoluteFixturePath(attributes.fixture)
332 const fixtureVideoProbe = await getAudioStream(fixturePath)
334 const file = videoDetails.files.find(f => f.resolution.id === 240)
335 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
337 const videoProbe = await getAudioStream(path)
339 if (videoProbe.audioStream && fixtureVideoProbe.audioStream) {
340 const toOmit = [ 'max_bit_rate', 'duration', 'duration_ts', 'nb_frames', 'start_time', 'start_pts' ]
341 expect(omit(videoProbe.audioStream, toOmit)).to.be.deep.equal(omit(fixtureVideoProbe.audioStream, toOmit))
343 this.fail('Could not retrieve the audio stream on ' + videoProbe.absolutePath)
349 describe('Audio upload', function () {
351 function runSuite (mode: 'legacy' | 'resumable') {
353 before(async function () {
354 await servers[1].config.updateCustomSubConfig({
357 hls: { enabled: true },
358 webtorrent: { enabled: true },
374 it('Should merge an audio file with the preview file', async function () {
377 const attributes = { name: 'audio_with_preview', previewfile: 'preview.jpg', fixture: 'sample.ogg' }
378 await servers[1].videos.upload({ attributes, mode })
380 await waitJobs(servers)
382 for (const server of servers) {
383 const { data } = await server.videos.list()
385 const video = data.find(v => v.name === 'audio_with_preview')
386 const videoDetails = await server.videos.get({ id: video.id })
388 expect(videoDetails.files).to.have.lengthOf(1)
390 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 })
391 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, expectedStatus: HttpStatusCode.OK_200 })
393 const magnetUri = videoDetails.files[0].magnetUri
394 expect(magnetUri).to.contain('.mp4')
398 it('Should upload an audio file and choose a default background image', async function () {
401 const attributes = { name: 'audio_without_preview', fixture: 'sample.ogg' }
402 await servers[1].videos.upload({ attributes, mode })
404 await waitJobs(servers)
406 for (const server of servers) {
407 const { data } = await server.videos.list()
409 const video = data.find(v => v.name === 'audio_without_preview')
410 const videoDetails = await server.videos.get({ id: video.id })
412 expect(videoDetails.files).to.have.lengthOf(1)
414 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 })
415 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, expectedStatus: HttpStatusCode.OK_200 })
417 const magnetUri = videoDetails.files[0].magnetUri
418 expect(magnetUri).to.contain('.mp4')
422 it('Should upload an audio file and create an audio version only', async function () {
425 await servers[1].config.updateCustomSubConfig({
428 hls: { enabled: true },
429 webtorrent: { enabled: true },
439 const attributes = { name: 'audio_with_preview', previewfile: 'preview.jpg', fixture: 'sample.ogg' }
440 const { id } = await servers[1].videos.upload({ attributes, mode })
442 await waitJobs(servers)
444 for (const server of servers) {
445 const videoDetails = await server.videos.get({ id })
447 for (const files of [ videoDetails.files, videoDetails.streamingPlaylists[0].files ]) {
448 expect(files).to.have.lengthOf(2)
449 expect(files.find(f => f.resolution.id === 0)).to.not.be.undefined
453 await updateConfigForTranscoding(servers[1])
457 describe('Legacy upload', function () {
461 describe('Resumable upload', function () {
462 runSuite('resumable')
466 describe('Framerate', function () {
468 it('Should transcode a 60 FPS video', async function () {
472 name: 'my super 30fps name for server 2',
473 description: 'my super 30fps description for server 2',
474 fixture: '60fps_720p_small.mp4'
476 await servers[1].videos.upload({ attributes })
478 await waitJobs(servers)
480 for (const server of servers) {
481 const { data } = await server.videos.list()
483 const video = data.find(v => v.name === attributes.name)
484 const videoDetails = await server.videos.get({ id: video.id })
486 expect(videoDetails.files).to.have.lengthOf(4)
487 expect(videoDetails.files[0].fps).to.be.above(58).and.below(62)
488 expect(videoDetails.files[1].fps).to.be.below(31)
489 expect(videoDetails.files[2].fps).to.be.below(31)
490 expect(videoDetails.files[3].fps).to.be.below(31)
492 for (const resolution of [ 240, 360, 480 ]) {
493 const file = videoDetails.files.find(f => f.resolution.id === resolution)
494 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
495 const fps = await getVideoFileFPS(path)
497 expect(fps).to.be.below(31)
500 const file = videoDetails.files.find(f => f.resolution.id === 720)
501 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
502 const fps = await getVideoFileFPS(path)
504 expect(fps).to.be.above(58).and.below(62)
508 it('Should downscale to the closest divisor standard framerate', async function () {
509 this.timeout(200_000)
511 let tempFixturePath: string
514 tempFixturePath = await generateVideoWithFramerate(59)
516 const fps = await getVideoFileFPS(tempFixturePath)
517 expect(fps).to.be.equal(59)
522 description: '59fps video',
523 fixture: tempFixturePath
526 await servers[1].videos.upload({ attributes })
528 await waitJobs(servers)
530 for (const server of servers) {
531 const { data } = await server.videos.list()
533 const { id } = data.find(v => v.name === attributes.name)
534 const video = await server.videos.get({ id })
537 const file = video.files.find(f => f.resolution.id === 240)
538 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
539 const fps = await getVideoFileFPS(path)
540 expect(fps).to.be.equal(25)
544 const file = video.files.find(f => f.resolution.id === 720)
545 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
546 const fps = await getVideoFileFPS(path)
547 expect(fps).to.be.equal(59)
553 describe('Bitrate control', function () {
555 it('Should respect maximum bitrate values', async function () {
556 this.timeout(160_000)
558 let tempFixturePath: string
561 tempFixturePath = await generateHighBitrateVideo()
563 const bitrate = await getVideoFileBitrate(tempFixturePath)
564 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
568 name: 'high bitrate video',
569 description: 'high bitrate video',
570 fixture: tempFixturePath
573 await servers[1].videos.upload({ attributes })
575 await waitJobs(servers)
577 for (const server of servers) {
578 const { data } = await server.videos.list()
580 const { id } = data.find(v => v.name === attributes.name)
581 const video = await server.videos.get({ id })
583 for (const resolution of [ 240, 360, 480, 720, 1080 ]) {
584 const file = video.files.find(f => f.resolution.id === resolution)
585 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
587 const bitrate = await getVideoFileBitrate(path)
588 const fps = await getVideoFileFPS(path)
589 const { videoFileResolution } = await getVideoFileResolution(path)
591 expect(videoFileResolution).to.equal(resolution)
592 expect(bitrate).to.be.below(getMaxBitrate(videoFileResolution, fps, VIDEO_TRANSCODING_FPS))
597 it('Should not transcode to an higher bitrate than the original file', async function () {
598 this.timeout(160_000)
612 webtorrent: { enabled: true },
613 hls: { enabled: true }
616 await servers[1].config.updateCustomSubConfig({ newConfig })
620 fixture: 'low-bitrate.mp4'
623 const { id } = await servers[1].videos.upload({ attributes })
625 await waitJobs(servers)
627 const video = await servers[1].videos.get({ id })
629 const resolutions = [ 240, 360, 480, 720, 1080 ]
630 for (const r of resolutions) {
631 const file = video.files.find(f => f.resolution.id === r)
633 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
634 const size = await getFileSize(path)
635 expect(size, `${path} not below ${60_000}`).to.be.below(60_000)
640 describe('FFprobe', function () {
642 it('Should provide valid ffprobe data', async function () {
643 this.timeout(160_000)
645 const videoUUID = (await servers[1].videos.quickUpload({ name: 'ffprobe data' })).uuid
646 await waitJobs(servers)
649 const video = await servers[1].videos.get({ id: videoUUID })
650 const file = video.files.find(f => f.resolution.id === 240)
651 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
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 videoDetails = await server.videos.get({ id: videoUUID })
683 const videoFiles = videoDetails.files
684 .concat(videoDetails.streamingPlaylists[0].files)
685 expect(videoFiles).to.have.lengthOf(8)
687 for (const file of videoFiles) {
688 expect(file.metadata).to.be.undefined
689 expect(file.metadataUrl).to.exist
690 expect(file.metadataUrl).to.contain(servers[1].url)
691 expect(file.metadataUrl).to.contain(videoUUID)
693 const metadata = await server.videos.getFileMetadata({ url: file.metadataUrl })
694 expect(metadata).to.have.nested.property('format.size')
699 it('Should correctly detect if quick transcode is possible', async function () {
702 expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.mp4'))).to.be.true
703 expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.webm'))).to.be.false
707 describe('Transcoding job queue', function () {
709 it('Should have the appropriate priorities for transcoding jobs', async function () {
710 const body = await servers[1].jobs.getJobsList({
714 jobType: 'video-transcoding'
717 const jobs = body.data
718 const transcodingJobs = jobs.filter(j => j.data.videoUUID === video4k)
720 expect(transcodingJobs).to.have.lengthOf(14)
722 const hlsJobs = transcodingJobs.filter(j => j.data.type === 'new-resolution-to-hls')
723 const webtorrentJobs = transcodingJobs.filter(j => j.data.type === 'new-resolution-to-webtorrent')
724 const optimizeJobs = transcodingJobs.filter(j => j.data.type === 'optimize-to-webtorrent')
726 expect(hlsJobs).to.have.lengthOf(7)
727 expect(webtorrentJobs).to.have.lengthOf(6)
728 expect(optimizeJobs).to.have.lengthOf(1)
730 for (const j of optimizeJobs.concat(hlsJobs.concat(webtorrentJobs))) {
731 expect(j.priority).to.be.greaterThan(100)
732 expect(j.priority).to.be.lessThan(150)
737 after(async function () {
738 await cleanupTests(servers)