aboutsummaryrefslogtreecommitdiffhomepage
path: root/packages/ffmpeg/src/ffmpeg-default-transcoding-profile.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-31 14:34:36 +0200
committerChocobozzz <me@florianbigard.com>2023-08-11 15:02:33 +0200
commit3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch)
treee4510b39bdac9c318fdb4b47018d08f15368b8f0 /packages/ffmpeg/src/ffmpeg-default-transcoding-profile.ts
parent04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff)
downloadPeerTube-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 'packages/ffmpeg/src/ffmpeg-default-transcoding-profile.ts')
-rw-r--r--packages/ffmpeg/src/ffmpeg-default-transcoding-profile.ts187
1 files changed, 187 insertions, 0 deletions
diff --git a/packages/ffmpeg/src/ffmpeg-default-transcoding-profile.ts b/packages/ffmpeg/src/ffmpeg-default-transcoding-profile.ts
new file mode 100644
index 000000000..0d3538512
--- /dev/null
+++ b/packages/ffmpeg/src/ffmpeg-default-transcoding-profile.ts
@@ -0,0 +1,187 @@
1import { FfprobeData } from 'fluent-ffmpeg'
2import { getAverageTheoreticalBitrate, getMaxTheoreticalBitrate, getMinTheoreticalBitrate } from '@peertube/peertube-core-utils'
3import {
4 buildStreamSuffix,
5 ffprobePromise,
6 getAudioStream,
7 getMaxAudioBitrate,
8 getVideoStream,
9 getVideoStreamBitrate,
10 getVideoStreamDimensionsInfo,
11 getVideoStreamFPS
12} from '@peertube/peertube-ffmpeg'
13import { EncoderOptionsBuilder, EncoderOptionsBuilderParams } from '@peertube/peertube-models'
14
15const defaultX264VODOptionsBuilder: EncoderOptionsBuilder = (options: EncoderOptionsBuilderParams) => {
16 const { fps, inputRatio, inputBitrate, resolution } = options
17
18 const targetBitrate = getTargetBitrate({ inputBitrate, ratio: inputRatio, fps, resolution })
19
20 return {
21 outputOptions: [
22 ...getCommonOutputOptions(targetBitrate),
23
24 `-r ${fps}`
25 ]
26 }
27}
28
29const defaultX264LiveOptionsBuilder: EncoderOptionsBuilder = (options: EncoderOptionsBuilderParams) => {
30 const { streamNum, fps, inputBitrate, inputRatio, resolution } = options
31
32 const targetBitrate = getTargetBitrate({ inputBitrate, ratio: inputRatio, fps, resolution })
33
34 return {
35 outputOptions: [
36 ...getCommonOutputOptions(targetBitrate, streamNum),
37
38 `${buildStreamSuffix('-r:v', streamNum)} ${fps}`,
39 `${buildStreamSuffix('-b:v', streamNum)} ${targetBitrate}`
40 ]
41 }
42}
43
44const defaultAACOptionsBuilder: EncoderOptionsBuilder = async ({ input, streamNum, canCopyAudio }) => {
45 const probe = await ffprobePromise(input)
46
47 if (canCopyAudio && await canDoQuickAudioTranscode(input, probe)) {
48 return { copy: true, outputOptions: [ ] }
49 }
50
51 const parsedAudio = await getAudioStream(input, probe)
52
53 // We try to reduce the ceiling bitrate by making rough matches of bitrates
54 // Of course this is far from perfect, but it might save some space in the end
55
56 const audioCodecName = parsedAudio.audioStream['codec_name']
57
58 const bitrate = getMaxAudioBitrate(audioCodecName, parsedAudio.bitrate)
59
60 // Force stereo as it causes some issues with HLS playback in Chrome
61 const base = [ '-channel_layout', 'stereo' ]
62
63 if (bitrate !== -1) {
64 return { outputOptions: base.concat([ buildStreamSuffix('-b:a', streamNum), bitrate + 'k' ]) }
65 }
66
67 return { outputOptions: base }
68}
69
70const defaultLibFDKAACVODOptionsBuilder: EncoderOptionsBuilder = ({ streamNum }) => {
71 return { outputOptions: [ buildStreamSuffix('-q:a', streamNum), '5' ] }
72}
73
74export function getDefaultAvailableEncoders () {
75 return {
76 vod: {
77 libx264: {
78 default: defaultX264VODOptionsBuilder
79 },
80 aac: {
81 default: defaultAACOptionsBuilder
82 },
83 libfdk_aac: {
84 default: defaultLibFDKAACVODOptionsBuilder
85 }
86 },
87 live: {
88 libx264: {
89 default: defaultX264LiveOptionsBuilder
90 },
91 aac: {
92 default: defaultAACOptionsBuilder
93 }
94 }
95 }
96}
97
98export function getDefaultEncodersToTry () {
99 return {
100 vod: {
101 video: [ 'libx264' ],
102 audio: [ 'libfdk_aac', 'aac' ]
103 },
104
105 live: {
106 video: [ 'libx264' ],
107 audio: [ 'libfdk_aac', 'aac' ]
108 }
109 }
110}
111
112export async function canDoQuickAudioTranscode (path: string, probe?: FfprobeData): Promise<boolean> {
113 const parsedAudio = await getAudioStream(path, probe)
114
115 if (!parsedAudio.audioStream) return true
116
117 if (parsedAudio.audioStream['codec_name'] !== 'aac') return false
118
119 const audioBitrate = parsedAudio.bitrate
120 if (!audioBitrate) return false
121
122 const maxAudioBitrate = getMaxAudioBitrate('aac', audioBitrate)
123 if (maxAudioBitrate !== -1 && audioBitrate > maxAudioBitrate) return false
124
125 const channelLayout = parsedAudio.audioStream['channel_layout']
126 // Causes playback issues with Chrome
127 if (!channelLayout || channelLayout === 'unknown' || channelLayout === 'quad') return false
128
129 return true
130}
131
132export async function canDoQuickVideoTranscode (path: string, probe?: FfprobeData): Promise<boolean> {
133 const videoStream = await getVideoStream(path, probe)
134 const fps = await getVideoStreamFPS(path, probe)
135 const bitRate = await getVideoStreamBitrate(path, probe)
136 const resolutionData = await getVideoStreamDimensionsInfo(path, probe)
137
138 // If ffprobe did not manage to guess the bitrate
139 if (!bitRate) return false
140
141 // check video params
142 if (!videoStream) return false
143 if (videoStream['codec_name'] !== 'h264') return false
144 if (videoStream['pix_fmt'] !== 'yuv420p') return false
145 if (fps < 2 || fps > 65) return false
146 if (bitRate > getMaxTheoreticalBitrate({ ...resolutionData, fps })) return false
147
148 return true
149}
150
151// ---------------------------------------------------------------------------
152
153function getTargetBitrate (options: {
154 inputBitrate: number
155 resolution: number
156 ratio: number
157 fps: number
158}) {
159 const { inputBitrate, resolution, ratio, fps } = options
160
161 const capped = capBitrate(inputBitrate, getAverageTheoreticalBitrate({ resolution, fps, ratio }))
162 const limit = getMinTheoreticalBitrate({ resolution, fps, ratio })
163
164 return Math.max(limit, capped)
165}
166
167function capBitrate (inputBitrate: number, targetBitrate: number) {
168 if (!inputBitrate) return targetBitrate
169
170 // Add 30% margin to input bitrate
171 const inputBitrateWithMargin = inputBitrate + (inputBitrate * 0.3)
172
173 return Math.min(targetBitrate, inputBitrateWithMargin)
174}
175
176function getCommonOutputOptions (targetBitrate: number, streamNum?: number) {
177 return [
178 `-preset veryfast`,
179 `${buildStreamSuffix('-maxrate:v', streamNum)} ${targetBitrate}`,
180 `${buildStreamSuffix('-bufsize:v', streamNum)} ${targetBitrate * 2}`,
181
182 // NOTE: b-strategy 1 - heuristic algorithm, 16 is optimal B-frames for it
183 `-b_strategy 1`,
184 // NOTE: Why 16: https://github.com/Chocobozzz/PeerTube/pull/774. b-strategy 2 -> B-frames<16
185 `-bf 16`
186 ]
187}