]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/ffmpeg-utils.ts
Split ffmpeg utils with ffprobe utils
[github/Chocobozzz/PeerTube.git] / server / helpers / ffmpeg-utils.ts
1 import * as ffmpeg from 'fluent-ffmpeg'
2 import { readFile, remove, writeFile } from 'fs-extra'
3 import { dirname, join } from 'path'
4 import { getTargetBitrate, VideoResolution } from '../../shared/models/videos'
5 import { checkFFmpegEncoders } from '../initializers/checker-before-init'
6 import { CONFIG } from '../initializers/config'
7 import { FFMPEG_NICE, VIDEO_LIVE, VIDEO_TRANSCODING_FPS } from '../initializers/constants'
8 import { getAudioStream, getClosestFramerateStandard, getMaxAudioBitrate, getVideoFileFPS } from './ffprobe-utils'
9 import { processImage } from './image-utils'
10 import { logger } from './logger'
11
12 async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) {
13 const pendingImageName = 'pending-' + imageName
14
15 const options = {
16 filename: pendingImageName,
17 count: 1,
18 folder
19 }
20
21 const pendingImagePath = join(folder, pendingImageName)
22
23 try {
24 await new Promise<string>((res, rej) => {
25 ffmpeg(fromPath, { niceness: FFMPEG_NICE.THUMBNAIL })
26 .on('error', rej)
27 .on('end', () => res(imageName))
28 .thumbnail(options)
29 })
30
31 const destination = join(folder, imageName)
32 await processImage(pendingImagePath, destination, size)
33 } catch (err) {
34 logger.error('Cannot generate image from video %s.', fromPath, { err })
35
36 try {
37 await remove(pendingImagePath)
38 } catch (err) {
39 logger.debug('Cannot remove pending image path after generation error.', { err })
40 }
41 }
42 }
43
44 // ---------------------------------------------------------------------------
45 // Transcode meta function
46 // ---------------------------------------------------------------------------
47
48 type TranscodeOptionsType = 'hls' | 'quick-transcode' | 'video' | 'merge-audio' | 'only-audio'
49
50 interface BaseTranscodeOptions {
51 type: TranscodeOptionsType
52 inputPath: string
53 outputPath: string
54 resolution: VideoResolution
55 isPortraitMode?: boolean
56 }
57
58 interface HLSTranscodeOptions extends BaseTranscodeOptions {
59 type: 'hls'
60 copyCodecs: boolean
61 hlsPlaylist: {
62 videoFilename: string
63 }
64 }
65
66 interface QuickTranscodeOptions extends BaseTranscodeOptions {
67 type: 'quick-transcode'
68 }
69
70 interface VideoTranscodeOptions extends BaseTranscodeOptions {
71 type: 'video'
72 }
73
74 interface MergeAudioTranscodeOptions extends BaseTranscodeOptions {
75 type: 'merge-audio'
76 audioPath: string
77 }
78
79 interface OnlyAudioTranscodeOptions extends BaseTranscodeOptions {
80 type: 'only-audio'
81 }
82
83 type TranscodeOptions =
84 HLSTranscodeOptions
85 | VideoTranscodeOptions
86 | MergeAudioTranscodeOptions
87 | OnlyAudioTranscodeOptions
88 | QuickTranscodeOptions
89
90 const builders: {
91 [ type in TranscodeOptionsType ]: (c: ffmpeg.FfmpegCommand, o?: TranscodeOptions) => Promise<ffmpeg.FfmpegCommand> | ffmpeg.FfmpegCommand
92 } = {
93 'quick-transcode': buildQuickTranscodeCommand,
94 'hls': buildHLSVODCommand,
95 'merge-audio': buildAudioMergeCommand,
96 'only-audio': buildOnlyAudioCommand,
97 'video': buildx264Command
98 }
99
100 async function transcode (options: TranscodeOptions) {
101 logger.debug('Will run transcode.', { options })
102
103 let command = getFFmpeg(options.inputPath)
104 .output(options.outputPath)
105
106 command = await builders[options.type](command, options)
107
108 await runCommand(command)
109
110 await fixHLSPlaylistIfNeeded(options)
111 }
112
113 function convertWebPToJPG (path: string, destination: string): Promise<void> {
114 return new Promise<void>(async (res, rej) => {
115 try {
116 const command = ffmpeg(path).output(destination)
117
118 command.on('error', (err, stdout, stderr) => {
119 logger.error('Error in ffmpeg webp convert process.', { stdout, stderr })
120 return rej(err)
121 })
122 .on('end', () => res())
123 .run()
124 } catch (err) {
125 return rej(err)
126 }
127 })
128 }
129
130 function processGIF (
131 path: string,
132 destination: string,
133 newSize: { width: number, height: number },
134 keepOriginal = false
135 ): Promise<void> {
136 return new Promise<void>(async (res, rej) => {
137 if (path === destination) {
138 throw new Error('FFmpeg needs an input path different that the output path.')
139 }
140
141 logger.debug('Processing gif %s to %s.', path, destination)
142
143 try {
144 const command = ffmpeg(path)
145 .fps(20)
146 .size(`${newSize.width}x${newSize.height}`)
147 .output(destination)
148
149 command.on('error', (err, stdout, stderr) => {
150 logger.error('Error in ffmpeg gif resizing process.', { stdout, stderr })
151 return rej(err)
152 })
153 .on('end', async () => {
154 if (keepOriginal !== true) await remove(path)
155 res()
156 })
157 .run()
158 } catch (err) {
159 return rej(err)
160 }
161 })
162 }
163
164 function runLiveTranscoding (rtmpUrl: string, outPath: string, resolutions: number[], fps, deleteSegments: boolean) {
165 const command = getFFmpeg(rtmpUrl)
166 command.inputOption('-fflags nobuffer')
167
168 const varStreamMap: string[] = []
169
170 command.complexFilter([
171 {
172 inputs: '[v:0]',
173 filter: 'split',
174 options: resolutions.length,
175 outputs: resolutions.map(r => `vtemp${r}`)
176 },
177
178 ...resolutions.map(r => ({
179 inputs: `vtemp${r}`,
180 filter: 'scale',
181 options: `w=-2:h=${r}`,
182 outputs: `vout${r}`
183 }))
184 ])
185
186 command.outputOption('-b_strategy 1')
187 command.outputOption('-bf 16')
188 command.outputOption('-preset superfast')
189 command.outputOption('-level 3.1')
190 command.outputOption('-map_metadata -1')
191 command.outputOption('-pix_fmt yuv420p')
192 command.outputOption('-max_muxing_queue_size 1024')
193 command.outputOption('-g ' + (fps * 2))
194
195 for (let i = 0; i < resolutions.length; i++) {
196 const resolution = resolutions[i]
197
198 command.outputOption(`-map [vout${resolution}]`)
199 command.outputOption(`-c:v:${i} libx264`)
200 command.outputOption(`-b:v:${i} ${getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS)}`)
201
202 command.outputOption(`-map a:0`)
203 command.outputOption(`-c:a:${i} aac`)
204
205 varStreamMap.push(`v:${i},a:${i}`)
206 }
207
208 addDefaultLiveHLSParams(command, outPath, deleteSegments)
209
210 command.outputOption('-var_stream_map', varStreamMap.join(' '))
211
212 command.run()
213
214 return command
215 }
216
217 function runLiveMuxing (rtmpUrl: string, outPath: string, deleteSegments: boolean) {
218 const command = getFFmpeg(rtmpUrl)
219 command.inputOption('-fflags nobuffer')
220
221 command.outputOption('-c:v copy')
222 command.outputOption('-c:a copy')
223 command.outputOption('-map 0:a?')
224 command.outputOption('-map 0:v?')
225
226 addDefaultLiveHLSParams(command, outPath, deleteSegments)
227
228 command.run()
229
230 return command
231 }
232
233 async function hlsPlaylistToFragmentedMP4 (hlsDirectory: string, segmentFiles: string[], outputPath: string) {
234 const concatFilePath = join(hlsDirectory, 'concat.txt')
235
236 function cleaner () {
237 remove(concatFilePath)
238 .catch(err => logger.error('Cannot remove concat file in %s.', hlsDirectory, { err }))
239 }
240
241 // First concat the ts files to a mp4 file
242 const content = segmentFiles.map(f => 'file ' + f)
243 .join('\n')
244
245 await writeFile(concatFilePath, content + '\n')
246
247 const command = getFFmpeg(concatFilePath)
248 command.inputOption('-safe 0')
249 command.inputOption('-f concat')
250
251 command.outputOption('-c:v copy')
252 command.audioFilter('aresample=async=1:first_pts=0')
253 command.output(outputPath)
254
255 return runCommand(command, cleaner)
256 }
257
258 async function runCommand (command: ffmpeg.FfmpegCommand, onEnd?: Function) {
259 return new Promise<string>((res, rej) => {
260 command.on('error', (err, stdout, stderr) => {
261 if (onEnd) onEnd()
262
263 logger.error('Error in transcoding job.', { stdout, stderr })
264 rej(err)
265 })
266
267 command.on('end', () => {
268 if (onEnd) onEnd()
269
270 res()
271 })
272
273 command.run()
274 })
275 }
276
277 // ---------------------------------------------------------------------------
278
279 export {
280 runLiveMuxing,
281 convertWebPToJPG,
282 processGIF,
283 runLiveTranscoding,
284 generateImageFromVideoFile,
285 TranscodeOptions,
286 TranscodeOptionsType,
287 transcode,
288 hlsPlaylistToFragmentedMP4
289 }
290
291 // ---------------------------------------------------------------------------
292
293 function addDefaultX264Params (command: ffmpeg.FfmpegCommand) {
294 command.outputOption('-level 3.1') // 3.1 is the minimal resource allocation for our highest supported resolution
295 .outputOption('-b_strategy 1') // NOTE: b-strategy 1 - heuristic algorithm, 16 is optimal B-frames for it
296 .outputOption('-bf 16') // NOTE: Why 16: https://github.com/Chocobozzz/PeerTube/pull/774. b-strategy 2 -> B-frames<16
297 .outputOption('-pix_fmt yuv420p') // allows import of source material with incompatible pixel formats (e.g. MJPEG video)
298 .outputOption('-map_metadata -1') // strip all metadata
299 }
300
301 function addDefaultLiveHLSParams (command: ffmpeg.FfmpegCommand, outPath: string, deleteSegments: boolean) {
302 command.outputOption('-hls_time ' + VIDEO_LIVE.SEGMENT_TIME_SECONDS)
303 command.outputOption('-hls_list_size ' + VIDEO_LIVE.SEGMENTS_LIST_SIZE)
304
305 if (deleteSegments === true) {
306 command.outputOption('-hls_flags delete_segments')
307 }
308
309 command.outputOption(`-hls_segment_filename ${join(outPath, '%v-%06d.ts')}`)
310 command.outputOption('-master_pl_name master.m3u8')
311 command.outputOption(`-f hls`)
312
313 command.output(join(outPath, '%v.m3u8'))
314 }
315
316 async function buildx264Command (command: ffmpeg.FfmpegCommand, options: TranscodeOptions) {
317 let fps = await getVideoFileFPS(options.inputPath)
318 if (
319 // On small/medium resolutions, limit FPS
320 options.resolution !== undefined &&
321 options.resolution < VIDEO_TRANSCODING_FPS.KEEP_ORIGIN_FPS_RESOLUTION_MIN &&
322 fps > VIDEO_TRANSCODING_FPS.AVERAGE
323 ) {
324 // Get closest standard framerate by modulo: downsampling has to be done to a divisor of the nominal fps value
325 fps = getClosestFramerateStandard(fps, 'STANDARD')
326 }
327
328 command = await presetH264(command, options.inputPath, options.resolution, fps)
329
330 if (options.resolution !== undefined) {
331 // '?x720' or '720x?' for example
332 const size = options.isPortraitMode === true ? `${options.resolution}x?` : `?x${options.resolution}`
333 command = command.size(size)
334 }
335
336 if (fps) {
337 // Hard FPS limits
338 if (fps > VIDEO_TRANSCODING_FPS.MAX) fps = getClosestFramerateStandard(fps, 'HD_STANDARD')
339 else if (fps < VIDEO_TRANSCODING_FPS.MIN) fps = VIDEO_TRANSCODING_FPS.MIN
340
341 command = command.withFPS(fps)
342 }
343
344 return command
345 }
346
347 async function buildAudioMergeCommand (command: ffmpeg.FfmpegCommand, options: MergeAudioTranscodeOptions) {
348 command = command.loop(undefined)
349
350 command = await presetH264VeryFast(command, options.audioPath, options.resolution)
351
352 command = command.input(options.audioPath)
353 .videoFilter('scale=trunc(iw/2)*2:trunc(ih/2)*2') // Avoid "height not divisible by 2" error
354 .outputOption('-tune stillimage')
355 .outputOption('-shortest')
356
357 return command
358 }
359
360 function buildOnlyAudioCommand (command: ffmpeg.FfmpegCommand, _options: OnlyAudioTranscodeOptions) {
361 command = presetOnlyAudio(command)
362
363 return command
364 }
365
366 function buildQuickTranscodeCommand (command: ffmpeg.FfmpegCommand) {
367 command = presetCopy(command)
368
369 command = command.outputOption('-map_metadata -1') // strip all metadata
370 .outputOption('-movflags faststart')
371
372 return command
373 }
374
375 async function buildHLSVODCommand (command: ffmpeg.FfmpegCommand, options: HLSTranscodeOptions) {
376 const videoPath = getHLSVideoPath(options)
377
378 if (options.copyCodecs) command = presetCopy(command)
379 else if (options.resolution === VideoResolution.H_NOVIDEO) command = presetOnlyAudio(command)
380 else command = await buildx264Command(command, options)
381
382 command = command.outputOption('-hls_time 4')
383 .outputOption('-hls_list_size 0')
384 .outputOption('-hls_playlist_type vod')
385 .outputOption('-hls_segment_filename ' + videoPath)
386 .outputOption('-hls_segment_type fmp4')
387 .outputOption('-f hls')
388 .outputOption('-hls_flags single_file')
389
390 return command
391 }
392
393 function getHLSVideoPath (options: HLSTranscodeOptions) {
394 return `${dirname(options.outputPath)}/${options.hlsPlaylist.videoFilename}`
395 }
396
397 async function fixHLSPlaylistIfNeeded (options: TranscodeOptions) {
398 if (options.type !== 'hls') return
399
400 const fileContent = await readFile(options.outputPath)
401
402 const videoFileName = options.hlsPlaylist.videoFilename
403 const videoFilePath = getHLSVideoPath(options)
404
405 // Fix wrong mapping with some ffmpeg versions
406 const newContent = fileContent.toString()
407 .replace(`#EXT-X-MAP:URI="${videoFilePath}",`, `#EXT-X-MAP:URI="${videoFileName}",`)
408
409 await writeFile(options.outputPath, newContent)
410 }
411
412 /**
413 * A slightly customised version of the 'veryfast' x264 preset
414 *
415 * The veryfast preset is right in the sweet spot of performance
416 * and quality. Superfast and ultrafast will give you better
417 * performance, but then quality is noticeably worse.
418 */
419 async function presetH264VeryFast (command: ffmpeg.FfmpegCommand, input: string, resolution: VideoResolution, fps?: number) {
420 let localCommand = await presetH264(command, input, resolution, fps)
421
422 localCommand = localCommand.outputOption('-preset:v veryfast')
423
424 /*
425 MAIN reference: https://slhck.info/video/2017/03/01/rate-control.html
426 Our target situation is closer to a livestream than a stream,
427 since we want to reduce as much a possible the encoding burden,
428 although not to the point of a livestream where there is a hard
429 constraint on the frames per second to be encoded.
430 */
431
432 return localCommand
433 }
434
435 /**
436 * Standard profile, with variable bitrate audio and faststart.
437 *
438 * As for the audio, quality '5' is the highest and ensures 96-112kbps/channel
439 * See https://trac.ffmpeg.org/wiki/Encode/AAC#fdk_vbr
440 */
441 async function presetH264 (command: ffmpeg.FfmpegCommand, input: string, resolution: VideoResolution, fps?: number) {
442 let localCommand = command
443 .format('mp4')
444 .videoCodec('libx264')
445 .outputOption('-movflags faststart')
446
447 addDefaultX264Params(localCommand)
448
449 const parsedAudio = await getAudioStream(input)
450
451 if (!parsedAudio.audioStream) {
452 localCommand = localCommand.noAudio()
453 } else if ((await checkFFmpegEncoders()).get('libfdk_aac')) { // we favor VBR, if a good AAC encoder is available
454 localCommand = localCommand
455 .audioCodec('libfdk_aac')
456 .audioQuality(5)
457 } else {
458 // we try to reduce the ceiling bitrate by making rough matches of bitrates
459 // of course this is far from perfect, but it might save some space in the end
460 localCommand = localCommand.audioCodec('aac')
461
462 const audioCodecName = parsedAudio.audioStream['codec_name']
463
464 const bitrate = getMaxAudioBitrate(audioCodecName, parsedAudio.bitrate)
465
466 if (bitrate !== undefined && bitrate !== -1) localCommand = localCommand.audioBitrate(bitrate)
467 }
468
469 if (fps) {
470 // Constrained Encoding (VBV)
471 // https://slhck.info/video/2017/03/01/rate-control.html
472 // https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate
473 const targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS)
474 localCommand = localCommand.outputOptions([ `-maxrate ${targetBitrate}`, `-bufsize ${targetBitrate * 2}` ])
475
476 // Keyframe interval of 2 seconds for faster seeking and resolution switching.
477 // https://streaminglearningcenter.com/blogs/whats-the-right-keyframe-interval.html
478 // https://superuser.com/a/908325
479 localCommand = localCommand.outputOption(`-g ${fps * 2}`)
480 }
481
482 return localCommand
483 }
484
485 function presetCopy (command: ffmpeg.FfmpegCommand): ffmpeg.FfmpegCommand {
486 return command
487 .format('mp4')
488 .videoCodec('copy')
489 .audioCodec('copy')
490 }
491
492 function presetOnlyAudio (command: ffmpeg.FfmpegCommand): ffmpeg.FfmpegCommand {
493 return command
494 .format('mp4')
495 .audioCodec('copy')
496 .noVideo()
497 }
498
499 function getFFmpeg (input: string) {
500 // We set cwd explicitly because ffmpeg appears to create temporary files when trancoding which fails in read-only file systems
501 const command = ffmpeg(input, { niceness: FFMPEG_NICE.TRANSCODING, cwd: CONFIG.STORAGE.TMP_DIR })
502
503 if (CONFIG.TRANSCODING.THREADS > 0) {
504 // If we don't set any threads ffmpeg will chose automatically
505 command.outputOption('-threads ' + CONFIG.TRANSCODING.THREADS)
506 }
507
508 return command
509 }