]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/ffmpeg-utils.ts
Add RSS feed to subscribe button
[github/Chocobozzz/PeerTube.git] / server / helpers / ffmpeg-utils.ts
CommitLineData
14d3270f 1import * as ffmpeg from 'fluent-ffmpeg'
6fdc553a 2import { join } from 'path'
3fd3ab2d 3import { VideoResolution } from '../../shared/models/videos'
62689b94 4import { CONFIG, FFMPEG_NICE, VIDEO_TRANSCODING_FPS } from '../initializers'
26670720 5import { processImage } from './image-utils'
6fdc553a 6import { logger } from './logger'
e5565833 7import { checkFFmpegEncoders } from '../initializers/checker-before-init'
62689b94 8import { remove } from 'fs-extra'
14d3270f 9
06215f15
C
10function 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
056aa7f2 32async function getVideoFileResolution (path: string) {
73c69591 33 const videoStream = await getVideoFileStream(path)
056aa7f2
C
34
35 return {
36 videoFileResolution: Math.min(videoStream.height, videoStream.width),
37 isPortraitMode: videoStream.height > videoStream.width
38 }
73c69591 39}
14d3270f 40
73c69591
C
41async 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)
3a6f351b 52 if (result > 0) return Math.round(result)
73c69591
C
53 }
54
55 return 0
14d3270f
C
56}
57
58function getDurationFromVideoFile (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(Math.floor(metadata.format.duration))
64 })
65 })
66}
67
26670720
C
68async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) {
69 const pendingImageName = 'pending-' + imageName
70
14d3270f 71 const options = {
26670720 72 filename: pendingImageName,
14d3270f
C
73 count: 1,
74 folder
75 }
76
26670720 77 const pendingImagePath = join(folder, pendingImageName)
6fdc553a
C
78
79 try {
80 await new Promise<string>((res, rej) => {
7160878c 81 ffmpeg(fromPath, { niceness: FFMPEG_NICE.THUMBNAIL })
6fdc553a
C
82 .on('error', rej)
83 .on('end', () => res(imageName))
84 .thumbnail(options)
85 })
86
87 const destination = join(folder, imageName)
88 await processImage({ path: pendingImagePath }, destination, size)
89 } catch (err) {
d5b7d911 90 logger.error('Cannot generate image from video %s.', fromPath, { err })
6fdc553a
C
91
92 try {
62689b94 93 await remove(pendingImagePath)
6fdc553a 94 } catch (err) {
d5b7d911 95 logger.debug('Cannot remove pending image path after generation error.', { err })
6fdc553a
C
96 }
97 }
14d3270f
C
98}
99
100type TranscodeOptions = {
101 inputPath: string
102 outputPath: string
103 resolution?: VideoResolution
056aa7f2 104 isPortraitMode?: boolean
14d3270f
C
105}
106
107function transcode (options: TranscodeOptions) {
73c69591 108 return new Promise<void>(async (res, rej) => {
7160878c 109 let command = ffmpeg(options.inputPath, { niceness: FFMPEG_NICE.TRANSCODING })
14d3270f 110 .output(options.outputPath)
19ca8ca9 111 .preset(standard)
7160878c 112
991feec9 113 if (CONFIG.TRANSCODING.THREADS > 0) {
7160878c
RK
114 // if we don't set any threads ffmpeg will chose automatically
115 command = command.outputOption('-threads ' + CONFIG.TRANSCODING.THREADS)
991feec9 116 }
14d3270f 117
3a6f351b 118 let fps = await getVideoFileFPS(options.inputPath)
14d3270f 119 if (options.resolution !== undefined) {
056aa7f2
C
120 // '?x720' or '720x?' for example
121 const size = options.isPortraitMode === true ? `${options.resolution}x?` : `?x${options.resolution}`
14d3270f 122 command = command.size(size)
3a6f351b
C
123
124 // On small/medium resolutions, limit FPS
125 if (
126 options.resolution < VIDEO_TRANSCODING_FPS.KEEP_ORIGIN_FPS_RESOLUTION_MIN &&
127 fps > VIDEO_TRANSCODING_FPS.AVERAGE
128 ) {
129 fps = VIDEO_TRANSCODING_FPS.AVERAGE
130 }
131 }
132
133 if (fps) {
134 // Hard FPS limits
135 if (fps > VIDEO_TRANSCODING_FPS.MAX) fps = VIDEO_TRANSCODING_FPS.MAX
136 else if (fps < VIDEO_TRANSCODING_FPS.MIN) fps = VIDEO_TRANSCODING_FPS.MIN
137
138 command = command.withFPS(fps)
14d3270f
C
139 }
140
747b2990
C
141 command
142 .on('error', (err, stdout, stderr) => {
143 logger.error('Error in transcoding job.', { stdout, stderr })
144 return rej(err)
145 })
146 .on('end', res)
147 .run()
14d3270f
C
148 })
149}
150
151// ---------------------------------------------------------------------------
152
153export {
056aa7f2 154 getVideoFileResolution,
14d3270f
C
155 getDurationFromVideoFile,
156 generateImageFromVideoFile,
73c69591 157 transcode,
7160878c 158 getVideoFileFPS,
06215f15 159 computeResolutionsToTranscode,
7160878c 160 audio
73c69591
C
161}
162
163// ---------------------------------------------------------------------------
164
165function getVideoFileStream (path: string) {
166 return new Promise<any>((res, rej) => {
167 ffmpeg.ffprobe(path, (err, metadata) => {
168 if (err) return rej(err)
169
170 const videoStream = metadata.streams.find(s => s.codec_type === 'video')
171 if (!videoStream) throw new Error('Cannot find video stream of ' + path)
172
173 return res(videoStream)
174 })
175 })
14d3270f 176}
4176e227
RK
177
178/**
179 * A slightly customised version of the 'veryfast' x264 preset
180 *
181 * The veryfast preset is right in the sweet spot of performance
182 * and quality. Superfast and ultrafast will give you better
183 * performance, but then quality is noticeably worse.
184 */
4a5ccac5
RK
185function veryfast (_ffmpeg) {
186 _ffmpeg
4176e227
RK
187 .preset(standard)
188 .outputOption('-preset:v veryfast')
189 .outputOption(['--aq-mode=2', '--aq-strength=1.3'])
190 /*
191 MAIN reference: https://slhck.info/video/2017/03/01/rate-control.html
192 Our target situation is closer to a livestream than a stream,
193 since we want to reduce as much a possible the encoding burden,
194 altough not to the point of a livestream where there is a hard
195 constraint on the frames per second to be encoded.
196
197 why '--aq-mode=2 --aq-strength=1.3' instead of '-profile:v main'?
198 Make up for most of the loss of grain and macroblocking
199 with less computing power.
200 */
201}
202
203/**
204 * A preset optimised for a stillimage audio video
205 */
4a5ccac5
RK
206function audio (_ffmpeg) {
207 _ffmpeg
4176e227
RK
208 .preset(veryfast)
209 .outputOption('-tune stillimage')
210}
211
212/**
213 * A toolbox to play with audio
214 */
215namespace audio {
4a5ccac5 216 export const get = (_ffmpeg, pos: number | string = 0) => {
4176e227
RK
217 // without position, ffprobe considers the last input only
218 // we make it consider the first input only
4a5ccac5 219 // if you pass a file path to pos, then ffprobe acts on that file directly
7160878c
RK
220 return new Promise<{ absolutePath: string, audioStream?: any }>((res, rej) => {
221 _ffmpeg.ffprobe(pos, (err,data) => {
222 if (err) return rej(err)
223
224 if ('streams' in data) {
225 const audioStream = data['streams'].find(stream => stream['codec_type'] === 'audio')
226 if (audioStream) {
227 return res({
228 absolutePath: data.format.filename,
229 audioStream
230 })
4a5ccac5 231 }
7160878c
RK
232 }
233 return res({ absolutePath: data.format.filename })
234 })
4a5ccac5 235 })
4176e227
RK
236 }
237
238 export namespace bitrate {
eed24d26 239 const baseKbitrate = 384
4176e227
RK
240
241 const toBits = (kbits: number): number => { return kbits * 8000 }
242
243 export const aac = (bitrate: number): number => {
244 switch (true) {
7160878c 245 case bitrate > toBits(baseKbitrate):
4176e227
RK
246 return baseKbitrate
247 default:
248 return -1 // we interpret it as a signal to copy the audio stream as is
249 }
250 }
251
252 export const mp3 = (bitrate: number): number => {
7160878c
RK
253 /*
254 a 192kbit/sec mp3 doesn't hold as much information as a 192kbit/sec aac.
255 That's why, when using aac, we can go to lower kbit/sec. The equivalences
256 made here are not made to be accurate, especially with good mp3 encoders.
257 */
4176e227
RK
258 switch (true) {
259 case bitrate <= toBits(192):
260 return 128
261 case bitrate <= toBits(384):
262 return 256
263 default:
264 return baseKbitrate
265 }
266 }
267 }
268}
269
270/**
271 * Standard profile, with variable bitrate audio and faststart.
272 *
273 * As for the audio, quality '5' is the highest and ensures 96-112kbps/channel
274 * See https://trac.ffmpeg.org/wiki/Encode/AAC#fdk_vbr
275 */
4a5ccac5 276async function standard (_ffmpeg) {
4a5ccac5 277 let localFfmpeg = _ffmpeg
4176e227
RK
278 .format('mp4')
279 .videoCodec('libx264')
280 .outputOption('-level 3.1') // 3.1 is the minimal ressource allocation for our highest supported resolution
281 .outputOption('-b_strategy 1') // NOTE: b-strategy 1 - heuristic algorythm, 16 is optimal B-frames for it
282 .outputOption('-bf 16') // NOTE: Why 16: https://github.com/Chocobozzz/PeerTube/pull/774. b-strategy 2 -> B-frames<16
4a5ccac5 283 .outputOption('-map_metadata -1') // strip all metadata
4176e227 284 .outputOption('-movflags faststart')
7160878c 285 const _audio = await audio.get(localFfmpeg)
4176e227 286
7160878c
RK
287 if (!_audio.audioStream) {
288 return localFfmpeg.noAudio()
289 }
4176e227 290
4176e227
RK
291 // we favor VBR, if a good AAC encoder is available
292 if ((await checkFFmpegEncoders()).get('libfdk_aac')) {
4a5ccac5 293 return localFfmpeg
4176e227
RK
294 .audioCodec('libfdk_aac')
295 .audioQuality(5)
296 }
297
eed24d26
C
298 // we try to reduce the ceiling bitrate by making rough correspondances of bitrates
299 // of course this is far from perfect, but it might save some space in the end
300 const audioCodecName = _audio.audioStream['codec_name']
301 let bitrate: number
302 if (audio.bitrate[audioCodecName]) {
303 bitrate = audio.bitrate[audioCodecName](_audio.audioStream['bit_rate'])
304
305 if (bitrate === -1) return localFfmpeg.audioCodec('copy')
306 }
307
308 if (bitrate !== undefined) return localFfmpeg.audioBitrate(bitrate)
309
310 return localFfmpeg
4176e227 311}