1 import * as ffmpeg from 'fluent-ffmpeg'
2 import { 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'
10 function computeResolutionsToTranscode (videoFileHeight: number) {
11 const resolutionsEnabled: number[] = []
12 const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
14 // Put in the order we want to proceed jobs
16 VideoResolution.H_480P,
17 VideoResolution.H_360P,
18 VideoResolution.H_720P,
19 VideoResolution.H_240P,
20 VideoResolution.H_1080P
23 for (const resolution of resolutions) {
24 if (configResolutions[ resolution + 'p' ] === true && videoFileHeight > resolution) {
25 resolutionsEnabled.push(resolution)
29 return resolutionsEnabled
32 async function getVideoFileResolution (path: string) {
33 const videoStream = await getVideoFileStream(path)
36 videoFileResolution: Math.min(videoStream.height, videoStream.width),
37 isPortraitMode: videoStream.height > videoStream.width
41 async function getVideoFileFPS (path: string) {
42 const videoStream = await getVideoFileStream(path)
44 for (const key of [ 'avg_frame_rate', 'r_frame_rate' ]) {
45 const valuesText: string = videoStream[key]
46 if (!valuesText) continue
48 const [ frames, seconds ] = valuesText.split('/')
49 if (!frames || !seconds) continue
51 const result = parseInt(frames, 10) / parseInt(seconds, 10)
52 if (result > 0) return Math.round(result)
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)
63 return res(metadata.format.bit_rate)
68 function getDurationFromVideoFile (path: string) {
69 return new Promise<number>((res, rej) => {
70 ffmpeg.ffprobe(path, (err, metadata) => {
71 if (err) return rej(err)
73 return res(Math.floor(metadata.format.duration))
78 async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) {
79 const pendingImageName = 'pending-' + imageName
82 filename: pendingImageName,
87 const pendingImagePath = join(folder, pendingImageName)
90 await new Promise<string>((res, rej) => {
91 ffmpeg(fromPath, { niceness: FFMPEG_NICE.THUMBNAIL })
93 .on('end', () => res(imageName))
97 const destination = join(folder, imageName)
98 await processImage({ path: pendingImagePath }, destination, size)
100 logger.error('Cannot generate image from video %s.', fromPath, { err })
103 await remove(pendingImagePath)
105 logger.debug('Cannot remove pending image path after generation error.', { err })
110 type TranscodeOptions = {
113 resolution?: VideoResolution
114 isPortraitMode?: boolean
117 function transcode (options: TranscodeOptions) {
118 return new Promise<void>(async (res, rej) => {
120 let fps = await getVideoFileFPS(options.inputPath)
121 // On small/medium resolutions, limit FPS
123 options.resolution !== undefined &&
124 options.resolution < VIDEO_TRANSCODING_FPS.KEEP_ORIGIN_FPS_RESOLUTION_MIN &&
125 fps > VIDEO_TRANSCODING_FPS.AVERAGE
127 fps = VIDEO_TRANSCODING_FPS.AVERAGE
130 let command = ffmpeg(options.inputPath, { niceness: FFMPEG_NICE.TRANSCODING })
131 .output(options.outputPath)
132 command = await presetH264(command, options.resolution, fps)
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)
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)
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
150 command = command.withFPS(fps)
154 .on('error', (err, stdout, stderr) => {
155 logger.error('Error in transcoding job.', { stdout, stderr })
166 // ---------------------------------------------------------------------------
169 getVideoFileResolution,
170 getDurationFromVideoFile,
171 generateImageFromVideoFile,
174 computeResolutionsToTranscode,
179 // ---------------------------------------------------------------------------
181 function getVideoFileStream (path: string) {
182 return new Promise<any>((res, rej) => {
183 ffmpeg.ffprobe(path, (err, metadata) => {
184 if (err) return rej(err)
186 const videoStream = metadata.streams.find(s => s.codec_type === 'video')
187 if (!videoStream) return rej(new Error('Cannot find video stream of ' + path))
189 return res(videoStream)
195 * A slightly customised version of the 'veryfast' x264 preset
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.
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' ])
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.
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.
221 * A preset optimised for a stillimage audio video
223 async function presetStillImageWithAudio (
224 command: ffmpeg.FfmpegCommand,
225 resolution: VideoResolution,
227 ): Promise<ffmpeg.FfmpegCommand> {
228 let localCommand = await presetH264VeryFast(command, resolution, fps)
229 localCommand = localCommand.outputOption('-tune stillimage')
235 * A toolbox to play with audio
238 export const get = (option: ffmpeg.FfmpegCommand | string) => {
239 // without position, ffprobe considers the last input only
240 // we make it consider the first input only
241 // if you pass a file path to pos, then ffprobe acts on that file directly
242 return new Promise<{ absolutePath: string, audioStream?: any }>((res, rej) => {
244 function parseFfprobe (err: any, data: ffmpeg.FfprobeData) {
245 if (err) return rej(err)
247 if ('streams' in data) {
248 const audioStream = data.streams.find(stream => stream['codec_type'] === 'audio')
251 absolutePath: data.format.filename,
257 return res({ absolutePath: data.format.filename })
260 if (typeof option === 'string') {
261 return ffmpeg.ffprobe(option, parseFfprobe)
264 return option.ffprobe(parseFfprobe)
268 export namespace bitrate {
269 const baseKbitrate = 384
271 const toBits = (kbits: number): number => { return kbits * 8000 }
273 export const aac = (bitrate: number): number => {
275 case bitrate > toBits(baseKbitrate):
278 return -1 // we interpret it as a signal to copy the audio stream as is
282 export const mp3 = (bitrate: number): number => {
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.
289 case bitrate <= toBits(192):
291 case bitrate <= toBits(384):
301 * Standard profile, with variable bitrate audio and faststart.
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
306 async function presetH264 (command: ffmpeg.FfmpegCommand, resolution: VideoResolution, fps: number): Promise<ffmpeg.FfmpegCommand> {
307 let localCommand = command
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
313 .outputOption('-pix_fmt yuv420p') // allows import of source material with incompatible pixel formats (e.g. MJPEG video)
314 .outputOption('-map_metadata -1') // strip all metadata
315 .outputOption('-movflags faststart')
317 const parsedAudio = await audio.get(localCommand)
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
323 .audioCodec('libfdk_aac')
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' ]
330 if (audio.bitrate[ audioCodecName ]) {
331 localCommand = localCommand.audioCodec('aac')
333 bitrate = audio.bitrate[ audioCodecName ](parsedAudio.audioStream[ 'bit_rate' ])
334 if (bitrate !== undefined && bitrate !== -1) localCommand = localCommand.audioBitrate(bitrate)
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)
342 localCommand = localCommand.outputOptions([`-maxrate ${ targetBitrate }`, `-bufsize ${ targetBitrate * 2 }`])
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
347 localCommand = localCommand.outputOption(`-g ${ fps * 2 }`)