aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-08-06 13:35:25 +0200
committerChocobozzz <me@florianbigard.com>2021-08-06 14:13:26 +0200
commit679c12e69c9f3a2d003ee3abe8b8da49f25b2bd3 (patch)
tree03abf589275db05e5b1fa1c89f57049cd807324a /server/tests
parentc826f34a45757b324a20f71665b44ed10e6953b5 (diff)
downloadPeerTube-679c12e69c9f3a2d003ee3abe8b8da49f25b2bd3.tar.gz
PeerTube-679c12e69c9f3a2d003ee3abe8b8da49f25b2bd3.tar.zst
PeerTube-679c12e69c9f3a2d003ee3abe8b8da49f25b2bd3.zip
Improve target bitrate calculation
Diffstat (limited to 'server/tests')
-rw-r--r--server/tests/api/live/live.ts2
-rw-r--r--server/tests/api/videos/video-transcoder.ts30
-rw-r--r--server/tests/cli/optimize-old-videos.ts20
-rw-r--r--server/tests/cli/print-transcode-command.ts10
-rw-r--r--server/tests/helpers/core-utils.ts38
5 files changed, 59 insertions, 41 deletions
diff --git a/server/tests/api/live/live.ts b/server/tests/api/live/live.ts
index 4095cdb1c..ba952aff5 100644
--- a/server/tests/api/live/live.ts
+++ b/server/tests/api/live/live.ts
@@ -3,7 +3,7 @@
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { basename, join } from 'path' 5import { basename, join } from 'path'
6import { ffprobePromise, getVideoFileBitrate, getVideoStreamFromFile } from '@server/helpers/ffprobe-utils' 6import { ffprobePromise, getVideoStreamFromFile } from '@server/helpers/ffprobe-utils'
7import { 7import {
8 checkLiveCleanupAfterSave, 8 checkLiveCleanupAfterSave,
9 checkLiveSegmentHash, 9 checkLiveSegmentHash,
diff --git a/server/tests/api/videos/video-transcoder.ts b/server/tests/api/videos/video-transcoder.ts
index 2a09e95bf..f67752d69 100644
--- a/server/tests/api/videos/video-transcoder.ts
+++ b/server/tests/api/videos/video-transcoder.ts
@@ -3,6 +3,7 @@
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { omit } from 'lodash' 5import { omit } from 'lodash'
6import { getMaxBitrate } from '@shared/core-utils'
6import { 7import {
7 buildAbsoluteFixturePath, 8 buildAbsoluteFixturePath,
8 cleanupTests, 9 cleanupTests,
@@ -17,8 +18,7 @@ import {
17 waitJobs, 18 waitJobs,
18 webtorrentAdd 19 webtorrentAdd
19} from '@shared/extra-utils' 20} from '@shared/extra-utils'
20import { getMaxBitrate, HttpStatusCode, VideoResolution, VideoState } from '@shared/models' 21import { HttpStatusCode, VideoState } from '@shared/models'
21import { VIDEO_TRANSCODING_FPS } from '../../../../server/initializers/constants'
22import { 22import {
23 canDoQuickTranscode, 23 canDoQuickTranscode,
24 getAudioStream, 24 getAudioStream,
@@ -191,15 +191,6 @@ describe('Test video transcoding', function () {
191 it('Should accept and transcode additional extensions', async function () { 191 it('Should accept and transcode additional extensions', async function () {
192 this.timeout(300_000) 192 this.timeout(300_000)
193 193
194 let tempFixturePath: string
195
196 {
197 tempFixturePath = await generateHighBitrateVideo()
198
199 const bitrate = await getVideoFileBitrate(tempFixturePath)
200 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
201 }
202
203 for (const fixture of [ 'video_short.mkv', 'video_short.avi' ]) { 194 for (const fixture of [ 'video_short.mkv', 'video_short.avi' ]) {
204 const attributes = { 195 const attributes = {
205 name: fixture, 196 name: fixture,
@@ -555,14 +546,7 @@ describe('Test video transcoding', function () {
555 it('Should respect maximum bitrate values', async function () { 546 it('Should respect maximum bitrate values', async function () {
556 this.timeout(160_000) 547 this.timeout(160_000)
557 548
558 let tempFixturePath: string 549 const tempFixturePath = await generateHighBitrateVideo()
559
560 {
561 tempFixturePath = await generateHighBitrateVideo()
562
563 const bitrate = await getVideoFileBitrate(tempFixturePath)
564 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
565 }
566 550
567 const attributes = { 551 const attributes = {
568 name: 'high bitrate video', 552 name: 'high bitrate video',
@@ -586,10 +570,12 @@ describe('Test video transcoding', function () {
586 570
587 const bitrate = await getVideoFileBitrate(path) 571 const bitrate = await getVideoFileBitrate(path)
588 const fps = await getVideoFileFPS(path) 572 const fps = await getVideoFileFPS(path)
589 const { videoFileResolution } = await getVideoFileResolution(path) 573 const dataResolution = await getVideoFileResolution(path)
574
575 expect(resolution).to.equal(resolution)
590 576
591 expect(videoFileResolution).to.equal(resolution) 577 const maxBitrate = getMaxBitrate({ ...dataResolution, fps })
592 expect(bitrate).to.be.below(getMaxBitrate(videoFileResolution, fps, VIDEO_TRANSCODING_FPS)) 578 expect(bitrate).to.be.below(maxBitrate)
593 } 579 }
594 } 580 }
595 }) 581 })
diff --git a/server/tests/cli/optimize-old-videos.ts b/server/tests/cli/optimize-old-videos.ts
index 579b2e7d8..9b75ae164 100644
--- a/server/tests/cli/optimize-old-videos.ts
+++ b/server/tests/cli/optimize-old-videos.ts
@@ -2,6 +2,7 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { getMaxBitrate } from '@shared/core-utils'
5import { 6import {
6 cleanupTests, 7 cleanupTests,
7 createMultipleServers, 8 createMultipleServers,
@@ -12,9 +13,7 @@ import {
12 wait, 13 wait,
13 waitJobs 14 waitJobs
14} from '@shared/extra-utils' 15} from '@shared/extra-utils'
15import { getMaxBitrate, VideoResolution } from '@shared/models'
16import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '../../helpers/ffprobe-utils' 16import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '../../helpers/ffprobe-utils'
17import { VIDEO_TRANSCODING_FPS } from '../../initializers/constants'
18 17
19const expect = chai.expect 18const expect = chai.expect
20 19
@@ -30,14 +29,7 @@ describe('Test optimize old videos', function () {
30 29
31 await doubleFollow(servers[0], servers[1]) 30 await doubleFollow(servers[0], servers[1])
32 31
33 let tempFixturePath: string 32 const tempFixturePath = await generateHighBitrateVideo()
34
35 {
36 tempFixturePath = await generateHighBitrateVideo()
37
38 const bitrate = await getVideoFileBitrate(tempFixturePath)
39 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
40 }
41 33
42 // Upload two videos for our needs 34 // Upload two videos for our needs
43 await servers[0].videos.upload({ attributes: { name: 'video1', fixture: tempFixturePath } }) 35 await servers[0].videos.upload({ attributes: { name: 'video1', fixture: tempFixturePath } })
@@ -88,10 +80,12 @@ describe('Test optimize old videos', function () {
88 const path = servers[0].servers.buildWebTorrentFilePath(file.fileUrl) 80 const path = servers[0].servers.buildWebTorrentFilePath(file.fileUrl)
89 const bitrate = await getVideoFileBitrate(path) 81 const bitrate = await getVideoFileBitrate(path)
90 const fps = await getVideoFileFPS(path) 82 const fps = await getVideoFileFPS(path)
91 const resolution = await getVideoFileResolution(path) 83 const data = await getVideoFileResolution(path)
84
85 expect(data.resolution).to.equal(file.resolution.id)
92 86
93 expect(resolution.videoFileResolution).to.equal(file.resolution.id) 87 const maxBitrate = getMaxBitrate({ ...data, fps })
94 expect(bitrate).to.be.below(getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)) 88 expect(bitrate).to.be.below(maxBitrate)
95 } 89 }
96 } 90 }
97 }) 91 })
diff --git a/server/tests/cli/print-transcode-command.ts b/server/tests/cli/print-transcode-command.ts
index 3a7969e68..e328a6072 100644
--- a/server/tests/cli/print-transcode-command.ts
+++ b/server/tests/cli/print-transcode-command.ts
@@ -3,16 +3,16 @@
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { getVideoFileBitrate, getVideoFileFPS } from '@server/helpers/ffprobe-utils' 5import { getVideoFileBitrate, getVideoFileFPS } from '@server/helpers/ffprobe-utils'
6import { CLICommand } from '@shared/extra-utils' 6import { getMaxBitrate } from '@shared/core-utils'
7import { getTargetBitrate, VideoResolution } from '../../../shared/models/videos' 7import { buildAbsoluteFixturePath, CLICommand } from '@shared/extra-utils'
8import { VIDEO_TRANSCODING_FPS } from '../../initializers/constants' 8import { VideoResolution } from '../../../shared/models/videos'
9 9
10const expect = chai.expect 10const expect = chai.expect
11 11
12describe('Test create transcoding jobs', function () { 12describe('Test create transcoding jobs', function () {
13 13
14 it('Should print the correct command for each resolution', async function () { 14 it('Should print the correct command for each resolution', async function () {
15 const fixturePath = 'server/tests/fixtures/video_short.webm' 15 const fixturePath = buildAbsoluteFixturePath('video_short.webm')
16 const fps = await getVideoFileFPS(fixturePath) 16 const fps = await getVideoFileFPS(fixturePath)
17 const bitrate = await getVideoFileBitrate(fixturePath) 17 const bitrate = await getVideoFileBitrate(fixturePath)
18 18
@@ -21,7 +21,7 @@ describe('Test create transcoding jobs', function () {
21 VideoResolution.H_1080P 21 VideoResolution.H_1080P
22 ]) { 22 ]) {
23 const command = await CLICommand.exec(`npm run print-transcode-command -- ${fixturePath} -r ${resolution}`) 23 const command = await CLICommand.exec(`npm run print-transcode-command -- ${fixturePath} -r ${resolution}`)
24 const targetBitrate = Math.min(getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS), bitrate) 24 const targetBitrate = Math.min(getMaxBitrate({ resolution, fps, ratio: 16 / 9 }), bitrate)
25 25
26 expect(command).to.includes(`-vf scale=w=-2:h=${resolution}`) 26 expect(command).to.includes(`-vf scale=w=-2:h=${resolution}`)
27 expect(command).to.includes(`-y -acodec aac -vcodec libx264`) 27 expect(command).to.includes(`-y -acodec aac -vcodec libx264`)
diff --git a/server/tests/helpers/core-utils.ts b/server/tests/helpers/core-utils.ts
index d5cac51a3..a6bf5b4c5 100644
--- a/server/tests/helpers/core-utils.ts
+++ b/server/tests/helpers/core-utils.ts
@@ -4,6 +4,8 @@ import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { snakeCase } from 'lodash' 5import { snakeCase } from 'lodash'
6import validator from 'validator' 6import validator from 'validator'
7import { getAverageBitrate, getMaxBitrate } from '@shared/core-utils'
8import { VideoResolution } from '@shared/models'
7import { objectConverter, parseBytes } from '../../helpers/core-utils' 9import { objectConverter, parseBytes } from '../../helpers/core-utils'
8 10
9const expect = chai.expect 11const expect = chai.expect
@@ -46,6 +48,9 @@ describe('Parse Bytes', function () {
46 it('Should be invalid when given invalid value', async function () { 48 it('Should be invalid when given invalid value', async function () {
47 expect(parseBytes('6GB 1GB')).to.be.eq(6) 49 expect(parseBytes('6GB 1GB')).to.be.eq(6)
48 }) 50 })
51})
52
53describe('Object', function () {
49 54
50 it('Should convert an object', async function () { 55 it('Should convert an object', async function () {
51 function keyConverter (k: string) { 56 function keyConverter (k: string) {
@@ -94,3 +99,36 @@ describe('Parse Bytes', function () {
94 expect(obj['my_super_key']).to.be.undefined 99 expect(obj['my_super_key']).to.be.undefined
95 }) 100 })
96}) 101})
102
103describe('Bitrate', function () {
104
105 it('Should get appropriate max bitrate', function () {
106 const tests = [
107 { resolution: VideoResolution.H_240P, ratio: 16 / 9, fps: 24, min: 600, max: 800 },
108 { resolution: VideoResolution.H_360P, ratio: 16 / 9, fps: 24, min: 1200, max: 1600 },
109 { resolution: VideoResolution.H_480P, ratio: 16 / 9, fps: 24, min: 2000, max: 2300 },
110 { resolution: VideoResolution.H_720P, ratio: 16 / 9, fps: 24, min: 4000, max: 4400 },
111 { resolution: VideoResolution.H_1080P, ratio: 16 / 9, fps: 24, min: 8000, max: 10000 },
112 { resolution: VideoResolution.H_4K, ratio: 16 / 9, fps: 24, min: 25000, max: 30000 }
113 ]
114
115 for (const test of tests) {
116 expect(getMaxBitrate(test)).to.be.above(test.min * 1000).and.below(test.max * 1000)
117 }
118 })
119
120 it('Should get appropriate average bitrate', function () {
121 const tests = [
122 { resolution: VideoResolution.H_240P, ratio: 16 / 9, fps: 24, min: 350, max: 450 },
123 { resolution: VideoResolution.H_360P, ratio: 16 / 9, fps: 24, min: 700, max: 900 },
124 { resolution: VideoResolution.H_480P, ratio: 16 / 9, fps: 24, min: 1100, max: 1300 },
125 { resolution: VideoResolution.H_720P, ratio: 16 / 9, fps: 24, min: 2300, max: 2500 },
126 { resolution: VideoResolution.H_1080P, ratio: 16 / 9, fps: 24, min: 4700, max: 5000 },
127 { resolution: VideoResolution.H_4K, ratio: 16 / 9, fps: 24, min: 15000, max: 17000 }
128 ]
129
130 for (const test of tests) {
131 expect(getAverageBitrate(test)).to.be.above(test.min * 1000).and.below(test.max * 1000)
132 }
133 })
134})