]>
Commit | Line | Data |
---|---|---|
06aad801 | 1 | import { FfprobeData } from 'fluent-ffmpeg' |
679c12e6 | 2 | import { getMaxBitrate } from '@shared/core-utils' |
06aad801 | 3 | import { |
1bb4c9ab | 4 | buildFileMetadata, |
06aad801 | 5 | ffprobePromise, |
06aad801 | 6 | getAudioStream, |
7 | getMaxAudioBitrate, | |
c729caf6 | 8 | getVideoStream, |
1bb4c9ab | 9 | getVideoStreamBitrate, |
c729caf6 | 10 | getVideoStreamDimensionsInfo, |
1bb4c9ab C |
11 | getVideoStreamDuration, |
12 | getVideoStreamFPS, | |
c729caf6 | 13 | hasAudioStream |
06aad801 | 14 | } from '@shared/extra-utils/ffprobe' |
c729caf6 C |
15 | import { VideoResolution, VideoTranscodingFPS } from '@shared/models' |
16 | import { CONFIG } from '../../initializers/config' | |
17 | import { VIDEO_TRANSCODING_FPS } from '../../initializers/constants' | |
18 | import { logger } from '../logger' | |
daf6e480 | 19 | |
6b67897e C |
20 | /** |
21 | * | |
22 | * Helpers to run ffprobe and extract data from the JSON output | |
23 | * | |
24 | */ | |
25 | ||
c729caf6 C |
26 | // --------------------------------------------------------------------------- |
27 | // Codecs | |
28 | // --------------------------------------------------------------------------- | |
daf6e480 | 29 | |
c729caf6 C |
30 | async function getVideoStreamCodec (path: string) { |
31 | const videoStream = await getVideoStream(path) | |
daf6e480 C |
32 | if (!videoStream) return '' |
33 | ||
34 | const videoCodec = videoStream.codec_tag_string | |
35 | ||
78995146 | 36 | if (videoCodec === 'vp09') return 'vp09.00.50.08' |
08370f62 | 37 | if (videoCodec === 'hev1') return 'hev1.1.6.L93.B0' |
78995146 | 38 | |
daf6e480 | 39 | const baseProfileMatrix = { |
78995146 C |
40 | avc1: { |
41 | High: '6400', | |
42 | Main: '4D40', | |
43 | Baseline: '42E0' | |
44 | }, | |
45 | av01: { | |
46 | High: '1', | |
47 | Main: '0', | |
48 | Professional: '2' | |
49 | } | |
daf6e480 C |
50 | } |
51 | ||
78995146 | 52 | let baseProfile = baseProfileMatrix[videoCodec][videoStream.profile] |
daf6e480 C |
53 | if (!baseProfile) { |
54 | logger.warn('Cannot get video profile codec of %s.', path, { videoStream }) | |
78995146 C |
55 | baseProfile = baseProfileMatrix[videoCodec]['High'] // Fallback |
56 | } | |
57 | ||
01ec3975 | 58 | if (videoCodec === 'av01') { |
31951bad C |
59 | let level = videoStream.level.toString() |
60 | if (level.length === 1) level = `0${level}` | |
61 | ||
78995146 C |
62 | // Guess the tier indicator and bit depth |
63 | return `${videoCodec}.${baseProfile}.${level}M.08` | |
daf6e480 C |
64 | } |
65 | ||
31951bad C |
66 | let level = videoStream.level.toString(16) |
67 | if (level.length === 1) level = `0${level}` | |
68 | ||
78995146 | 69 | // Default, h264 codec |
daf6e480 C |
70 | return `${videoCodec}.${baseProfile}${level}` |
71 | } | |
72 | ||
41fb13c3 | 73 | async function getAudioStreamCodec (path: string, existingProbe?: FfprobeData) { |
daf6e480 C |
74 | const { audioStream } = await getAudioStream(path, existingProbe) |
75 | ||
76 | if (!audioStream) return '' | |
77 | ||
78995146 C |
78 | const audioCodecName = audioStream.codec_name |
79 | ||
80 | if (audioCodecName === 'opus') return 'opus' | |
81 | if (audioCodecName === 'vorbis') return 'vorbis' | |
82 | if (audioCodecName === 'aac') return 'mp4a.40.2' | |
3d401c1b | 83 | if (audioCodecName === 'mp3') return 'mp4a.40.34' |
daf6e480 C |
84 | |
85 | logger.warn('Cannot get audio codec of %s.', path, { audioStream }) | |
86 | ||
87 | return 'mp4a.40.2' // Fallback | |
88 | } | |
89 | ||
c729caf6 C |
90 | // --------------------------------------------------------------------------- |
91 | // Resolutions | |
92 | // --------------------------------------------------------------------------- | |
93 | ||
84cae54e | 94 | function computeResolutionsToTranscode (options: { |
5e2afe42 | 95 | input: number |
84cae54e | 96 | type: 'vod' | 'live' |
5e2afe42 C |
97 | includeInput: boolean |
98 | strictLower: boolean | |
84cae54e | 99 | }) { |
5e2afe42 | 100 | const { input, type, includeInput, strictLower } = options |
84cae54e | 101 | |
daf6e480 C |
102 | const configResolutions = type === 'vod' |
103 | ? CONFIG.TRANSCODING.RESOLUTIONS | |
104 | : CONFIG.LIVE.TRANSCODING.RESOLUTIONS | |
105 | ||
84cae54e | 106 | const resolutionsEnabled = new Set<number>() |
daf6e480 C |
107 | |
108 | // Put in the order we want to proceed jobs | |
84cae54e | 109 | const availableResolutions: VideoResolution[] = [ |
daf6e480 C |
110 | VideoResolution.H_NOVIDEO, |
111 | VideoResolution.H_480P, | |
112 | VideoResolution.H_360P, | |
113 | VideoResolution.H_720P, | |
114 | VideoResolution.H_240P, | |
8dd754c7 | 115 | VideoResolution.H_144P, |
daf6e480 | 116 | VideoResolution.H_1080P, |
b7085c71 | 117 | VideoResolution.H_1440P, |
daf6e480 C |
118 | VideoResolution.H_4K |
119 | ] | |
120 | ||
84cae54e | 121 | for (const resolution of availableResolutions) { |
5e2afe42 C |
122 | // Resolution not enabled |
123 | if (configResolutions[resolution + 'p'] !== true) continue | |
124 | // Too big resolution for input file | |
125 | if (input < resolution) continue | |
126 | // We only want lower resolutions than input file | |
127 | if (strictLower && input === resolution) continue | |
128 | ||
129 | resolutionsEnabled.add(resolution) | |
daf6e480 C |
130 | } |
131 | ||
5e2afe42 C |
132 | if (includeInput) { |
133 | resolutionsEnabled.add(input) | |
84cae54e C |
134 | } |
135 | ||
136 | return Array.from(resolutionsEnabled) | |
daf6e480 C |
137 | } |
138 | ||
c729caf6 C |
139 | // --------------------------------------------------------------------------- |
140 | // Can quick transcode | |
141 | // --------------------------------------------------------------------------- | |
142 | ||
daf6e480 | 143 | async function canDoQuickTranscode (path: string): Promise<boolean> { |
ffd970fa C |
144 | if (CONFIG.TRANSCODING.PROFILE !== 'default') return false |
145 | ||
daf6e480 C |
146 | const probe = await ffprobePromise(path) |
147 | ||
5a547f69 C |
148 | return await canDoQuickVideoTranscode(path, probe) && |
149 | await canDoQuickAudioTranscode(path, probe) | |
150 | } | |
151 | ||
c729caf6 C |
152 | async function canDoQuickAudioTranscode (path: string, probe?: FfprobeData): Promise<boolean> { |
153 | const parsedAudio = await getAudioStream(path, probe) | |
154 | ||
155 | if (!parsedAudio.audioStream) return true | |
156 | ||
157 | if (parsedAudio.audioStream['codec_name'] !== 'aac') return false | |
158 | ||
159 | const audioBitrate = parsedAudio.bitrate | |
160 | if (!audioBitrate) return false | |
161 | ||
162 | const maxAudioBitrate = getMaxAudioBitrate('aac', audioBitrate) | |
163 | if (maxAudioBitrate !== -1 && audioBitrate > maxAudioBitrate) return false | |
164 | ||
165 | const channelLayout = parsedAudio.audioStream['channel_layout'] | |
166 | // Causes playback issues with Chrome | |
4fd6dcfb | 167 | if (!channelLayout || channelLayout === 'unknown' || channelLayout === 'quad') return false |
c729caf6 C |
168 | |
169 | return true | |
170 | } | |
171 | ||
41fb13c3 | 172 | async function canDoQuickVideoTranscode (path: string, probe?: FfprobeData): Promise<boolean> { |
c729caf6 C |
173 | const videoStream = await getVideoStream(path, probe) |
174 | const fps = await getVideoStreamFPS(path, probe) | |
175 | const bitRate = await getVideoStreamBitrate(path, probe) | |
176 | const resolutionData = await getVideoStreamDimensionsInfo(path, probe) | |
daf6e480 | 177 | |
33ff70ba C |
178 | // If ffprobe did not manage to guess the bitrate |
179 | if (!bitRate) return false | |
180 | ||
daf6e480 | 181 | // check video params |
c729caf6 | 182 | if (!videoStream) return false |
daf6e480 C |
183 | if (videoStream['codec_name'] !== 'h264') return false |
184 | if (videoStream['pix_fmt'] !== 'yuv420p') return false | |
185 | if (fps < VIDEO_TRANSCODING_FPS.MIN || fps > VIDEO_TRANSCODING_FPS.MAX) return false | |
679c12e6 | 186 | if (bitRate > getMaxBitrate({ ...resolutionData, fps })) return false |
daf6e480 | 187 | |
5a547f69 C |
188 | return true |
189 | } | |
190 | ||
c729caf6 C |
191 | // --------------------------------------------------------------------------- |
192 | // Framerate | |
193 | // --------------------------------------------------------------------------- | |
194 | ||
679c12e6 | 195 | function getClosestFramerateStandard <K extends keyof Pick<VideoTranscodingFPS, 'HD_STANDARD' | 'STANDARD'>> (fps: number, type: K) { |
daf6e480 C |
196 | return VIDEO_TRANSCODING_FPS[type].slice(0) |
197 | .sort((a, b) => fps % a - fps % b)[0] | |
198 | } | |
199 | ||
884d2c39 C |
200 | function computeFPS (fpsArg: number, resolution: VideoResolution) { |
201 | let fps = fpsArg | |
202 | ||
203 | if ( | |
204 | // On small/medium resolutions, limit FPS | |
205 | resolution !== undefined && | |
206 | resolution < VIDEO_TRANSCODING_FPS.KEEP_ORIGIN_FPS_RESOLUTION_MIN && | |
207 | fps > VIDEO_TRANSCODING_FPS.AVERAGE | |
208 | ) { | |
209 | // Get closest standard framerate by modulo: downsampling has to be done to a divisor of the nominal fps value | |
210 | fps = getClosestFramerateStandard(fps, 'STANDARD') | |
211 | } | |
212 | ||
213 | // Hard FPS limits | |
214 | if (fps > VIDEO_TRANSCODING_FPS.MAX) fps = getClosestFramerateStandard(fps, 'HD_STANDARD') | |
f7bb2bb5 C |
215 | |
216 | if (fps < VIDEO_TRANSCODING_FPS.MIN) { | |
217 | throw new Error(`Cannot compute FPS because ${fps} is lower than our minimum value ${VIDEO_TRANSCODING_FPS.MIN}`) | |
218 | } | |
884d2c39 C |
219 | |
220 | return fps | |
221 | } | |
222 | ||
daf6e480 C |
223 | // --------------------------------------------------------------------------- |
224 | ||
225 | export { | |
c729caf6 C |
226 | // Re export ffprobe utils |
227 | getVideoStreamDimensionsInfo, | |
228 | buildFileMetadata, | |
daf6e480 | 229 | getMaxAudioBitrate, |
c729caf6 C |
230 | getVideoStream, |
231 | getVideoStreamDuration, | |
daf6e480 | 232 | getAudioStream, |
c729caf6 C |
233 | hasAudioStream, |
234 | getVideoStreamFPS, | |
5a547f69 | 235 | ffprobePromise, |
c729caf6 C |
236 | getVideoStreamBitrate, |
237 | ||
238 | getVideoStreamCodec, | |
239 | getAudioStreamCodec, | |
240 | ||
241 | computeFPS, | |
daf6e480 | 242 | getClosestFramerateStandard, |
c729caf6 | 243 | |
84cae54e | 244 | computeResolutionsToTranscode, |
c729caf6 | 245 | |
5a547f69 C |
246 | canDoQuickTranscode, |
247 | canDoQuickVideoTranscode, | |
248 | canDoQuickAudioTranscode | |
daf6e480 | 249 | } |