PeerTube/server/tests/shared/generate.ts

75 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-08-06 13:35:25 +02:00
import { expect } from 'chai'
2021-08-27 14:32:44 +02:00
import ffmpeg from 'fluent-ffmpeg'
2021-07-13 09:43:59 +02:00
import { ensureDir, pathExists } from 'fs-extra'
import { dirname } from 'path'
2023-05-05 10:53:04 +02:00
import { buildAbsoluteFixturePath, getMaxTheoreticalBitrate } from '@shared/core-utils'
2023-04-21 15:00:01 +02:00
import { getVideoStreamBitrate, getVideoStreamDimensionsInfo, getVideoStreamFPS } from '@shared/ffmpeg'
2021-07-13 09:43:59 +02:00
2021-08-06 13:35:25 +02:00
async function ensureHasTooBigBitrate (fixturePath: string) {
2022-02-11 10:51:33 +01:00
const bitrate = await getVideoStreamBitrate(fixturePath)
const dataResolution = await getVideoStreamDimensionsInfo(fixturePath)
const fps = await getVideoStreamFPS(fixturePath)
2021-08-06 13:35:25 +02:00
2023-05-05 10:53:04 +02:00
const maxBitrate = getMaxTheoreticalBitrate({ ...dataResolution, fps })
2021-08-06 13:35:25 +02:00
expect(bitrate).to.be.above(maxBitrate)
}
2021-07-13 09:43:59 +02:00
async function generateHighBitrateVideo () {
const tempFixturePath = buildAbsoluteFixturePath('video_high_bitrate_1080p.mp4', true)
await ensureDir(dirname(tempFixturePath))
const exists = await pathExists(tempFixturePath)
if (!exists) {
console.log('Generating high bitrate video.')
// Generate a random, high bitrate video on the fly, so we don't have to include
// a large file in the repo. The video needs to have a certain minimum length so
// that FFmpeg properly applies bitrate limits.
// https://stackoverflow.com/a/15795112
return new Promise<string>((res, rej) => {
ffmpeg()
.outputOptions([ '-f rawvideo', '-video_size 1920x1080', '-i /dev/urandom' ])
.outputOptions([ '-ac 2', '-f s16le', '-i /dev/urandom', '-t 10' ])
.outputOptions([ '-maxrate 10M', '-bufsize 10M' ])
.output(tempFixturePath)
.on('error', rej)
.on('end', () => res(tempFixturePath))
.run()
})
}
2021-08-06 13:35:25 +02:00
await ensureHasTooBigBitrate(tempFixturePath)
2021-07-13 09:43:59 +02:00
return tempFixturePath
}
async function generateVideoWithFramerate (fps = 60) {
const tempFixturePath = buildAbsoluteFixturePath(`video_${fps}fps.mp4`, true)
await ensureDir(dirname(tempFixturePath))
const exists = await pathExists(tempFixturePath)
if (!exists) {
console.log('Generating video with framerate %d.', fps)
return new Promise<string>((res, rej) => {
ffmpeg()
.outputOptions([ '-f rawvideo', '-video_size 1280x720', '-i /dev/urandom' ])
.outputOptions([ '-ac 2', '-f s16le', '-i /dev/urandom', '-t 10' ])
.outputOptions([ `-r ${fps}` ])
.output(tempFixturePath)
.on('error', rej)
.on('end', () => res(tempFixturePath))
.run()
})
}
return tempFixturePath
}
export {
generateHighBitrateVideo,
generateVideoWithFramerate
}