]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/ffmpeg-utils.ts
Fix crash regarding video stream issue
[github/Chocobozzz/PeerTube.git] / server / helpers / ffmpeg-utils.ts
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'
9
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
32 async function getVideoFileResolution (path: string) {
33 const videoStream = await getVideoFileStream(path)
34
35 return {
36 videoFileResolution: Math.min(videoStream.height, videoStream.width),
37 isPortraitMode: videoStream.height > videoStream.width
38 }
39 }
40
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)
52 if (result > 0) return Math.round(result)
53 }
54
55 return 0
56 }
57
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
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
78 async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) {
79 const pendingImageName = 'pending-' + imageName
80
81 const options = {
82 filename: pendingImageName,
83 count: 1,
84 folder
85 }
86
87 const pendingImagePath = join(folder, pendingImageName)
88
89 try {
90 await new Promise<string>((res, rej) => {
91 ffmpeg(fromPath, { niceness: FFMPEG_NICE.THUMBNAIL })
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) {
100 logger.error('Cannot generate image from video %s.', fromPath, { err })
101
102 try {
103 await remove(pendingImagePath)
104 } catch (err) {
105 logger.debug('Cannot remove pending image path after generation error.', { err })
106 }
107 }
108 }
109
110 type TranscodeOptions = {
111 inputPath: string
112 outputPath: string
113 resolution?: VideoResolution
114 isPortraitMode?: boolean
115 }
116
117 function transcode (options: TranscodeOptions) {
118 return new Promise<void>(async (res, rej) => {
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 }
129
130 let command = ffmpeg(options.inputPath, { niceness: FFMPEG_NICE.TRANSCODING })
131 .output(options.outputPath)
132 command = await presetH264(command, options.resolution, fps)
133
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 }
138
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 }
144
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
149
150 command = command.withFPS(fps)
151 }
152
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 }
163 })
164 }
165
166 // ---------------------------------------------------------------------------
167
168 export {
169 getVideoFileResolution,
170 getDurationFromVideoFile,
171 generateImageFromVideoFile,
172 transcode,
173 getVideoFileFPS,
174 computeResolutionsToTranscode,
175 audio,
176 getVideoFileBitrate
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) return rej(new Error('Cannot find video stream of ' + path))
188
189 return res(videoStream)
190 })
191 })
192 }
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 */
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' ])
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 */
216
217 return localCommand
218 }
219
220 /**
221 * A preset optimised for a stillimage audio video
222 */
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
232 }
233
234 /**
235 * A toolbox to play with audio
236 */
237 namespace 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) => {
243
244 function parseFfprobe (err: any, data: ffmpeg.FfprobeData) {
245 if (err) return rej(err)
246
247 if ('streams' in data) {
248 const audioStream = data.streams.find(stream => stream['codec_type'] === 'audio')
249 if (audioStream) {
250 return res({
251 absolutePath: data.format.filename,
252 audioStream
253 })
254 }
255 }
256
257 return res({ absolutePath: data.format.filename })
258 }
259
260 if (typeof option === 'string') {
261 return ffmpeg.ffprobe(option, parseFfprobe)
262 }
263
264 return option.ffprobe(parseFfprobe)
265 })
266 }
267
268 export namespace bitrate {
269 const baseKbitrate = 384
270
271 const toBits = (kbits: number): number => { return kbits * 8000 }
272
273 export const aac = (bitrate: number): number => {
274 switch (true) {
275 case bitrate > toBits(baseKbitrate):
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 => {
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 */
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 */
306 async function presetH264 (command: ffmpeg.FfmpegCommand, resolution: VideoResolution, fps: number): Promise<ffmpeg.FfmpegCommand> {
307 let localCommand = command
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
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')
316
317 const parsedAudio = await audio.get(localCommand)
318
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')
324 .audioQuality(5)
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 localCommand = localCommand.audioCodec('aac')
332
333 bitrate = audio.bitrate[ audioCodecName ](parsedAudio.audioStream[ 'bit_rate' ])
334 if (bitrate !== undefined && bitrate !== -1) localCommand = localCommand.audioBitrate(bitrate)
335 }
336 }
337
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 }`])
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
347 localCommand = localCommand.outputOption(`-g ${ fps * 2 }`)
348
349 return localCommand
350 }