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