diff options
Diffstat (limited to 'server/helpers/ffprobe-utils.ts')
-rw-r--r-- | server/helpers/ffprobe-utils.ts | 173 |
1 files changed, 15 insertions, 158 deletions
diff --git a/server/helpers/ffprobe-utils.ts b/server/helpers/ffprobe-utils.ts index e15628e2a..595112bce 100644 --- a/server/helpers/ffprobe-utils.ts +++ b/server/helpers/ffprobe-utils.ts | |||
@@ -1,9 +1,22 @@ | |||
1 | import { ffprobe, FfprobeData } from 'fluent-ffmpeg' | 1 | import { FfprobeData } from 'fluent-ffmpeg' |
2 | import { getMaxBitrate } from '@shared/core-utils' | 2 | import { getMaxBitrate } from '@shared/core-utils' |
3 | import { VideoFileMetadata, VideoResolution, VideoTranscodingFPS } from '../../shared/models/videos' | 3 | import { VideoResolution, VideoTranscodingFPS } from '../../shared/models/videos' |
4 | import { CONFIG } from '../initializers/config' | 4 | import { CONFIG } from '../initializers/config' |
5 | import { VIDEO_TRANSCODING_FPS } from '../initializers/constants' | 5 | import { VIDEO_TRANSCODING_FPS } from '../initializers/constants' |
6 | import { logger } from './logger' | 6 | import { logger } from './logger' |
7 | import { | ||
8 | canDoQuickAudioTranscode, | ||
9 | ffprobePromise, | ||
10 | getDurationFromVideoFile, | ||
11 | getAudioStream, | ||
12 | getMaxAudioBitrate, | ||
13 | getMetadataFromFile, | ||
14 | getVideoFileBitrate, | ||
15 | getVideoFileFPS, | ||
16 | getVideoFileResolution, | ||
17 | getVideoStreamFromFile, | ||
18 | getVideoStreamSize | ||
19 | } from '@shared/extra-utils/ffprobe' | ||
7 | 20 | ||
8 | /** | 21 | /** |
9 | * | 22 | * |
@@ -11,79 +24,6 @@ import { logger } from './logger' | |||
11 | * | 24 | * |
12 | */ | 25 | */ |
13 | 26 | ||
14 | function ffprobePromise (path: string) { | ||
15 | return new Promise<FfprobeData>((res, rej) => { | ||
16 | ffprobe(path, (err, data) => { | ||
17 | if (err) return rej(err) | ||
18 | |||
19 | return res(data) | ||
20 | }) | ||
21 | }) | ||
22 | } | ||
23 | |||
24 | async function getAudioStream (videoPath: string, existingProbe?: FfprobeData) { | ||
25 | // without position, ffprobe considers the last input only | ||
26 | // we make it consider the first input only | ||
27 | // if you pass a file path to pos, then ffprobe acts on that file directly | ||
28 | const data = existingProbe || await ffprobePromise(videoPath) | ||
29 | |||
30 | if (Array.isArray(data.streams)) { | ||
31 | const audioStream = data.streams.find(stream => stream['codec_type'] === 'audio') | ||
32 | |||
33 | if (audioStream) { | ||
34 | return { | ||
35 | absolutePath: data.format.filename, | ||
36 | audioStream, | ||
37 | bitrate: parseInt(audioStream['bit_rate'] + '', 10) | ||
38 | } | ||
39 | } | ||
40 | } | ||
41 | |||
42 | return { absolutePath: data.format.filename } | ||
43 | } | ||
44 | |||
45 | function getMaxAudioBitrate (type: 'aac' | 'mp3' | string, bitrate: number) { | ||
46 | const maxKBitrate = 384 | ||
47 | const kToBits = (kbits: number) => kbits * 1000 | ||
48 | |||
49 | // If we did not manage to get the bitrate, use an average value | ||
50 | if (!bitrate) return 256 | ||
51 | |||
52 | if (type === 'aac') { | ||
53 | switch (true) { | ||
54 | case bitrate > kToBits(maxKBitrate): | ||
55 | return maxKBitrate | ||
56 | |||
57 | default: | ||
58 | return -1 // we interpret it as a signal to copy the audio stream as is | ||
59 | } | ||
60 | } | ||
61 | |||
62 | /* | ||
63 | a 192kbit/sec mp3 doesn't hold as much information as a 192kbit/sec aac. | ||
64 | That's why, when using aac, we can go to lower kbit/sec. The equivalences | ||
65 | made here are not made to be accurate, especially with good mp3 encoders. | ||
66 | */ | ||
67 | switch (true) { | ||
68 | case bitrate <= kToBits(192): | ||
69 | return 128 | ||
70 | |||
71 | case bitrate <= kToBits(384): | ||
72 | return 256 | ||
73 | |||
74 | default: | ||
75 | return maxKBitrate | ||
76 | } | ||
77 | } | ||
78 | |||
79 | async function getVideoStreamSize (path: string, existingProbe?: FfprobeData): Promise<{ width: number, height: number }> { | ||
80 | const videoStream = await getVideoStreamFromFile(path, existingProbe) | ||
81 | |||
82 | return videoStream === null | ||
83 | ? { width: 0, height: 0 } | ||
84 | : { width: videoStream.width, height: videoStream.height } | ||
85 | } | ||
86 | |||
87 | async function getVideoStreamCodec (path: string) { | 27 | async function getVideoStreamCodec (path: string) { |
88 | const videoStream = await getVideoStreamFromFile(path) | 28 | const videoStream = await getVideoStreamFromFile(path) |
89 | 29 | ||
@@ -143,69 +83,6 @@ async function getAudioStreamCodec (path: string, existingProbe?: FfprobeData) { | |||
143 | return 'mp4a.40.2' // Fallback | 83 | return 'mp4a.40.2' // Fallback |
144 | } | 84 | } |
145 | 85 | ||
146 | async function getVideoFileResolution (path: string, existingProbe?: FfprobeData) { | ||
147 | const size = await getVideoStreamSize(path, existingProbe) | ||
148 | |||
149 | return { | ||
150 | width: size.width, | ||
151 | height: size.height, | ||
152 | ratio: Math.max(size.height, size.width) / Math.min(size.height, size.width), | ||
153 | resolution: Math.min(size.height, size.width), | ||
154 | isPortraitMode: size.height > size.width | ||
155 | } | ||
156 | } | ||
157 | |||
158 | async function getVideoFileFPS (path: string, existingProbe?: FfprobeData) { | ||
159 | const videoStream = await getVideoStreamFromFile(path, existingProbe) | ||
160 | if (videoStream === null) return 0 | ||
161 | |||
162 | for (const key of [ 'avg_frame_rate', 'r_frame_rate' ]) { | ||
163 | const valuesText: string = videoStream[key] | ||
164 | if (!valuesText) continue | ||
165 | |||
166 | const [ frames, seconds ] = valuesText.split('/') | ||
167 | if (!frames || !seconds) continue | ||
168 | |||
169 | const result = parseInt(frames, 10) / parseInt(seconds, 10) | ||
170 | if (result > 0) return Math.round(result) | ||
171 | } | ||
172 | |||
173 | return 0 | ||
174 | } | ||
175 | |||
176 | async function getMetadataFromFile (path: string, existingProbe?: FfprobeData) { | ||
177 | const metadata = existingProbe || await ffprobePromise(path) | ||
178 | |||
179 | return new VideoFileMetadata(metadata) | ||
180 | } | ||
181 | |||
182 | async function getVideoFileBitrate (path: string, existingProbe?: FfprobeData): Promise<number> { | ||
183 | const metadata = await getMetadataFromFile(path, existingProbe) | ||
184 | |||
185 | let bitrate = metadata.format.bit_rate as number | ||
186 | if (bitrate && !isNaN(bitrate)) return bitrate | ||
187 | |||
188 | const videoStream = await getVideoStreamFromFile(path, existingProbe) | ||
189 | if (!videoStream) return undefined | ||
190 | |||
191 | bitrate = videoStream?.bit_rate | ||
192 | if (bitrate && !isNaN(bitrate)) return bitrate | ||
193 | |||
194 | return undefined | ||
195 | } | ||
196 | |||
197 | async function getDurationFromVideoFile (path: string, existingProbe?: FfprobeData) { | ||
198 | const metadata = await getMetadataFromFile(path, existingProbe) | ||
199 | |||
200 | return Math.round(metadata.format.duration) | ||
201 | } | ||
202 | |||
203 | async function getVideoStreamFromFile (path: string, existingProbe?: FfprobeData) { | ||
204 | const metadata = await getMetadataFromFile(path, existingProbe) | ||
205 | |||
206 | return metadata.streams.find(s => s.codec_type === 'video') || null | ||
207 | } | ||
208 | |||
209 | function computeLowerResolutionsToTranscode (videoFileResolution: number, type: 'vod' | 'live') { | 86 | function computeLowerResolutionsToTranscode (videoFileResolution: number, type: 'vod' | 'live') { |
210 | const configResolutions = type === 'vod' | 87 | const configResolutions = type === 'vod' |
211 | ? CONFIG.TRANSCODING.RESOLUTIONS | 88 | ? CONFIG.TRANSCODING.RESOLUTIONS |
@@ -263,26 +140,6 @@ async function canDoQuickVideoTranscode (path: string, probe?: FfprobeData): Pro | |||
263 | return true | 140 | return true |
264 | } | 141 | } |
265 | 142 | ||
266 | async function canDoQuickAudioTranscode (path: string, probe?: FfprobeData): Promise<boolean> { | ||
267 | const parsedAudio = await getAudioStream(path, probe) | ||
268 | |||
269 | if (!parsedAudio.audioStream) return true | ||
270 | |||
271 | if (parsedAudio.audioStream['codec_name'] !== 'aac') return false | ||
272 | |||
273 | const audioBitrate = parsedAudio.bitrate | ||
274 | if (!audioBitrate) return false | ||
275 | |||
276 | const maxAudioBitrate = getMaxAudioBitrate('aac', audioBitrate) | ||
277 | if (maxAudioBitrate !== -1 && audioBitrate > maxAudioBitrate) return false | ||
278 | |||
279 | const channelLayout = parsedAudio.audioStream['channel_layout'] | ||
280 | // Causes playback issues with Chrome | ||
281 | if (!channelLayout || channelLayout === 'unknown') return false | ||
282 | |||
283 | return true | ||
284 | } | ||
285 | |||
286 | function getClosestFramerateStandard <K extends keyof Pick<VideoTranscodingFPS, 'HD_STANDARD' | 'STANDARD'>> (fps: number, type: K) { | 143 | function getClosestFramerateStandard <K extends keyof Pick<VideoTranscodingFPS, 'HD_STANDARD' | 'STANDARD'>> (fps: number, type: K) { |
287 | return VIDEO_TRANSCODING_FPS[type].slice(0) | 144 | return VIDEO_TRANSCODING_FPS[type].slice(0) |
288 | .sort((a, b) => fps % a - fps % b)[0] | 145 | .sort((a, b) => fps % a - fps % b)[0] |