]>
Commit | Line | Data |
---|---|---|
1 | import * as ffmpeg from 'fluent-ffmpeg' | |
2 | import { dirname, join } from 'path' | |
3 | import { getTargetBitrate, VideoResolution } from '../../shared/models/videos' | |
4 | import { CONFIG, FFMPEG_NICE, VIDEO_TRANSCODING_FPS } from '../initializers/constants' | |
5 | import { processImage } from './image-utils' | |
6 | import { logger } from './logger' | |
7 | import { checkFFmpegEncoders } from '../initializers/checker-before-init' | |
8 | import { remove } from 'fs-extra' | |
9 | ||
10 | function computeResolutionsToTranscode (videoFileHeight: number) { | |
11 | const resolutionsEnabled: number[] = [] | |
12 | const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS | |
13 | ||
14 | // Put in the order we want to proceed jobs | |
15 | const resolutions = [ | |
16 | VideoResolution.H_480P, | |
17 | VideoResolution.H_360P, | |
18 | VideoResolution.H_720P, | |
19 | VideoResolution.H_240P, | |
20 | VideoResolution.H_1080P | |
21 | ] | |
22 | ||
23 | for (const resolution of resolutions) { | |
24 | if (configResolutions[ resolution + 'p' ] === true && videoFileHeight > resolution) { | |
25 | resolutionsEnabled.push(resolution) | |
26 | } | |
27 | } | |
28 | ||
29 | return resolutionsEnabled | |
30 | } | |
31 | ||
32 | async function getVideoFileSize (path: string) { | |
33 | const videoStream = await getVideoFileStream(path) | |
34 | ||
35 | return { | |
36 | width: videoStream.width, | |
37 | height: videoStream.height | |
38 | } | |
39 | } | |
40 | ||
41 | async function getVideoFileResolution (path: string) { | |
42 | const size = await getVideoFileSize(path) | |
43 | ||
44 | return { | |
45 | videoFileResolution: Math.min(size.height, size.width), | |
46 | isPortraitMode: size.height > size.width | |
47 | } | |
48 | } | |
49 | ||
50 | async function getVideoFileFPS (path: string) { | |
51 | const videoStream = await getVideoFileStream(path) | |
52 | ||
53 | for (const key of [ 'avg_frame_rate', 'r_frame_rate' ]) { | |
54 | const valuesText: string = videoStream[key] | |
55 | if (!valuesText) continue | |
56 | ||
57 | const [ frames, seconds ] = valuesText.split('/') | |
58 | if (!frames || !seconds) continue | |
59 | ||
60 | const result = parseInt(frames, 10) / parseInt(seconds, 10) | |
61 | if (result > 0) return Math.round(result) | |
62 | } | |
63 | ||
64 | return 0 | |
65 | } | |
66 | ||
67 | async function getVideoFileBitrate (path: string) { | |
68 | return new Promise<number>((res, rej) => { | |
69 | ffmpeg.ffprobe(path, (err, metadata) => { | |
70 | if (err) return rej(err) | |
71 | ||
72 | return res(metadata.format.bit_rate) | |
73 | }) | |
74 | }) | |
75 | } | |
76 | ||
77 | function getDurationFromVideoFile (path: string) { | |
78 | return new Promise<number>((res, rej) => { | |
79 | ffmpeg.ffprobe(path, (err, metadata) => { | |
80 | if (err) return rej(err) | |
81 | ||
82 | return res(Math.floor(metadata.format.duration)) | |
83 | }) | |
84 | }) | |
85 | } | |
86 | ||
87 | async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) { | |
88 | const pendingImageName = 'pending-' + imageName | |
89 | ||
90 | const options = { | |
91 | filename: pendingImageName, | |
92 | count: 1, | |
93 | folder | |
94 | } | |
95 | ||
96 | const pendingImagePath = join(folder, pendingImageName) | |
97 | ||
98 | try { | |
99 | await new Promise<string>((res, rej) => { | |
100 | ffmpeg(fromPath, { niceness: FFMPEG_NICE.THUMBNAIL }) | |
101 | .on('error', rej) | |
102 | .on('end', () => res(imageName)) | |
103 | .thumbnail(options) | |
104 | }) | |
105 | ||
106 | const destination = join(folder, imageName) | |
107 | await processImage({ path: pendingImagePath }, destination, size) | |
108 | } catch (err) { | |
109 | logger.error('Cannot generate image from video %s.', fromPath, { err }) | |
110 | ||
111 | try { | |
112 | await remove(pendingImagePath) | |
113 | } catch (err) { | |
114 | logger.debug('Cannot remove pending image path after generation error.', { err }) | |
115 | } | |
116 | } | |
117 | } | |
118 | ||
119 | type TranscodeOptions = { | |
120 | inputPath: string | |
121 | outputPath: string | |
122 | resolution: VideoResolution | |
123 | isPortraitMode?: boolean | |
124 | ||
125 | hlsPlaylist?: { | |
126 | videoFilename: string | |
127 | } | |
128 | } | |
129 | ||
130 | function transcode (options: TranscodeOptions) { | |
131 | return new Promise<void>(async (res, rej) => { | |
132 | try { | |
133 | let fps = await getVideoFileFPS(options.inputPath) | |
134 | // On small/medium resolutions, limit FPS | |
135 | if ( | |
136 | options.resolution !== undefined && | |
137 | options.resolution < VIDEO_TRANSCODING_FPS.KEEP_ORIGIN_FPS_RESOLUTION_MIN && | |
138 | fps > VIDEO_TRANSCODING_FPS.AVERAGE | |
139 | ) { | |
140 | fps = VIDEO_TRANSCODING_FPS.AVERAGE | |
141 | } | |
142 | ||
143 | let command = ffmpeg(options.inputPath, { niceness: FFMPEG_NICE.TRANSCODING }) | |
144 | .output(options.outputPath) | |
145 | command = await presetH264(command, options.resolution, fps) | |
146 | ||
147 | if (CONFIG.TRANSCODING.THREADS > 0) { | |
148 | // if we don't set any threads ffmpeg will chose automatically | |
149 | command = command.outputOption('-threads ' + CONFIG.TRANSCODING.THREADS) | |
150 | } | |
151 | ||
152 | if (options.resolution !== undefined) { | |
153 | // '?x720' or '720x?' for example | |
154 | const size = options.isPortraitMode === true ? `${options.resolution}x?` : `?x${options.resolution}` | |
155 | command = command.size(size) | |
156 | } | |
157 | ||
158 | if (fps) { | |
159 | // Hard FPS limits | |
160 | if (fps > VIDEO_TRANSCODING_FPS.MAX) fps = VIDEO_TRANSCODING_FPS.MAX | |
161 | else if (fps < VIDEO_TRANSCODING_FPS.MIN) fps = VIDEO_TRANSCODING_FPS.MIN | |
162 | ||
163 | command = command.withFPS(fps) | |
164 | } | |
165 | ||
166 | if (options.hlsPlaylist) { | |
167 | const videoPath = `${dirname(options.outputPath)}/${options.hlsPlaylist.videoFilename}` | |
168 | ||
169 | command = command.outputOption('-hls_time 4') | |
170 | .outputOption('-hls_list_size 0') | |
171 | .outputOption('-hls_playlist_type vod') | |
172 | .outputOption('-hls_segment_filename ' + videoPath) | |
173 | .outputOption('-hls_segment_type fmp4') | |
174 | .outputOption('-f hls') | |
175 | .outputOption('-hls_flags single_file') | |
176 | } | |
177 | ||
178 | command | |
179 | .on('error', (err, stdout, stderr) => { | |
180 | logger.error('Error in transcoding job.', { stdout, stderr }) | |
181 | return rej(err) | |
182 | }) | |
183 | .on('end', res) | |
184 | .run() | |
185 | } catch (err) { | |
186 | return rej(err) | |
187 | } | |
188 | }) | |
189 | } | |
190 | ||
191 | // --------------------------------------------------------------------------- | |
192 | ||
193 | export { | |
194 | getVideoFileSize, | |
195 | getVideoFileResolution, | |
196 | getDurationFromVideoFile, | |
197 | generateImageFromVideoFile, | |
198 | transcode, | |
199 | getVideoFileFPS, | |
200 | computeResolutionsToTranscode, | |
201 | audio, | |
202 | getVideoFileBitrate | |
203 | } | |
204 | ||
205 | // --------------------------------------------------------------------------- | |
206 | ||
207 | function getVideoFileStream (path: string) { | |
208 | return new Promise<any>((res, rej) => { | |
209 | ffmpeg.ffprobe(path, (err, metadata) => { | |
210 | if (err) return rej(err) | |
211 | ||
212 | const videoStream = metadata.streams.find(s => s.codec_type === 'video') | |
213 | if (!videoStream) return rej(new Error('Cannot find video stream of ' + path)) | |
214 | ||
215 | return res(videoStream) | |
216 | }) | |
217 | }) | |
218 | } | |
219 | ||
220 | /** | |
221 | * A slightly customised version of the 'veryfast' x264 preset | |
222 | * | |
223 | * The veryfast preset is right in the sweet spot of performance | |
224 | * and quality. Superfast and ultrafast will give you better | |
225 | * performance, but then quality is noticeably worse. | |
226 | */ | |
227 | async function presetH264VeryFast (command: ffmpeg.FfmpegCommand, resolution: VideoResolution, fps: number): Promise<ffmpeg.FfmpegCommand> { | |
228 | let localCommand = await presetH264(command, resolution, fps) | |
229 | localCommand = localCommand.outputOption('-preset:v veryfast') | |
230 | .outputOption([ '--aq-mode=2', '--aq-strength=1.3' ]) | |
231 | /* | |
232 | MAIN reference: https://slhck.info/video/2017/03/01/rate-control.html | |
233 | Our target situation is closer to a livestream than a stream, | |
234 | since we want to reduce as much a possible the encoding burden, | |
235 | altough not to the point of a livestream where there is a hard | |
236 | constraint on the frames per second to be encoded. | |
237 | ||
238 | why '--aq-mode=2 --aq-strength=1.3' instead of '-profile:v main'? | |
239 | Make up for most of the loss of grain and macroblocking | |
240 | with less computing power. | |
241 | */ | |
242 | ||
243 | return localCommand | |
244 | } | |
245 | ||
246 | /** | |
247 | * A preset optimised for a stillimage audio video | |
248 | */ | |
249 | async function presetStillImageWithAudio ( | |
250 | command: ffmpeg.FfmpegCommand, | |
251 | resolution: VideoResolution, | |
252 | fps: number | |
253 | ): Promise<ffmpeg.FfmpegCommand> { | |
254 | let localCommand = await presetH264VeryFast(command, resolution, fps) | |
255 | localCommand = localCommand.outputOption('-tune stillimage') | |
256 | ||
257 | return localCommand | |
258 | } | |
259 | ||
260 | /** | |
261 | * A toolbox to play with audio | |
262 | */ | |
263 | namespace audio { | |
264 | export const get = (option: ffmpeg.FfmpegCommand | string) => { | |
265 | // without position, ffprobe considers the last input only | |
266 | // we make it consider the first input only | |
267 | // if you pass a file path to pos, then ffprobe acts on that file directly | |
268 | return new Promise<{ absolutePath: string, audioStream?: any }>((res, rej) => { | |
269 | ||
270 | function parseFfprobe (err: any, data: ffmpeg.FfprobeData) { | |
271 | if (err) return rej(err) | |
272 | ||
273 | if ('streams' in data) { | |
274 | const audioStream = data.streams.find(stream => stream['codec_type'] === 'audio') | |
275 | if (audioStream) { | |
276 | return res({ | |
277 | absolutePath: data.format.filename, | |
278 | audioStream | |
279 | }) | |
280 | } | |
281 | } | |
282 | ||
283 | return res({ absolutePath: data.format.filename }) | |
284 | } | |
285 | ||
286 | if (typeof option === 'string') { | |
287 | return ffmpeg.ffprobe(option, parseFfprobe) | |
288 | } | |
289 | ||
290 | return option.ffprobe(parseFfprobe) | |
291 | }) | |
292 | } | |
293 | ||
294 | export namespace bitrate { | |
295 | const baseKbitrate = 384 | |
296 | ||
297 | const toBits = (kbits: number): number => { return kbits * 8000 } | |
298 | ||
299 | export const aac = (bitrate: number): number => { | |
300 | switch (true) { | |
301 | case bitrate > toBits(baseKbitrate): | |
302 | return baseKbitrate | |
303 | default: | |
304 | return -1 // we interpret it as a signal to copy the audio stream as is | |
305 | } | |
306 | } | |
307 | ||
308 | export const mp3 = (bitrate: number): number => { | |
309 | /* | |
310 | a 192kbit/sec mp3 doesn't hold as much information as a 192kbit/sec aac. | |
311 | That's why, when using aac, we can go to lower kbit/sec. The equivalences | |
312 | made here are not made to be accurate, especially with good mp3 encoders. | |
313 | */ | |
314 | switch (true) { | |
315 | case bitrate <= toBits(192): | |
316 | return 128 | |
317 | case bitrate <= toBits(384): | |
318 | return 256 | |
319 | default: | |
320 | return baseKbitrate | |
321 | } | |
322 | } | |
323 | } | |
324 | } | |
325 | ||
326 | /** | |
327 | * Standard profile, with variable bitrate audio and faststart. | |
328 | * | |
329 | * As for the audio, quality '5' is the highest and ensures 96-112kbps/channel | |
330 | * See https://trac.ffmpeg.org/wiki/Encode/AAC#fdk_vbr | |
331 | */ | |
332 | async function presetH264 (command: ffmpeg.FfmpegCommand, resolution: VideoResolution, fps: number): Promise<ffmpeg.FfmpegCommand> { | |
333 | let localCommand = command | |
334 | .format('mp4') | |
335 | .videoCodec('libx264') | |
336 | .outputOption('-level 3.1') // 3.1 is the minimal ressource allocation for our highest supported resolution | |
337 | .outputOption('-b_strategy 1') // NOTE: b-strategy 1 - heuristic algorythm, 16 is optimal B-frames for it | |
338 | .outputOption('-bf 16') // NOTE: Why 16: https://github.com/Chocobozzz/PeerTube/pull/774. b-strategy 2 -> B-frames<16 | |
339 | .outputOption('-pix_fmt yuv420p') // allows import of source material with incompatible pixel formats (e.g. MJPEG video) | |
340 | .outputOption('-map_metadata -1') // strip all metadata | |
341 | .outputOption('-movflags faststart') | |
342 | ||
343 | const parsedAudio = await audio.get(localCommand) | |
344 | ||
345 | if (!parsedAudio.audioStream) { | |
346 | localCommand = localCommand.noAudio() | |
347 | } else if ((await checkFFmpegEncoders()).get('libfdk_aac')) { // we favor VBR, if a good AAC encoder is available | |
348 | localCommand = localCommand | |
349 | .audioCodec('libfdk_aac') | |
350 | .audioQuality(5) | |
351 | } else { | |
352 | // we try to reduce the ceiling bitrate by making rough correspondances of bitrates | |
353 | // of course this is far from perfect, but it might save some space in the end | |
354 | const audioCodecName = parsedAudio.audioStream[ 'codec_name' ] | |
355 | let bitrate: number | |
356 | if (audio.bitrate[ audioCodecName ]) { | |
357 | localCommand = localCommand.audioCodec('aac') | |
358 | ||
359 | bitrate = audio.bitrate[ audioCodecName ](parsedAudio.audioStream[ 'bit_rate' ]) | |
360 | if (bitrate !== undefined && bitrate !== -1) localCommand = localCommand.audioBitrate(bitrate) | |
361 | } | |
362 | } | |
363 | ||
364 | // Constrained Encoding (VBV) | |
365 | // https://slhck.info/video/2017/03/01/rate-control.html | |
366 | // https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate | |
367 | const targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS) | |
368 | localCommand = localCommand.outputOptions([`-maxrate ${ targetBitrate }`, `-bufsize ${ targetBitrate * 2 }`]) | |
369 | ||
370 | // Keyframe interval of 2 seconds for faster seeking and resolution switching. | |
371 | // https://streaminglearningcenter.com/blogs/whats-the-right-keyframe-interval.html | |
372 | // https://superuser.com/a/908325 | |
373 | localCommand = localCommand.outputOption(`-g ${ fps * 2 }`) | |
374 | ||
375 | return localCommand | |
376 | } |