diff options
author | Chocobozzz <me@florianbigard.com> | 2023-07-31 14:34:36 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-08-11 15:02:33 +0200 |
commit | 3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch) | |
tree | e4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/lib/transcoding/default-transcoding-profiles.ts | |
parent | 04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff) | |
download | PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip |
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge
conflicts, but it's a major step forward:
* Server can be faster at startup because imports() are async and we can
easily lazy import big modules
* Angular doesn't seem to support ES import (with .js extension), so we
had to correctly organize peertube into a monorepo:
* Use yarn workspace feature
* Use typescript reference projects for dependencies
* Shared projects have been moved into "packages", each one is now a
node module (with a dedicated package.json/tsconfig.json)
* server/tools have been moved into apps/ and is now a dedicated app
bundled and published on NPM so users don't have to build peertube
cli tools manually
* server/tests have been moved into packages/ so we don't compile
them every time we want to run the server
* Use isolatedModule option:
* Had to move from const enum to const
(https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums)
* Had to explictely specify "type" imports when used in decorators
* Prefer tsx (that uses esbuild under the hood) instead of ts-node to
load typescript files (tests with mocha or scripts):
* To reduce test complexity as esbuild doesn't support decorator
metadata, we only test server files that do not import server
models
* We still build tests files into js files for a faster CI
* Remove unmaintained peertube CLI import script
* Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'server/lib/transcoding/default-transcoding-profiles.ts')
-rw-r--r-- | server/lib/transcoding/default-transcoding-profiles.ts | 143 |
1 files changed, 0 insertions, 143 deletions
diff --git a/server/lib/transcoding/default-transcoding-profiles.ts b/server/lib/transcoding/default-transcoding-profiles.ts deleted file mode 100644 index 8f8fdd026..000000000 --- a/server/lib/transcoding/default-transcoding-profiles.ts +++ /dev/null | |||
@@ -1,143 +0,0 @@ | |||
1 | |||
2 | import { logger } from '@server/helpers/logger' | ||
3 | import { FFmpegCommandWrapper, getDefaultAvailableEncoders } from '@shared/ffmpeg' | ||
4 | import { AvailableEncoders, EncoderOptionsBuilder } from '@shared/models' | ||
5 | |||
6 | // --------------------------------------------------------------------------- | ||
7 | // Profile manager to get and change default profiles | ||
8 | // --------------------------------------------------------------------------- | ||
9 | |||
10 | class VideoTranscodingProfilesManager { | ||
11 | private static instance: VideoTranscodingProfilesManager | ||
12 | |||
13 | // 1 === less priority | ||
14 | private readonly encodersPriorities = { | ||
15 | vod: this.buildDefaultEncodersPriorities(), | ||
16 | live: this.buildDefaultEncodersPriorities() | ||
17 | } | ||
18 | |||
19 | private readonly availableEncoders = getDefaultAvailableEncoders() | ||
20 | |||
21 | private availableProfiles = { | ||
22 | vod: [] as string[], | ||
23 | live: [] as string[] | ||
24 | } | ||
25 | |||
26 | private constructor () { | ||
27 | this.buildAvailableProfiles() | ||
28 | } | ||
29 | |||
30 | getAvailableEncoders (): AvailableEncoders { | ||
31 | return { | ||
32 | available: this.availableEncoders, | ||
33 | encodersToTry: { | ||
34 | vod: { | ||
35 | video: this.getEncodersByPriority('vod', 'video'), | ||
36 | audio: this.getEncodersByPriority('vod', 'audio') | ||
37 | }, | ||
38 | live: { | ||
39 | video: this.getEncodersByPriority('live', 'video'), | ||
40 | audio: this.getEncodersByPriority('live', 'audio') | ||
41 | } | ||
42 | } | ||
43 | } | ||
44 | } | ||
45 | |||
46 | getAvailableProfiles (type: 'vod' | 'live') { | ||
47 | return this.availableProfiles[type] | ||
48 | } | ||
49 | |||
50 | addProfile (options: { | ||
51 | type: 'vod' | 'live' | ||
52 | encoder: string | ||
53 | profile: string | ||
54 | builder: EncoderOptionsBuilder | ||
55 | }) { | ||
56 | const { type, encoder, profile, builder } = options | ||
57 | |||
58 | const encoders = this.availableEncoders[type] | ||
59 | |||
60 | if (!encoders[encoder]) encoders[encoder] = {} | ||
61 | encoders[encoder][profile] = builder | ||
62 | |||
63 | this.buildAvailableProfiles() | ||
64 | } | ||
65 | |||
66 | removeProfile (options: { | ||
67 | type: 'vod' | 'live' | ||
68 | encoder: string | ||
69 | profile: string | ||
70 | }) { | ||
71 | const { type, encoder, profile } = options | ||
72 | |||
73 | delete this.availableEncoders[type][encoder][profile] | ||
74 | this.buildAvailableProfiles() | ||
75 | } | ||
76 | |||
77 | addEncoderPriority (type: 'vod' | 'live', streamType: 'audio' | 'video', encoder: string, priority: number) { | ||
78 | this.encodersPriorities[type][streamType].push({ name: encoder, priority }) | ||
79 | |||
80 | FFmpegCommandWrapper.resetSupportedEncoders() | ||
81 | } | ||
82 | |||
83 | removeEncoderPriority (type: 'vod' | 'live', streamType: 'audio' | 'video', encoder: string, priority: number) { | ||
84 | this.encodersPriorities[type][streamType] = this.encodersPriorities[type][streamType] | ||
85 | .filter(o => o.name !== encoder && o.priority !== priority) | ||
86 | |||
87 | FFmpegCommandWrapper.resetSupportedEncoders() | ||
88 | } | ||
89 | |||
90 | private getEncodersByPriority (type: 'vod' | 'live', streamType: 'audio' | 'video') { | ||
91 | return this.encodersPriorities[type][streamType] | ||
92 | .sort((e1, e2) => { | ||
93 | if (e1.priority > e2.priority) return -1 | ||
94 | else if (e1.priority === e2.priority) return 0 | ||
95 | |||
96 | return 1 | ||
97 | }) | ||
98 | .map(e => e.name) | ||
99 | } | ||
100 | |||
101 | private buildAvailableProfiles () { | ||
102 | for (const type of [ 'vod', 'live' ]) { | ||
103 | const result = new Set() | ||
104 | |||
105 | const encoders = this.availableEncoders[type] | ||
106 | |||
107 | for (const encoderName of Object.keys(encoders)) { | ||
108 | for (const profile of Object.keys(encoders[encoderName])) { | ||
109 | result.add(profile) | ||
110 | } | ||
111 | } | ||
112 | |||
113 | this.availableProfiles[type] = Array.from(result) | ||
114 | } | ||
115 | |||
116 | logger.debug('Available transcoding profiles built.', { availableProfiles: this.availableProfiles }) | ||
117 | } | ||
118 | |||
119 | private buildDefaultEncodersPriorities () { | ||
120 | return { | ||
121 | video: [ | ||
122 | { name: 'libx264', priority: 100 } | ||
123 | ], | ||
124 | |||
125 | // Try the first one, if not available try the second one etc | ||
126 | audio: [ | ||
127 | // we favor VBR, if a good AAC encoder is available | ||
128 | { name: 'libfdk_aac', priority: 200 }, | ||
129 | { name: 'aac', priority: 100 } | ||
130 | ] | ||
131 | } | ||
132 | } | ||
133 | |||
134 | static get Instance () { | ||
135 | return this.instance || (this.instance = new this()) | ||
136 | } | ||
137 | } | ||
138 | |||
139 | // --------------------------------------------------------------------------- | ||
140 | |||
141 | export { | ||
142 | VideoTranscodingProfilesManager | ||
143 | } | ||