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