1 import { expect } from 'chai'
2 import ffmpeg from 'fluent-ffmpeg'
3 import { ensureDir, pathExists } from 'fs-extra'
4 import { dirname } from 'path'
5 import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '@shared/extra-utils/ffprobe'
6 import { getMaxBitrate } from '@shared/core-utils'
7 import { buildAbsoluteFixturePath } from './tests'
9 async function ensureHasTooBigBitrate (fixturePath: string) {
10 const bitrate = await getVideoFileBitrate(fixturePath)
11 const dataResolution = await getVideoFileResolution(fixturePath)
12 const fps = await getVideoFileFPS(fixturePath)
14 const maxBitrate = getMaxBitrate({ ...dataResolution, fps })
15 expect(bitrate).to.be.above(maxBitrate)
18 async function generateHighBitrateVideo () {
19 const tempFixturePath = buildAbsoluteFixturePath('video_high_bitrate_1080p.mp4', true)
21 await ensureDir(dirname(tempFixturePath))
23 const exists = await pathExists(tempFixturePath)
25 console.log('Generating high bitrate video.')
27 // Generate a random, high bitrate video on the fly, so we don't have to include
28 // a large file in the repo. The video needs to have a certain minimum length so
29 // that FFmpeg properly applies bitrate limits.
30 // https://stackoverflow.com/a/15795112
31 return new Promise<string>((res, rej) => {
33 .outputOptions([ '-f rawvideo', '-video_size 1920x1080', '-i /dev/urandom' ])
34 .outputOptions([ '-ac 2', '-f s16le', '-i /dev/urandom', '-t 10' ])
35 .outputOptions([ '-maxrate 10M', '-bufsize 10M' ])
36 .output(tempFixturePath)
38 .on('end', () => res(tempFixturePath))
43 await ensureHasTooBigBitrate(tempFixturePath)
45 return tempFixturePath
48 async function generateVideoWithFramerate (fps = 60) {
49 const tempFixturePath = buildAbsoluteFixturePath(`video_${fps}fps.mp4`, true)
51 await ensureDir(dirname(tempFixturePath))
53 const exists = await pathExists(tempFixturePath)
55 console.log('Generating video with framerate %d.', fps)
57 return new Promise<string>((res, rej) => {
59 .outputOptions([ '-f rawvideo', '-video_size 1280x720', '-i /dev/urandom' ])
60 .outputOptions([ '-ac 2', '-f s16le', '-i /dev/urandom', '-t 10' ])
61 .outputOptions([ `-r ${fps}` ])
62 .output(tempFixturePath)
64 .on('end', () => res(tempFixturePath))
69 return tempFixturePath
73 generateHighBitrateVideo,
74 generateVideoWithFramerate