diff options
author | Chocobozzz <me@florianbigard.com> | 2021-12-17 09:29:23 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-12-17 09:29:23 +0100 |
commit | bf54587a3e2ad9c2c186828f2a5682b91ee2cc00 (patch) | |
tree | 54b40aaf01bae210632473285c3c7571d51e4f89 /shared/server-commands/miscs/generate.ts | |
parent | 6b5f72beda96d8b7e4d6329c4001827334de27dd (diff) | |
download | PeerTube-bf54587a3e2ad9c2c186828f2a5682b91ee2cc00.tar.gz PeerTube-bf54587a3e2ad9c2c186828f2a5682b91ee2cc00.tar.zst PeerTube-bf54587a3e2ad9c2c186828f2a5682b91ee2cc00.zip |
shared/ typescript types dir server-commands
Diffstat (limited to 'shared/server-commands/miscs/generate.ts')
-rw-r--r-- | shared/server-commands/miscs/generate.ts | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/shared/server-commands/miscs/generate.ts b/shared/server-commands/miscs/generate.ts new file mode 100644 index 000000000..93673a063 --- /dev/null +++ b/shared/server-commands/miscs/generate.ts | |||
@@ -0,0 +1,75 @@ | |||
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' | ||
8 | |||
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 | |||
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 | |||
43 | await ensureHasTooBigBitrate(tempFixturePath) | ||
44 | |||
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 | } | ||