]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/ffmpeg-utils.ts
32bd3e44a7ecca298718af56d8f15c12abd6b12b
[github/Chocobozzz/PeerTube.git] / server / helpers / ffmpeg-utils.ts
1 import { Job } from 'bull'
2 import * as ffmpeg from 'fluent-ffmpeg'
3 import { readFile, remove, writeFile } from 'fs-extra'
4 import { dirname, join } from 'path'
5 import { FFMPEG_NICE, VIDEO_LIVE } from '@server/initializers/constants'
6 import { AvailableEncoders, EncoderOptionsBuilder, EncoderProfile, VideoResolution } from '../../shared/models/videos'
7 import { CONFIG } from '../initializers/config'
8 import { execPromise, promisify0 } from './core-utils'
9 import { computeFPS, getAudioStream, getVideoFileFPS } from './ffprobe-utils'
10 import { processImage } from './image-utils'
11 import { logger } from './logger'
12
13 /**
14 *
15 * Functions that run transcoding/muxing ffmpeg processes
16 * Mainly called by lib/video-transcoding.ts and lib/live-manager.ts
17 *
18 */
19
20 // ---------------------------------------------------------------------------
21 // Encoder options
22 // ---------------------------------------------------------------------------
23
24 type StreamType = 'audio' | 'video'
25
26 // ---------------------------------------------------------------------------
27 // Encoders support
28 // ---------------------------------------------------------------------------
29
30 // Detect supported encoders by ffmpeg
31 let supportedEncoders: Map<string, boolean>
32 async function checkFFmpegEncoders (peertubeAvailableEncoders: AvailableEncoders): Promise<Map<string, boolean>> {
33 if (supportedEncoders !== undefined) {
34 return supportedEncoders
35 }
36
37 const getAvailableEncodersPromise = promisify0(ffmpeg.getAvailableEncoders)
38 const availableFFmpegEncoders = await getAvailableEncodersPromise()
39
40 const searchEncoders = new Set<string>()
41 for (const type of [ 'live', 'vod' ]) {
42 for (const streamType of [ 'audio', 'video' ]) {
43 for (const encoder of peertubeAvailableEncoders.encodersToTry[type][streamType]) {
44 searchEncoders.add(encoder)
45 }
46 }
47 }
48
49 supportedEncoders = new Map<string, boolean>()
50
51 for (const searchEncoder of searchEncoders) {
52 supportedEncoders.set(searchEncoder, availableFFmpegEncoders[searchEncoder] !== undefined)
53 }
54
55 logger.info('Built supported ffmpeg encoders.', { supportedEncoders, searchEncoders })
56
57 return supportedEncoders
58 }
59
60 function resetSupportedEncoders () {
61 supportedEncoders = undefined
62 }
63
64 // ---------------------------------------------------------------------------
65 // Image manipulation
66 // ---------------------------------------------------------------------------
67
68 function convertWebPToJPG (path: string, destination: string): Promise<void> {
69 const command = ffmpeg(path, { niceness: FFMPEG_NICE.THUMBNAIL })
70 .output(destination)
71
72 return runCommand({ command, silent: true })
73 }
74
75 function processGIF (
76 path: string,
77 destination: string,
78 newSize: { width: number, height: number }
79 ): Promise<void> {
80 const command = ffmpeg(path, { niceness: FFMPEG_NICE.THUMBNAIL })
81 .fps(20)
82 .size(`${newSize.width}x${newSize.height}`)
83 .output(destination)
84
85 return runCommand({ command })
86 }
87
88 async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) {
89 const pendingImageName = 'pending-' + imageName
90
91 const options = {
92 filename: pendingImageName,
93 count: 1,
94 folder
95 }
96
97 const pendingImagePath = join(folder, pendingImageName)
98
99 try {
100 await new Promise<string>((res, rej) => {
101 ffmpeg(fromPath, { niceness: FFMPEG_NICE.THUMBNAIL })
102 .on('error', rej)
103 .on('end', () => res(imageName))
104 .thumbnail(options)
105 })
106
107 const destination = join(folder, imageName)
108 await processImage(pendingImagePath, destination, size)
109 } catch (err) {
110 logger.error('Cannot generate image from video %s.', fromPath, { err })
111
112 try {
113 await remove(pendingImagePath)
114 } catch (err) {
115 logger.debug('Cannot remove pending image path after generation error.', { err })
116 }
117 }
118 }
119
120 // ---------------------------------------------------------------------------
121 // Transcode meta function
122 // ---------------------------------------------------------------------------
123
124 type TranscodeOptionsType = 'hls' | 'hls-from-ts' | 'quick-transcode' | 'video' | 'merge-audio' | 'only-audio'
125
126 interface BaseTranscodeOptions {
127 type: TranscodeOptionsType
128
129 inputPath: string
130 outputPath: string
131
132 availableEncoders: AvailableEncoders
133 profile: string
134
135 resolution: VideoResolution
136
137 isPortraitMode?: boolean
138
139 job?: Job
140 }
141
142 interface HLSTranscodeOptions extends BaseTranscodeOptions {
143 type: 'hls'
144 copyCodecs: boolean
145 hlsPlaylist: {
146 videoFilename: string
147 }
148 }
149
150 interface HLSFromTSTranscodeOptions extends BaseTranscodeOptions {
151 type: 'hls-from-ts'
152
153 isAAC: boolean
154
155 hlsPlaylist: {
156 videoFilename: string
157 }
158 }
159
160 interface QuickTranscodeOptions extends BaseTranscodeOptions {
161 type: 'quick-transcode'
162 }
163
164 interface VideoTranscodeOptions extends BaseTranscodeOptions {
165 type: 'video'
166 }
167
168 interface MergeAudioTranscodeOptions extends BaseTranscodeOptions {
169 type: 'merge-audio'
170 audioPath: string
171 }
172
173 interface OnlyAudioTranscodeOptions extends BaseTranscodeOptions {
174 type: 'only-audio'
175 }
176
177 type TranscodeOptions =
178 HLSTranscodeOptions
179 | HLSFromTSTranscodeOptions
180 | VideoTranscodeOptions
181 | MergeAudioTranscodeOptions
182 | OnlyAudioTranscodeOptions
183 | QuickTranscodeOptions
184
185 const builders: {
186 [ type in TranscodeOptionsType ]: (c: ffmpeg.FfmpegCommand, o?: TranscodeOptions) => Promise<ffmpeg.FfmpegCommand> | ffmpeg.FfmpegCommand
187 } = {
188 'quick-transcode': buildQuickTranscodeCommand,
189 'hls': buildHLSVODCommand,
190 'hls-from-ts': buildHLSVODFromTSCommand,
191 'merge-audio': buildAudioMergeCommand,
192 'only-audio': buildOnlyAudioCommand,
193 'video': buildx264VODCommand
194 }
195
196 async function transcode (options: TranscodeOptions) {
197 logger.debug('Will run transcode.', { options })
198
199 let command = getFFmpeg(options.inputPath, 'vod')
200 .output(options.outputPath)
201
202 command = await builders[options.type](command, options)
203
204 await runCommand({ command, job: options.job })
205
206 await fixHLSPlaylistIfNeeded(options)
207 }
208
209 // ---------------------------------------------------------------------------
210 // Live muxing/transcoding functions
211 // ---------------------------------------------------------------------------
212
213 async function getLiveTranscodingCommand (options: {
214 rtmpUrl: string
215 outPath: string
216 resolutions: number[]
217 fps: number
218
219 availableEncoders: AvailableEncoders
220 profile: string
221 }) {
222 const { rtmpUrl, outPath, resolutions, fps, availableEncoders, profile } = options
223 const input = rtmpUrl
224
225 const command = getFFmpeg(input, 'live')
226
227 const varStreamMap: string[] = []
228
229 command.complexFilter([
230 {
231 inputs: '[v:0]',
232 filter: 'split',
233 options: resolutions.length,
234 outputs: resolutions.map(r => `vtemp${r}`)
235 },
236
237 ...resolutions.map(r => ({
238 inputs: `vtemp${r}`,
239 filter: 'scale',
240 options: `w=-2:h=${r}`,
241 outputs: `vout${r}`
242 }))
243 ])
244
245 command.outputOption('-preset superfast')
246 command.outputOption('-sc_threshold 0')
247
248 addDefaultEncoderGlobalParams({ command })
249
250 for (let i = 0; i < resolutions.length; i++) {
251 const resolution = resolutions[i]
252 const resolutionFPS = computeFPS(fps, resolution)
253
254 const baseEncoderBuilderParams = {
255 input,
256
257 availableEncoders,
258 profile,
259
260 fps: resolutionFPS,
261 resolution,
262 streamNum: i,
263 videoType: 'live' as 'live'
264 }
265
266 {
267 const streamType: StreamType = 'video'
268 const builderResult = await getEncoderBuilderResult(Object.assign({}, baseEncoderBuilderParams, { streamType }))
269 if (!builderResult) {
270 throw new Error('No available live video encoder found')
271 }
272
273 command.outputOption(`-map [vout${resolution}]`)
274
275 addDefaultEncoderParams({ command, encoder: builderResult.encoder, fps: resolutionFPS, streamNum: i })
276
277 logger.debug('Apply ffmpeg live video params from %s using %s profile.', builderResult.encoder, profile, builderResult)
278
279 command.outputOption(`${buildStreamSuffix('-c:v', i)} ${builderResult.encoder}`)
280 command.addInputOptions(builderResult.result.inputOptions)
281 command.addOutputOptions(builderResult.result.outputOptions)
282 }
283
284 {
285 const streamType: StreamType = 'audio'
286 const builderResult = await getEncoderBuilderResult(Object.assign({}, baseEncoderBuilderParams, { streamType }))
287 if (!builderResult) {
288 throw new Error('No available live audio encoder found')
289 }
290
291 command.outputOption('-map a:0')
292
293 addDefaultEncoderParams({ command, encoder: builderResult.encoder, fps: resolutionFPS, streamNum: i })
294
295 logger.debug('Apply ffmpeg live audio params from %s using %s profile.', builderResult.encoder, profile, builderResult)
296
297 command.outputOption(`${buildStreamSuffix('-c:a', i)} ${builderResult.encoder}`)
298 command.addInputOptions(builderResult.result.inputOptions)
299 command.addOutputOptions(builderResult.result.outputOptions)
300 }
301
302 varStreamMap.push(`v:${i},a:${i}`)
303 }
304
305 addDefaultLiveHLSParams(command, outPath)
306
307 command.outputOption('-var_stream_map', varStreamMap.join(' '))
308
309 return command
310 }
311
312 function getLiveMuxingCommand (rtmpUrl: string, outPath: string) {
313 const command = getFFmpeg(rtmpUrl, 'live')
314
315 command.outputOption('-c:v copy')
316 command.outputOption('-c:a copy')
317 command.outputOption('-map 0:a?')
318 command.outputOption('-map 0:v?')
319
320 addDefaultLiveHLSParams(command, outPath)
321
322 return command
323 }
324
325 function buildStreamSuffix (base: string, streamNum?: number) {
326 if (streamNum !== undefined) {
327 return `${base}:${streamNum}`
328 }
329
330 return base
331 }
332
333 // ---------------------------------------------------------------------------
334 // Default options
335 // ---------------------------------------------------------------------------
336
337 function addDefaultEncoderGlobalParams (options: {
338 command: ffmpeg.FfmpegCommand
339 }) {
340 const { command } = options
341
342 // avoid issues when transcoding some files: https://trac.ffmpeg.org/ticket/6375
343 command.outputOption('-max_muxing_queue_size 1024')
344 // strip all metadata
345 .outputOption('-map_metadata -1')
346 // NOTE: b-strategy 1 - heuristic algorithm, 16 is optimal B-frames for it
347 .outputOption('-b_strategy 1')
348 // NOTE: Why 16: https://github.com/Chocobozzz/PeerTube/pull/774. b-strategy 2 -> B-frames<16
349 .outputOption('-bf 16')
350 // allows import of source material with incompatible pixel formats (e.g. MJPEG video)
351 .outputOption('-pix_fmt yuv420p')
352 }
353
354 function addDefaultEncoderParams (options: {
355 command: ffmpeg.FfmpegCommand
356 encoder: 'libx264' | string
357 streamNum?: number
358 fps?: number
359 }) {
360 const { command, encoder, fps, streamNum } = options
361
362 if (encoder === 'libx264') {
363 // 3.1 is the minimal resource allocation for our highest supported resolution
364 command.outputOption(buildStreamSuffix('-level:v', streamNum) + ' 3.1')
365
366 if (fps) {
367 // Keyframe interval of 2 seconds for faster seeking and resolution switching.
368 // https://streaminglearningcenter.com/blogs/whats-the-right-keyframe-interval.html
369 // https://superuser.com/a/908325
370 command.outputOption(buildStreamSuffix('-g:v', streamNum) + ' ' + (fps * 2))
371 }
372 }
373 }
374
375 function addDefaultLiveHLSParams (command: ffmpeg.FfmpegCommand, outPath: string) {
376 command.outputOption('-hls_time ' + VIDEO_LIVE.SEGMENT_TIME_SECONDS)
377 command.outputOption('-hls_list_size ' + VIDEO_LIVE.SEGMENTS_LIST_SIZE)
378 command.outputOption('-hls_flags delete_segments+independent_segments')
379 command.outputOption(`-hls_segment_filename ${join(outPath, '%v-%06d.ts')}`)
380 command.outputOption('-master_pl_name master.m3u8')
381 command.outputOption(`-f hls`)
382
383 command.output(join(outPath, '%v.m3u8'))
384 }
385
386 // ---------------------------------------------------------------------------
387 // Transcode VOD command builders
388 // ---------------------------------------------------------------------------
389
390 async function buildx264VODCommand (command: ffmpeg.FfmpegCommand, options: TranscodeOptions) {
391 let fps = await getVideoFileFPS(options.inputPath)
392 fps = computeFPS(fps, options.resolution)
393
394 command = await presetVideo(command, options.inputPath, options, fps)
395
396 if (options.resolution !== undefined) {
397 // '?x720' or '720x?' for example
398 const size = options.isPortraitMode === true
399 ? `${options.resolution}x?`
400 : `?x${options.resolution}`
401
402 command = command.size(size)
403 }
404
405 return command
406 }
407
408 async function buildAudioMergeCommand (command: ffmpeg.FfmpegCommand, options: MergeAudioTranscodeOptions) {
409 command = command.loop(undefined)
410
411 command = await presetVideo(command, options.audioPath, options)
412
413 command.outputOption('-preset:v veryfast')
414
415 command = command.input(options.audioPath)
416 .videoFilter('scale=trunc(iw/2)*2:trunc(ih/2)*2') // Avoid "height not divisible by 2" error
417 .outputOption('-tune stillimage')
418 .outputOption('-shortest')
419
420 return command
421 }
422
423 function buildOnlyAudioCommand (command: ffmpeg.FfmpegCommand, _options: OnlyAudioTranscodeOptions) {
424 command = presetOnlyAudio(command)
425
426 return command
427 }
428
429 function buildQuickTranscodeCommand (command: ffmpeg.FfmpegCommand) {
430 command = presetCopy(command)
431
432 command = command.outputOption('-map_metadata -1') // strip all metadata
433 .outputOption('-movflags faststart')
434
435 return command
436 }
437
438 function addCommonHLSVODCommandOptions (command: ffmpeg.FfmpegCommand, outputPath: string) {
439 return command.outputOption('-hls_time 4')
440 .outputOption('-hls_list_size 0')
441 .outputOption('-hls_playlist_type vod')
442 .outputOption('-hls_segment_filename ' + outputPath)
443 .outputOption('-hls_segment_type fmp4')
444 .outputOption('-f hls')
445 .outputOption('-hls_flags single_file')
446 }
447
448 async function buildHLSVODCommand (command: ffmpeg.FfmpegCommand, options: HLSTranscodeOptions) {
449 const videoPath = getHLSVideoPath(options)
450
451 if (options.copyCodecs) command = presetCopy(command)
452 else if (options.resolution === VideoResolution.H_NOVIDEO) command = presetOnlyAudio(command)
453 else command = await buildx264VODCommand(command, options)
454
455 addCommonHLSVODCommandOptions(command, videoPath)
456
457 return command
458 }
459
460 async function buildHLSVODFromTSCommand (command: ffmpeg.FfmpegCommand, options: HLSFromTSTranscodeOptions) {
461 const videoPath = getHLSVideoPath(options)
462
463 command.outputOption('-c copy')
464
465 if (options.isAAC) {
466 // Required for example when copying an AAC stream from an MPEG-TS
467 // Since it's a bitstream filter, we don't need to reencode the audio
468 command.outputOption('-bsf:a aac_adtstoasc')
469 }
470
471 addCommonHLSVODCommandOptions(command, videoPath)
472
473 return command
474 }
475
476 async function fixHLSPlaylistIfNeeded (options: TranscodeOptions) {
477 if (options.type !== 'hls' && options.type !== 'hls-from-ts') return
478
479 const fileContent = await readFile(options.outputPath)
480
481 const videoFileName = options.hlsPlaylist.videoFilename
482 const videoFilePath = getHLSVideoPath(options)
483
484 // Fix wrong mapping with some ffmpeg versions
485 const newContent = fileContent.toString()
486 .replace(`#EXT-X-MAP:URI="${videoFilePath}",`, `#EXT-X-MAP:URI="${videoFileName}",`)
487
488 await writeFile(options.outputPath, newContent)
489 }
490
491 function getHLSVideoPath (options: HLSTranscodeOptions | HLSFromTSTranscodeOptions) {
492 return `${dirname(options.outputPath)}/${options.hlsPlaylist.videoFilename}`
493 }
494
495 // ---------------------------------------------------------------------------
496 // Transcoding presets
497 // ---------------------------------------------------------------------------
498
499 // Run encoder builder depending on available encoders
500 // Try encoders by priority: if the encoder is available, run the chosen profile or fallback to the default one
501 // If the default one does not exist, check the next encoder
502 async function getEncoderBuilderResult (options: {
503 streamType: 'video' | 'audio'
504 input: string
505
506 availableEncoders: AvailableEncoders
507 profile: string
508
509 videoType: 'vod' | 'live'
510
511 resolution: number
512 fps?: number
513 streamNum?: number
514 }) {
515 const { availableEncoders, input, profile, resolution, streamType, fps, streamNum, videoType } = options
516
517 const encodersToTry = availableEncoders.encodersToTry[videoType][streamType]
518 const encoders = availableEncoders.available[videoType]
519
520 for (const encoder of encodersToTry) {
521 if (!(await checkFFmpegEncoders(availableEncoders)).get(encoder)) {
522 logger.debug('Encoder %s not available in ffmpeg, skipping.', encoder)
523 continue
524 }
525
526 if (!encoders[encoder]) {
527 logger.debug('Encoder %s not available in peertube encoders, skipping.', encoder)
528 continue
529 }
530
531 // An object containing available profiles for this encoder
532 const builderProfiles: EncoderProfile<EncoderOptionsBuilder> = encoders[encoder]
533 let builder = builderProfiles[profile]
534
535 if (!builder) {
536 logger.debug('Profile %s for encoder %s not available. Fallback to default.', profile, encoder)
537 builder = builderProfiles.default
538
539 if (!builder) {
540 logger.debug('Default profile for encoder %s not available. Try next available encoder.', encoder)
541 continue
542 }
543 }
544
545 const result = await builder({ input, resolution: resolution, fps, streamNum })
546
547 return {
548 result,
549
550 // If we don't have output options, then copy the input stream
551 encoder: result.copy === true
552 ? 'copy'
553 : encoder
554 }
555 }
556
557 return null
558 }
559
560 async function presetVideo (
561 command: ffmpeg.FfmpegCommand,
562 input: string,
563 transcodeOptions: TranscodeOptions,
564 fps?: number
565 ) {
566 let localCommand = command
567 .format('mp4')
568 .outputOption('-movflags faststart')
569
570 addDefaultEncoderGlobalParams({ command })
571
572 // Audio encoder
573 const parsedAudio = await getAudioStream(input)
574
575 let streamsToProcess: StreamType[] = [ 'audio', 'video' ]
576
577 if (!parsedAudio.audioStream) {
578 localCommand = localCommand.noAudio()
579 streamsToProcess = [ 'video' ]
580 }
581
582 for (const streamType of streamsToProcess) {
583 const { profile, resolution, availableEncoders } = transcodeOptions
584
585 const builderResult = await getEncoderBuilderResult({
586 streamType,
587 input,
588 resolution,
589 availableEncoders,
590 profile,
591 fps,
592 videoType: 'vod' as 'vod'
593 })
594
595 if (!builderResult) {
596 throw new Error('No available encoder found for stream ' + streamType)
597 }
598
599 logger.debug(
600 'Apply ffmpeg params from %s for %s stream of input %s using %s profile.',
601 builderResult.encoder, streamType, input, profile, builderResult
602 )
603
604 if (streamType === 'video') {
605 localCommand.videoCodec(builderResult.encoder)
606 } else if (streamType === 'audio') {
607 localCommand.audioCodec(builderResult.encoder)
608 }
609
610 command.addInputOptions(builderResult.result.inputOptions)
611 command.addOutputOptions(builderResult.result.outputOptions)
612 addDefaultEncoderParams({ command: localCommand, encoder: builderResult.encoder, fps })
613 }
614
615 return localCommand
616 }
617
618 function presetCopy (command: ffmpeg.FfmpegCommand): ffmpeg.FfmpegCommand {
619 return command
620 .format('mp4')
621 .videoCodec('copy')
622 .audioCodec('copy')
623 }
624
625 function presetOnlyAudio (command: ffmpeg.FfmpegCommand): ffmpeg.FfmpegCommand {
626 return command
627 .format('mp4')
628 .audioCodec('copy')
629 .noVideo()
630 }
631
632 // ---------------------------------------------------------------------------
633 // Utils
634 // ---------------------------------------------------------------------------
635
636 function getFFmpeg (input: string, type: 'live' | 'vod') {
637 // We set cwd explicitly because ffmpeg appears to create temporary files when trancoding which fails in read-only file systems
638 const command = ffmpeg(input, {
639 niceness: type === 'live' ? FFMPEG_NICE.LIVE : FFMPEG_NICE.VOD,
640 cwd: CONFIG.STORAGE.TMP_DIR
641 })
642
643 const threads = type === 'live'
644 ? CONFIG.LIVE.TRANSCODING.THREADS
645 : CONFIG.TRANSCODING.THREADS
646
647 if (threads > 0) {
648 // If we don't set any threads ffmpeg will chose automatically
649 command.outputOption('-threads ' + threads)
650 }
651
652 return command
653 }
654
655 function getFFmpegVersion () {
656 return new Promise<string>((res, rej) => {
657 (ffmpeg() as any)._getFfmpegPath((err, ffmpegPath) => {
658 if (err) return rej(err)
659 if (!ffmpegPath) return rej(new Error('Could not find ffmpeg path'))
660
661 return execPromise(`${ffmpegPath} -version`)
662 .then(stdout => {
663 const parsed = stdout.match(/ffmpeg version .?(\d+\.\d+\.\d+)/)
664 if (!parsed || !parsed[1]) return rej(new Error(`Could not find ffmpeg version in ${stdout}`))
665
666 return res(parsed[1])
667 })
668 .catch(err => rej(err))
669 })
670 })
671 }
672
673 async function runCommand (options: {
674 command: ffmpeg.FfmpegCommand
675 silent?: boolean // false
676 job?: Job
677 }) {
678 const { command, silent = false, job } = options
679
680 return new Promise<void>((res, rej) => {
681 command.on('error', (err, stdout, stderr) => {
682 if (silent !== true) logger.error('Error in ffmpeg.', { stdout, stderr })
683
684 rej(err)
685 })
686
687 command.on('end', (stdout, stderr) => {
688 logger.debug('FFmpeg command ended.', { stdout, stderr })
689
690 res()
691 })
692
693 if (job) {
694 command.on('progress', progress => {
695 if (!progress.percent) return
696
697 job.progress(Math.round(progress.percent))
698 .catch(err => logger.warn('Cannot set ffmpeg job progress.', { err }))
699 })
700 }
701
702 command.run()
703 })
704 }
705
706 // ---------------------------------------------------------------------------
707
708 export {
709 getLiveTranscodingCommand,
710 getLiveMuxingCommand,
711 buildStreamSuffix,
712 convertWebPToJPG,
713 processGIF,
714 generateImageFromVideoFile,
715 TranscodeOptions,
716 TranscodeOptionsType,
717 transcode,
718 runCommand,
719 getFFmpegVersion,
720
721 resetSupportedEncoders,
722
723 // builders
724 buildx264VODCommand
725 }