]>
Commit | Line | Data |
---|---|---|
679c12e6 | 1 | import { expect } from 'chai' |
41fb13c3 | 2 | import ffmpeg from 'fluent-ffmpeg' |
6c5065a0 C |
3 | import { ensureDir, pathExists } from 'fs-extra' |
4 | import { dirname } from 'path' | |
679c12e6 C |
5 | import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '@server/helpers/ffprobe-utils' |
6 | import { getMaxBitrate } from '@shared/core-utils' | |
6c5065a0 | 7 | import { buildAbsoluteFixturePath } from './tests' |
6c5065a0 | 8 | |
679c12e6 C |
9 | async function ensureHasTooBigBitrate (fixturePath: string) { |
10 | const bitrate = await getVideoFileBitrate(fixturePath) | |
11 | const dataResolution = await getVideoFileResolution(fixturePath) | |
12 | const fps = await getVideoFileFPS(fixturePath) | |
13 | ||
14 | const maxBitrate = getMaxBitrate({ ...dataResolution, fps }) | |
15 | expect(bitrate).to.be.above(maxBitrate) | |
16 | } | |
17 | ||
6c5065a0 C |
18 | async function generateHighBitrateVideo () { |
19 | const tempFixturePath = buildAbsoluteFixturePath('video_high_bitrate_1080p.mp4', true) | |
20 | ||
21 | await ensureDir(dirname(tempFixturePath)) | |
22 | ||
23 | const exists = await pathExists(tempFixturePath) | |
24 | if (!exists) { | |
25 | console.log('Generating high bitrate video.') | |
26 | ||
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) => { | |
32 | ffmpeg() | |
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) | |
37 | .on('error', rej) | |
38 | .on('end', () => res(tempFixturePath)) | |
39 | .run() | |
40 | }) | |
41 | } | |
42 | ||
679c12e6 C |
43 | await ensureHasTooBigBitrate(tempFixturePath) |
44 | ||
6c5065a0 C |
45 | return tempFixturePath |
46 | } | |
47 | ||
48 | async function generateVideoWithFramerate (fps = 60) { | |
49 | const tempFixturePath = buildAbsoluteFixturePath(`video_${fps}fps.mp4`, true) | |
50 | ||
51 | await ensureDir(dirname(tempFixturePath)) | |
52 | ||
53 | const exists = await pathExists(tempFixturePath) | |
54 | if (!exists) { | |
55 | console.log('Generating video with framerate %d.', fps) | |
56 | ||
57 | return new Promise<string>((res, rej) => { | |
58 | ffmpeg() | |
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) | |
63 | .on('error', rej) | |
64 | .on('end', () => res(tempFixturePath)) | |
65 | .run() | |
66 | }) | |
67 | } | |
68 | ||
69 | return tempFixturePath | |
70 | } | |
71 | ||
72 | export { | |
73 | generateHighBitrateVideo, | |
74 | generateVideoWithFramerate | |
75 | } |