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 { canDoQuickTranscode } from '@server/helpers/ffmpeg'
7 import { generateHighBitrateVideo, generateVideoWithFramerate, getAllFiles } from '@server/tests/shared'
8 import { buildAbsoluteFixturePath, getMaxBitrate, getMinLimitBitrate } from '@shared/core-utils'
12 getVideoStreamBitrate,
14 getVideoStreamDimensionsInfo,
16 } from '@shared/extra-utils'
17 import { HttpStatusCode, VideoState } from '@shared/models'
20 createMultipleServers,
24 setAccessTokensToServers,
27 } from '@shared/server-commands'
29 const expect = chai.expect
31 function updateConfigForTranscoding (server: PeerTubeServer) {
32 return server.config.updateCustomSubConfig({
36 allowAdditionalExtensions: true,
37 allowAudioFiles: true,
38 hls: { enabled: true },
39 webtorrent: { enabled: true },
56 describe('Test video transcoding', function () {
57 let servers: PeerTubeServer[] = []
60 before(async function () {
64 servers = await createMultipleServers(2)
66 await setAccessTokensToServers(servers)
68 await doubleFollow(servers[0], servers[1])
70 await updateConfigForTranscoding(servers[1])
73 describe('Basic transcoding (or not)', function () {
75 it('Should not transcode video on server 1', async function () {
79 name: 'my super name for server 1',
80 description: 'my super description for server 1',
81 fixture: 'video_short.webm'
83 await servers[0].videos.upload({ attributes })
85 await waitJobs(servers)
87 for (const server of servers) {
88 const { data } = await server.videos.list()
91 const videoDetails = await server.videos.get({ id: video.id })
92 expect(videoDetails.files).to.have.lengthOf(1)
94 const magnetUri = videoDetails.files[0].magnetUri
95 expect(magnetUri).to.match(/\.webm/)
97 const torrent = await webtorrentAdd(magnetUri, true)
98 expect(torrent.files).to.be.an('array')
99 expect(torrent.files.length).to.equal(1)
100 expect(torrent.files[0].path).match(/\.webm$/)
104 it('Should transcode video on server 2', async function () {
105 this.timeout(120_000)
108 name: 'my super name for server 2',
109 description: 'my super description for server 2',
110 fixture: 'video_short.webm'
112 await servers[1].videos.upload({ attributes })
114 await waitJobs(servers)
116 for (const server of servers) {
117 const { data } = await server.videos.list()
119 const video = data.find(v => v.name === attributes.name)
120 const videoDetails = await server.videos.get({ id: video.id })
122 expect(videoDetails.files).to.have.lengthOf(5)
124 const magnetUri = videoDetails.files[0].magnetUri
125 expect(magnetUri).to.match(/\.mp4/)
127 const torrent = await webtorrentAdd(magnetUri, true)
128 expect(torrent.files).to.be.an('array')
129 expect(torrent.files.length).to.equal(1)
130 expect(torrent.files[0].path).match(/\.mp4$/)
134 it('Should wait for transcoding before publishing the video', async function () {
135 this.timeout(160_000)
138 // Upload the video, but wait transcoding
140 name: 'waiting video',
141 fixture: 'video_short1.webm',
142 waitTranscoding: true
144 const { uuid } = await servers[1].videos.upload({ attributes })
147 // Should be in transcode state
148 const body = await servers[1].videos.get({ id: videoId })
149 expect(body.name).to.equal('waiting video')
150 expect(body.state.id).to.equal(VideoState.TO_TRANSCODE)
151 expect(body.state.label).to.equal('To transcode')
152 expect(body.waitTranscoding).to.be.true
155 // Should have my video
156 const { data } = await servers[1].videos.listMyVideos()
157 const videoToFindInMine = data.find(v => v.name === attributes.name)
158 expect(videoToFindInMine).not.to.be.undefined
159 expect(videoToFindInMine.state.id).to.equal(VideoState.TO_TRANSCODE)
160 expect(videoToFindInMine.state.label).to.equal('To transcode')
161 expect(videoToFindInMine.waitTranscoding).to.be.true
165 // Should not list this video
166 const { data } = await servers[1].videos.list()
167 const videoToFindInList = data.find(v => v.name === attributes.name)
168 expect(videoToFindInList).to.be.undefined
171 // Server 1 should not have the video yet
172 await servers[0].videos.get({ id: videoId, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
175 await waitJobs(servers)
177 for (const server of servers) {
178 const { data } = await server.videos.list()
179 const videoToFind = data.find(v => v.name === 'waiting video')
180 expect(videoToFind).not.to.be.undefined
182 const videoDetails = await server.videos.get({ id: videoToFind.id })
184 expect(videoDetails.state.id).to.equal(VideoState.PUBLISHED)
185 expect(videoDetails.state.label).to.equal('Published')
186 expect(videoDetails.waitTranscoding).to.be.true
190 it('Should accept and transcode additional extensions', async function () {
191 this.timeout(300_000)
193 for (const fixture of [ 'video_short.mkv', 'video_short.avi' ]) {
199 await servers[1].videos.upload({ attributes })
201 await waitJobs(servers)
203 for (const server of servers) {
204 const { data } = await server.videos.list()
206 const video = data.find(v => v.name === attributes.name)
207 const videoDetails = await server.videos.get({ id: video.id })
208 expect(videoDetails.files).to.have.lengthOf(5)
210 const magnetUri = videoDetails.files[0].magnetUri
211 expect(magnetUri).to.contain('.mp4')
216 it('Should transcode a 4k video', async function () {
217 this.timeout(200_000)
221 fixture: 'video_short_4k.mp4'
224 const { uuid } = await servers[1].videos.upload({ attributes })
227 await waitJobs(servers)
229 const resolutions = [ 144, 240, 360, 480, 720, 1080, 1440, 2160 ]
231 for (const server of servers) {
232 const videoDetails = await server.videos.get({ id: video4k })
233 expect(videoDetails.files).to.have.lengthOf(resolutions.length)
235 for (const r of resolutions) {
236 expect(videoDetails.files.find(f => f.resolution.id === r)).to.not.be.undefined
237 expect(videoDetails.streamingPlaylists[0].files.find(f => f.resolution.id === r)).to.not.be.undefined
243 describe('Audio transcoding', function () {
245 it('Should transcode high bit rate mp3 to proper bit rate', async function () {
250 fixture: 'video_short_mp3_256k.mp4'
252 await servers[1].videos.upload({ attributes })
254 await waitJobs(servers)
256 for (const server of servers) {
257 const { data } = await server.videos.list()
259 const video = data.find(v => v.name === attributes.name)
260 const videoDetails = await server.videos.get({ id: video.id })
262 expect(videoDetails.files).to.have.lengthOf(5)
264 const file = videoDetails.files.find(f => f.resolution.id === 240)
265 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
266 const probe = await getAudioStream(path)
268 if (probe.audioStream) {
269 expect(probe.audioStream['codec_name']).to.be.equal('aac')
270 expect(probe.audioStream['bit_rate']).to.be.at.most(384 * 8000)
272 this.fail('Could not retrieve the audio stream on ' + probe.absolutePath)
277 it('Should transcode video with no audio and have no audio itself', async function () {
282 fixture: 'video_short_no_audio.mp4'
284 await servers[1].videos.upload({ attributes })
286 await waitJobs(servers)
288 for (const server of servers) {
289 const { data } = await server.videos.list()
291 const video = data.find(v => v.name === attributes.name)
292 const videoDetails = await server.videos.get({ id: video.id })
294 const file = videoDetails.files.find(f => f.resolution.id === 240)
295 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
297 expect(await hasAudioStream(path)).to.be.false
301 it('Should leave the audio untouched, but properly transcode the video', async function () {
305 name: 'untouched_audio',
306 fixture: 'video_short.mp4'
308 await servers[1].videos.upload({ attributes })
310 await waitJobs(servers)
312 for (const server of servers) {
313 const { data } = await server.videos.list()
315 const video = data.find(v => v.name === attributes.name)
316 const videoDetails = await server.videos.get({ id: video.id })
318 expect(videoDetails.files).to.have.lengthOf(5)
320 const fixturePath = buildAbsoluteFixturePath(attributes.fixture)
321 const fixtureVideoProbe = await getAudioStream(fixturePath)
323 const file = videoDetails.files.find(f => f.resolution.id === 240)
324 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
326 const videoProbe = await getAudioStream(path)
328 if (videoProbe.audioStream && fixtureVideoProbe.audioStream) {
329 const toOmit = [ 'max_bit_rate', 'duration', 'duration_ts', 'nb_frames', 'start_time', 'start_pts' ]
330 expect(omit(videoProbe.audioStream, toOmit)).to.be.deep.equal(omit(fixtureVideoProbe.audioStream, toOmit))
332 this.fail('Could not retrieve the audio stream on ' + videoProbe.absolutePath)
338 describe('Audio upload', function () {
340 function runSuite (mode: 'legacy' | 'resumable') {
342 before(async function () {
343 await servers[1].config.updateCustomSubConfig({
346 hls: { enabled: true },
347 webtorrent: { enabled: true },
364 it('Should merge an audio file with the preview file', async function () {
367 const attributes = { name: 'audio_with_preview', previewfile: 'preview.jpg', fixture: 'sample.ogg' }
368 await servers[1].videos.upload({ attributes, mode })
370 await waitJobs(servers)
372 for (const server of servers) {
373 const { data } = await server.videos.list()
375 const video = data.find(v => v.name === 'audio_with_preview')
376 const videoDetails = await server.videos.get({ id: video.id })
378 expect(videoDetails.files).to.have.lengthOf(1)
380 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 })
381 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, expectedStatus: HttpStatusCode.OK_200 })
383 const magnetUri = videoDetails.files[0].magnetUri
384 expect(magnetUri).to.contain('.mp4')
388 it('Should upload an audio file and choose a default background image', async function () {
391 const attributes = { name: 'audio_without_preview', fixture: 'sample.ogg' }
392 await servers[1].videos.upload({ attributes, mode })
394 await waitJobs(servers)
396 for (const server of servers) {
397 const { data } = await server.videos.list()
399 const video = data.find(v => v.name === 'audio_without_preview')
400 const videoDetails = await server.videos.get({ id: video.id })
402 expect(videoDetails.files).to.have.lengthOf(1)
404 await makeGetRequest({ url: server.url, path: videoDetails.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 })
405 await makeGetRequest({ url: server.url, path: videoDetails.previewPath, expectedStatus: HttpStatusCode.OK_200 })
407 const magnetUri = videoDetails.files[0].magnetUri
408 expect(magnetUri).to.contain('.mp4')
412 it('Should upload an audio file and create an audio version only', async function () {
415 await servers[1].config.updateCustomSubConfig({
418 hls: { enabled: true },
419 webtorrent: { enabled: true },
430 const attributes = { name: 'audio_with_preview', previewfile: 'preview.jpg', fixture: 'sample.ogg' }
431 const { id } = await servers[1].videos.upload({ attributes, mode })
433 await waitJobs(servers)
435 for (const server of servers) {
436 const videoDetails = await server.videos.get({ id })
438 for (const files of [ videoDetails.files, videoDetails.streamingPlaylists[0].files ]) {
439 expect(files).to.have.lengthOf(2)
440 expect(files.find(f => f.resolution.id === 0)).to.not.be.undefined
444 await updateConfigForTranscoding(servers[1])
448 describe('Legacy upload', function () {
452 describe('Resumable upload', function () {
453 runSuite('resumable')
457 describe('Framerate', function () {
459 it('Should transcode a 60 FPS video', async function () {
463 name: 'my super 30fps name for server 2',
464 description: 'my super 30fps description for server 2',
465 fixture: '60fps_720p_small.mp4'
467 await servers[1].videos.upload({ attributes })
469 await waitJobs(servers)
471 for (const server of servers) {
472 const { data } = await server.videos.list()
474 const video = data.find(v => v.name === attributes.name)
475 const videoDetails = await server.videos.get({ id: video.id })
477 expect(videoDetails.files).to.have.lengthOf(5)
478 expect(videoDetails.files[0].fps).to.be.above(58).and.below(62)
479 expect(videoDetails.files[1].fps).to.be.below(31)
480 expect(videoDetails.files[2].fps).to.be.below(31)
481 expect(videoDetails.files[3].fps).to.be.below(31)
482 expect(videoDetails.files[4].fps).to.be.below(31)
484 for (const resolution of [ 144, 240, 360, 480 ]) {
485 const file = videoDetails.files.find(f => f.resolution.id === resolution)
486 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
487 const fps = await getVideoStreamFPS(path)
489 expect(fps).to.be.below(31)
492 const file = videoDetails.files.find(f => f.resolution.id === 720)
493 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
494 const fps = await getVideoStreamFPS(path)
496 expect(fps).to.be.above(58).and.below(62)
500 it('Should downscale to the closest divisor standard framerate', async function () {
501 this.timeout(200_000)
503 let tempFixturePath: string
506 tempFixturePath = await generateVideoWithFramerate(59)
508 const fps = await getVideoStreamFPS(tempFixturePath)
509 expect(fps).to.be.equal(59)
514 description: '59fps video',
515 fixture: tempFixturePath
518 await servers[1].videos.upload({ attributes })
520 await waitJobs(servers)
522 for (const server of servers) {
523 const { data } = await server.videos.list()
525 const { id } = data.find(v => v.name === attributes.name)
526 const video = await server.videos.get({ id })
529 const file = video.files.find(f => f.resolution.id === 240)
530 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
531 const fps = await getVideoStreamFPS(path)
532 expect(fps).to.be.equal(25)
536 const file = video.files.find(f => f.resolution.id === 720)
537 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
538 const fps = await getVideoStreamFPS(path)
539 expect(fps).to.be.equal(59)
545 describe('Bitrate control', function () {
547 it('Should respect maximum bitrate values', async function () {
548 this.timeout(160_000)
550 const tempFixturePath = await generateHighBitrateVideo()
553 name: 'high bitrate video',
554 description: 'high bitrate video',
555 fixture: tempFixturePath
558 await servers[1].videos.upload({ attributes })
560 await waitJobs(servers)
562 for (const server of servers) {
563 const { data } = await server.videos.list()
565 const { id } = data.find(v => v.name === attributes.name)
566 const video = await server.videos.get({ id })
568 for (const resolution of [ 240, 360, 480, 720, 1080 ]) {
569 const file = video.files.find(f => f.resolution.id === resolution)
570 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
572 const bitrate = await getVideoStreamBitrate(path)
573 const fps = await getVideoStreamFPS(path)
574 const dataResolution = await getVideoStreamDimensionsInfo(path)
576 expect(resolution).to.equal(resolution)
578 const maxBitrate = getMaxBitrate({ ...dataResolution, fps })
579 expect(bitrate).to.be.below(maxBitrate)
584 it('Should not transcode to an higher bitrate than the original file but above our low limit', async function () {
585 this.timeout(160_000)
600 webtorrent: { enabled: true },
601 hls: { enabled: true }
604 await servers[1].config.updateCustomSubConfig({ newConfig })
608 fixture: 'low-bitrate.mp4'
611 const { id } = await servers[1].videos.upload({ attributes })
613 await waitJobs(servers)
615 const video = await servers[1].videos.get({ id })
617 const resolutions = [ 240, 360, 480, 720, 1080 ]
618 for (const r of resolutions) {
619 const file = video.files.find(f => f.resolution.id === r)
621 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
622 const bitrate = await getVideoStreamBitrate(path)
624 const inputBitrate = 60_000
625 const limit = getMinLimitBitrate({ fps: 10, ratio: 1, resolution: r })
626 let belowValue = Math.max(inputBitrate, limit)
627 belowValue += belowValue * 0.20 // Apply 20% margin because bitrate control is not very precise
629 expect(bitrate, `${path} not below ${limit}`).to.be.below(belowValue)
634 describe('FFprobe', function () {
636 it('Should provide valid ffprobe data', async function () {
637 this.timeout(160_000)
639 const videoUUID = (await servers[1].videos.quickUpload({ name: 'ffprobe data' })).uuid
640 await waitJobs(servers)
643 const video = await servers[1].videos.get({ id: videoUUID })
644 const file = video.files.find(f => f.resolution.id === 240)
645 const path = servers[1].servers.buildWebTorrentFilePath(file.fileUrl)
646 const metadata = await buildFileMetadata(path)
648 // expected format properties
655 expect(metadata.format).to.have.nested.property(p)
658 // expected stream properties
664 'display_aspect_ratio',
668 expect(metadata.streams[0]).to.have.nested.property(p)
671 expect(metadata).to.not.have.nested.property('format.filename')
674 for (const server of servers) {
675 const videoDetails = await server.videos.get({ id: videoUUID })
677 const videoFiles = getAllFiles(videoDetails)
678 expect(videoFiles).to.have.lengthOf(10)
680 for (const file of videoFiles) {
681 expect(file.metadata).to.be.undefined
682 expect(file.metadataUrl).to.exist
683 expect(file.metadataUrl).to.contain(servers[1].url)
684 expect(file.metadataUrl).to.contain(videoUUID)
686 const metadata = await server.videos.getFileMetadata({ url: file.metadataUrl })
687 expect(metadata).to.have.nested.property('format.size')
692 it('Should correctly detect if quick transcode is possible', async function () {
695 expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.mp4'))).to.be.true
696 expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.webm'))).to.be.false
700 describe('Transcoding job queue', function () {
702 it('Should have the appropriate priorities for transcoding jobs', async function () {
703 const body = await servers[1].jobs.list({
707 jobType: 'video-transcoding'
710 const jobs = body.data
711 const transcodingJobs = jobs.filter(j => j.data.videoUUID === video4k)
713 expect(transcodingJobs).to.have.lengthOf(16)
715 const hlsJobs = transcodingJobs.filter(j => j.data.type === 'new-resolution-to-hls')
716 const webtorrentJobs = transcodingJobs.filter(j => j.data.type === 'new-resolution-to-webtorrent')
717 const optimizeJobs = transcodingJobs.filter(j => j.data.type === 'optimize-to-webtorrent')
719 expect(hlsJobs).to.have.lengthOf(8)
720 expect(webtorrentJobs).to.have.lengthOf(7)
721 expect(optimizeJobs).to.have.lengthOf(1)
723 for (const j of optimizeJobs.concat(hlsJobs.concat(webtorrentJobs))) {
724 expect(j.priority).to.be.greaterThan(100)
725 expect(j.priority).to.be.lessThan(150)
730 after(async function () {
731 await cleanupTests(servers)