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