Add audio-only option to transcoders and player
[oweals/peertube.git] / server / helpers / ffmpeg-utils.ts
1 import * as ffmpeg from 'fluent-ffmpeg'
2 import { dirname, join } from 'path'
3 import { getTargetBitrate, getMaxBitrate, VideoResolution } from '../../shared/models/videos'
4 import { 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 { readFile, remove, writeFile } from 'fs-extra'
9 import { CONFIG } from '../initializers/config'
10
11 function computeResolutionsToTranscode (videoFileHeight: number) {
12   const resolutionsEnabled: number[] = []
13   const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
14
15   // Put in the order we want to proceed jobs
16   const resolutions = [
17     VideoResolution.H_NOVIDEO,
18     VideoResolution.H_480P,
19     VideoResolution.H_360P,
20     VideoResolution.H_720P,
21     VideoResolution.H_240P,
22     VideoResolution.H_1080P,
23     VideoResolution.H_4K
24   ]
25
26   for (const resolution of resolutions) {
27     if (configResolutions[ resolution + 'p' ] === true && videoFileHeight > resolution) {
28       resolutionsEnabled.push(resolution)
29     }
30   }
31
32   return resolutionsEnabled
33 }
34
35 async function getVideoFileSize (path: string) {
36   const videoStream = await getVideoStreamFromFile(path)
37
38   return videoStream == null 
39     ? {
40         width: 0,
41         height: 0
42       }
43     : {
44         width: videoStream.width,
45         height: videoStream.height
46       }
47 }
48
49 async function getVideoFileResolution (path: string) {
50   const size = await getVideoFileSize(path)
51
52   return {
53     videoFileResolution: Math.min(size.height, size.width),
54     isPortraitMode: size.height > size.width
55   }
56 }
57
58 async function getVideoFileFPS (path: string) {
59   const videoStream = await getVideoStreamFromFile(path)
60
61   if (videoStream == null) {
62       return 0
63   }
64
65   for (const key of [ 'avg_frame_rate', 'r_frame_rate' ]) {
66     const valuesText: string = videoStream[key]
67     if (!valuesText) continue
68
69     const [ frames, seconds ] = valuesText.split('/')
70     if (!frames || !seconds) continue
71
72     const result = parseInt(frames, 10) / parseInt(seconds, 10)
73     if (result > 0) return Math.round(result)
74   }
75
76   return 0
77 }
78
79 async function getVideoFileBitrate (path: string) {
80   return new Promise<number>((res, rej) => {
81     ffmpeg.ffprobe(path, (err, metadata) => {
82       if (err) return rej(err)
83
84       return res(metadata.format.bit_rate)
85     })
86   })
87 }
88
89 function getDurationFromVideoFile (path: string) {
90   return new Promise<number>((res, rej) => {
91     ffmpeg.ffprobe(path, (err, metadata) => {
92       if (err) return rej(err)
93
94       return res(Math.floor(metadata.format.duration))
95     })
96   })
97 }
98
99 async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) {
100   const pendingImageName = 'pending-' + imageName
101
102   const options = {
103     filename: pendingImageName,
104     count: 1,
105     folder
106   }
107
108   const pendingImagePath = join(folder, pendingImageName)
109
110   try {
111     await new Promise<string>((res, rej) => {
112       ffmpeg(fromPath, { niceness: FFMPEG_NICE.THUMBNAIL })
113         .on('error', rej)
114         .on('end', () => res(imageName))
115         .thumbnail(options)
116     })
117
118     const destination = join(folder, imageName)
119     await processImage(pendingImagePath, destination, size)
120   } catch (err) {
121     logger.error('Cannot generate image from video %s.', fromPath, { err })
122
123     try {
124       await remove(pendingImagePath)
125     } catch (err) {
126       logger.debug('Cannot remove pending image path after generation error.', { err })
127     }
128   }
129 }
130
131 type TranscodeOptionsType = 'hls' | 'quick-transcode' | 'video' | 'merge-audio' | 'split-audio'
132
133 interface BaseTranscodeOptions {
134   type: TranscodeOptionsType
135   inputPath: string
136   outputPath: string
137   resolution: VideoResolution
138   isPortraitMode?: boolean
139 }
140
141 interface HLSTranscodeOptions extends BaseTranscodeOptions {
142   type: 'hls'
143   copyCodecs: boolean
144   hlsPlaylist: {
145     videoFilename: string
146   }
147 }
148
149 interface QuickTranscodeOptions extends BaseTranscodeOptions {
150   type: 'quick-transcode'
151 }
152
153 interface VideoTranscodeOptions extends BaseTranscodeOptions {
154   type: 'video'
155 }
156
157 interface MergeAudioTranscodeOptions extends BaseTranscodeOptions {
158   type: 'merge-audio'
159   audioPath: string
160 }
161
162 interface SplitAudioTranscodeOptions extends BaseTranscodeOptions {
163   type: 'split-audio'
164 }
165
166 type TranscodeOptions = HLSTranscodeOptions | VideoTranscodeOptions | MergeAudioTranscodeOptions | SplitAudioTranscodeOptions | QuickTranscodeOptions
167
168 function transcode (options: TranscodeOptions) {
169   return new Promise<void>(async (res, rej) => {
170     try {
171       let command = ffmpeg(options.inputPath, { niceness: FFMPEG_NICE.TRANSCODING })
172         .output(options.outputPath)
173
174       if (options.type === 'quick-transcode') {
175         command = await buildQuickTranscodeCommand(command)
176       } else if (options.type === 'hls') {
177         command = await buildHLSCommand(command, options)
178       } else if (options.type === 'merge-audio') {
179         command = await buildAudioMergeCommand(command, options)
180       } else if (options.type === 'split-audio') {
181         command = await buildAudioSplitCommand(command, options)
182       } else {
183         command = await buildx264Command(command, options)
184       }
185
186       if (CONFIG.TRANSCODING.THREADS > 0) {
187         // if we don't set any threads ffmpeg will chose automatically
188         command = command.outputOption('-threads ' + CONFIG.TRANSCODING.THREADS)
189       }
190
191       command
192         .on('error', (err, stdout, stderr) => {
193           logger.error('Error in transcoding job.', { stdout, stderr })
194           return rej(err)
195         })
196         .on('end', () => {
197           return fixHLSPlaylistIfNeeded(options)
198             .then(() => res())
199             .catch(err => rej(err))
200         })
201         .run()
202     } catch (err) {
203       return rej(err)
204     }
205   })
206 }
207
208 async function canDoQuickTranscode (path: string): Promise<boolean> {
209   // NOTE: This could be optimized by running ffprobe only once (but it runs fast anyway)
210   const videoStream = await getVideoStreamFromFile(path)
211   const parsedAudio = await audio.get(path)
212   const fps = await getVideoFileFPS(path)
213   const bitRate = await getVideoFileBitrate(path)
214   const resolution = await getVideoFileResolution(path)
215
216   // check video params
217   if (videoStream == null) return false
218   if (videoStream[ 'codec_name' ] !== 'h264') return false
219   if (videoStream[ 'pix_fmt' ] !== 'yuv420p') return false
220   if (fps < VIDEO_TRANSCODING_FPS.MIN || fps > VIDEO_TRANSCODING_FPS.MAX) return false
221   if (bitRate > getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)) return false
222
223     // check audio params (if audio stream exists)
224   if (parsedAudio.audioStream) {
225     if (parsedAudio.audioStream[ 'codec_name' ] !== 'aac') return false
226
227     const maxAudioBitrate = audio.bitrate[ 'aac' ](parsedAudio.audioStream[ 'bit_rate' ])
228     if (maxAudioBitrate !== -1 && parsedAudio.audioStream[ 'bit_rate' ] > maxAudioBitrate) return false
229   }
230
231   return true
232 }
233
234 // ---------------------------------------------------------------------------
235
236 export {
237   getVideoFileSize,
238   getVideoFileResolution,
239   getDurationFromVideoFile,
240   generateImageFromVideoFile,
241   TranscodeOptions,
242   TranscodeOptionsType,
243   transcode,
244   getVideoFileFPS,
245   computeResolutionsToTranscode,
246   audio,
247   getVideoFileBitrate,
248   canDoQuickTranscode
249 }
250
251 // ---------------------------------------------------------------------------
252
253 async function buildx264Command (command: ffmpeg.FfmpegCommand, options: TranscodeOptions) {
254   let fps = await getVideoFileFPS(options.inputPath)
255   // On small/medium resolutions, limit FPS
256   if (
257     options.resolution !== undefined &&
258     options.resolution < VIDEO_TRANSCODING_FPS.KEEP_ORIGIN_FPS_RESOLUTION_MIN &&
259     fps > VIDEO_TRANSCODING_FPS.AVERAGE
260   ) {
261     fps = VIDEO_TRANSCODING_FPS.AVERAGE
262   }
263
264   command = await presetH264(command, options.inputPath, options.resolution, fps)
265
266   if (options.resolution !== undefined) {
267     // '?x720' or '720x?' for example
268     const size = options.isPortraitMode === true ? `${options.resolution}x?` : `?x${options.resolution}`
269     command = command.size(size)
270   }
271
272   if (fps) {
273     // Hard FPS limits
274     if (fps > VIDEO_TRANSCODING_FPS.MAX) fps = VIDEO_TRANSCODING_FPS.MAX
275     else if (fps < VIDEO_TRANSCODING_FPS.MIN) fps = VIDEO_TRANSCODING_FPS.MIN
276
277     command = command.withFPS(fps)
278   }
279
280   return command
281 }
282
283 async function buildAudioMergeCommand (command: ffmpeg.FfmpegCommand, options: MergeAudioTranscodeOptions) {
284   command = command.loop(undefined)
285
286   command = await presetH264VeryFast(command, options.audioPath, options.resolution)
287
288   command = command.input(options.audioPath)
289                    .videoFilter('scale=trunc(iw/2)*2:trunc(ih/2)*2') // Avoid "height not divisible by 2" error
290                    .outputOption('-tune stillimage')
291                    .outputOption('-shortest')
292
293   return command
294 }
295
296 async function buildAudioSplitCommand (command: ffmpeg.FfmpegCommand, options: SplitAudioTranscodeOptions) {
297   command = await presetAudioSplit(command)
298
299   return command
300 }
301
302 async function buildQuickTranscodeCommand (command: ffmpeg.FfmpegCommand) {
303   command = await presetCopy(command)
304
305   command = command.outputOption('-map_metadata -1') // strip all metadata
306                    .outputOption('-movflags faststart')
307
308   return command
309 }
310
311 async function buildHLSCommand (command: ffmpeg.FfmpegCommand, options: HLSTranscodeOptions) {
312   const videoPath = getHLSVideoPath(options)
313
314   if (options.copyCodecs) command = await presetCopy(command)
315   else command = await buildx264Command(command, options)
316
317   command = command.outputOption('-hls_time 4')
318                    .outputOption('-hls_list_size 0')
319                    .outputOption('-hls_playlist_type vod')
320                    .outputOption('-hls_segment_filename ' + videoPath)
321                    .outputOption('-hls_segment_type fmp4')
322                    .outputOption('-f hls')
323                    .outputOption('-hls_flags single_file')
324
325   return command
326 }
327
328 function getHLSVideoPath (options: HLSTranscodeOptions) {
329   return `${dirname(options.outputPath)}/${options.hlsPlaylist.videoFilename}`
330 }
331
332 async function fixHLSPlaylistIfNeeded (options: TranscodeOptions) {
333   if (options.type !== 'hls') return
334
335   const fileContent = await readFile(options.outputPath)
336
337   const videoFileName = options.hlsPlaylist.videoFilename
338   const videoFilePath = getHLSVideoPath(options)
339
340   // Fix wrong mapping with some ffmpeg versions
341   const newContent = fileContent.toString()
342                                 .replace(`#EXT-X-MAP:URI="${videoFilePath}",`, `#EXT-X-MAP:URI="${videoFileName}",`)
343
344   await writeFile(options.outputPath, newContent)
345 }
346
347 function getVideoStreamFromFile (path: string) {
348   return new Promise<any>((res, rej) => {
349     ffmpeg.ffprobe(path, (err, metadata) => {
350       if (err) return rej(err)
351
352       const videoStream = metadata.streams.find(s => s.codec_type === 'video')
353       //if (!videoStream) return rej(new Error('Cannot find video stream of ' + path))
354
355       return res(videoStream)
356     })
357   })
358 }
359
360 /**
361  * A slightly customised version of the 'veryfast' x264 preset
362  *
363  * The veryfast preset is right in the sweet spot of performance
364  * and quality. Superfast and ultrafast will give you better
365  * performance, but then quality is noticeably worse.
366  */
367 async function presetH264VeryFast (command: ffmpeg.FfmpegCommand, input: string, resolution: VideoResolution, fps?: number) {
368   let localCommand = await presetH264(command, input, resolution, fps)
369
370   localCommand = localCommand.outputOption('-preset:v veryfast')
371
372   /*
373   MAIN reference: https://slhck.info/video/2017/03/01/rate-control.html
374   Our target situation is closer to a livestream than a stream,
375   since we want to reduce as much a possible the encoding burden,
376   although not to the point of a livestream where there is a hard
377   constraint on the frames per second to be encoded.
378   */
379
380   return localCommand
381 }
382
383 /**
384  * A toolbox to play with audio
385  */
386 namespace audio {
387   export const get = (option: string) => {
388     // without position, ffprobe considers the last input only
389     // we make it consider the first input only
390     // if you pass a file path to pos, then ffprobe acts on that file directly
391     return new Promise<{ absolutePath: string, audioStream?: any }>((res, rej) => {
392
393       function parseFfprobe (err: any, data: ffmpeg.FfprobeData) {
394         if (err) return rej(err)
395
396         if ('streams' in data) {
397           const audioStream = data.streams.find(stream => stream['codec_type'] === 'audio')
398           if (audioStream) {
399             return res({
400               absolutePath: data.format.filename,
401               audioStream
402             })
403           }
404         }
405
406         return res({ absolutePath: data.format.filename })
407       }
408
409       return ffmpeg.ffprobe(option, parseFfprobe)
410     })
411   }
412
413   export namespace bitrate {
414     const baseKbitrate = 384
415
416     const toBits = (kbits: number) => kbits * 8000
417
418     export const aac = (bitrate: number): number => {
419       switch (true) {
420         case bitrate > toBits(baseKbitrate):
421           return baseKbitrate
422
423         default:
424           return -1 // we interpret it as a signal to copy the audio stream as is
425       }
426     }
427
428     export const mp3 = (bitrate: number): number => {
429       /*
430       a 192kbit/sec mp3 doesn't hold as much information as a 192kbit/sec aac.
431       That's why, when using aac, we can go to lower kbit/sec. The equivalences
432       made here are not made to be accurate, especially with good mp3 encoders.
433       */
434       switch (true) {
435         case bitrate <= toBits(192):
436           return 128
437
438         case bitrate <= toBits(384):
439           return 256
440
441         default:
442           return baseKbitrate
443       }
444     }
445   }
446 }
447
448 /**
449  * Standard profile, with variable bitrate audio and faststart.
450  *
451  * As for the audio, quality '5' is the highest and ensures 96-112kbps/channel
452  * See https://trac.ffmpeg.org/wiki/Encode/AAC#fdk_vbr
453  */
454 async function presetH264 (command: ffmpeg.FfmpegCommand, input: string, resolution: VideoResolution, fps?: number) {
455   let localCommand = command
456     .format('mp4')
457     .videoCodec('libx264')
458     .outputOption('-level 3.1') // 3.1 is the minimal ressource allocation for our highest supported resolution
459     .outputOption('-b_strategy 1') // NOTE: b-strategy 1 - heuristic algorythm, 16 is optimal B-frames for it
460     .outputOption('-bf 16') // NOTE: Why 16: https://github.com/Chocobozzz/PeerTube/pull/774. b-strategy 2 -> B-frames<16
461     .outputOption('-pix_fmt yuv420p') // allows import of source material with incompatible pixel formats (e.g. MJPEG video)
462     .outputOption('-map_metadata -1') // strip all metadata
463     .outputOption('-movflags faststart')
464
465   const parsedAudio = await audio.get(input)
466
467   if (!parsedAudio.audioStream) {
468     localCommand = localCommand.noAudio()
469   } else if ((await checkFFmpegEncoders()).get('libfdk_aac')) { // we favor VBR, if a good AAC encoder is available
470     localCommand = localCommand
471       .audioCodec('libfdk_aac')
472       .audioQuality(5)
473   } else {
474     // we try to reduce the ceiling bitrate by making rough matches of bitrates
475     // of course this is far from perfect, but it might save some space in the end
476     localCommand = localCommand.audioCodec('aac')
477
478     const audioCodecName = parsedAudio.audioStream[ 'codec_name' ]
479
480     if (audio.bitrate[ audioCodecName ]) {
481       const bitrate = audio.bitrate[ audioCodecName ](parsedAudio.audioStream[ 'bit_rate' ])
482       if (bitrate !== undefined && bitrate !== -1) localCommand = localCommand.audioBitrate(bitrate)
483     }
484   }
485
486   if (fps) {
487     // Constrained Encoding (VBV)
488     // https://slhck.info/video/2017/03/01/rate-control.html
489     // https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate
490     const targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS)
491     localCommand = localCommand.outputOptions([ `-maxrate ${targetBitrate}`, `-bufsize ${targetBitrate * 2}` ])
492
493     // Keyframe interval of 2 seconds for faster seeking and resolution switching.
494     // https://streaminglearningcenter.com/blogs/whats-the-right-keyframe-interval.html
495     // https://superuser.com/a/908325
496     localCommand = localCommand.outputOption(`-g ${fps * 2}`)
497   }
498
499   return localCommand
500 }
501
502 async function presetCopy (command: ffmpeg.FfmpegCommand): Promise<ffmpeg.FfmpegCommand> {
503   return command
504     .format('mp4')
505     .videoCodec('copy')
506     .audioCodec('copy')
507 }
508
509
510 async function presetAudioSplit (command: ffmpeg.FfmpegCommand): Promise<ffmpeg.FfmpegCommand> {
511   return command
512     .format('mp4')
513     .audioCodec('copy')
514     .noVideo()
515 }