]>
Commit | Line | Data |
---|---|---|
14d3270f | 1 | import * as ffmpeg from 'fluent-ffmpeg' |
053aed43 | 2 | import { readFile, remove, writeFile } from 'fs-extra' |
09209296 | 3 | import { dirname, join } from 'path' |
c6c0fa6c | 4 | import { VideoFileMetadata } from '@shared/models/videos/video-file-metadata' |
a1587156 | 5 | import { getMaxBitrate, getTargetBitrate, VideoResolution } from '../../shared/models/videos' |
c6c0fa6c C |
6 | import { checkFFmpegEncoders } from '../initializers/checker-before-init' |
7 | import { CONFIG } from '../initializers/config' | |
fb719404 | 8 | import { FFMPEG_NICE, VIDEO_LIVE, VIDEO_TRANSCODING_FPS } from '../initializers/constants' |
26670720 | 9 | import { processImage } from './image-utils' |
6fdc553a | 10 | import { logger } from './logger' |
14d3270f | 11 | |
a1587156 C |
12 | /** |
13 | * A toolbox to play with audio | |
14 | */ | |
15 | namespace audio { | |
16 | export const get = (videoPath: string) => { | |
17 | // without position, ffprobe considers the last input only | |
18 | // we make it consider the first input only | |
19 | // if you pass a file path to pos, then ffprobe acts on that file directly | |
20 | return new Promise<{ absolutePath: string, audioStream?: any }>((res, rej) => { | |
21 | ||
22 | function parseFfprobe (err: any, data: ffmpeg.FfprobeData) { | |
23 | if (err) return rej(err) | |
24 | ||
25 | if ('streams' in data) { | |
26 | const audioStream = data.streams.find(stream => stream['codec_type'] === 'audio') | |
27 | if (audioStream) { | |
28 | return res({ | |
29 | absolutePath: data.format.filename, | |
30 | audioStream | |
31 | }) | |
32 | } | |
33 | } | |
34 | ||
35 | return res({ absolutePath: data.format.filename }) | |
36 | } | |
37 | ||
38 | return ffmpeg.ffprobe(videoPath, parseFfprobe) | |
39 | }) | |
40 | } | |
41 | ||
42 | export namespace bitrate { | |
43 | const baseKbitrate = 384 | |
44 | ||
45 | const toBits = (kbits: number) => kbits * 8000 | |
46 | ||
47 | export const aac = (bitrate: number): number => { | |
48 | switch (true) { | |
49 | case bitrate > toBits(baseKbitrate): | |
50 | return baseKbitrate | |
51 | ||
52 | default: | |
53 | return -1 // we interpret it as a signal to copy the audio stream as is | |
54 | } | |
55 | } | |
56 | ||
57 | export const mp3 = (bitrate: number): number => { | |
58 | /* | |
59 | a 192kbit/sec mp3 doesn't hold as much information as a 192kbit/sec aac. | |
60 | That's why, when using aac, we can go to lower kbit/sec. The equivalences | |
61 | made here are not made to be accurate, especially with good mp3 encoders. | |
62 | */ | |
63 | switch (true) { | |
64 | case bitrate <= toBits(192): | |
65 | return 128 | |
66 | ||
67 | case bitrate <= toBits(384): | |
68 | return 256 | |
69 | ||
70 | default: | |
71 | return baseKbitrate | |
72 | } | |
73 | } | |
74 | } | |
75 | } | |
76 | ||
c6c0fa6c C |
77 | function computeResolutionsToTranscode (videoFileResolution: number, type: 'vod' | 'live') { |
78 | const configResolutions = type === 'vod' | |
79 | ? CONFIG.TRANSCODING.RESOLUTIONS | |
80 | : CONFIG.LIVE.TRANSCODING.RESOLUTIONS | |
81 | ||
06215f15 | 82 | const resolutionsEnabled: number[] = [] |
06215f15 C |
83 | |
84 | // Put in the order we want to proceed jobs | |
85 | const resolutions = [ | |
5c7d6508 | 86 | VideoResolution.H_NOVIDEO, |
06215f15 C |
87 | VideoResolution.H_480P, |
88 | VideoResolution.H_360P, | |
89 | VideoResolution.H_720P, | |
90 | VideoResolution.H_240P, | |
ad3405d0 C |
91 | VideoResolution.H_1080P, |
92 | VideoResolution.H_4K | |
06215f15 C |
93 | ] |
94 | ||
95 | for (const resolution of resolutions) { | |
dca0fe12 | 96 | if (configResolutions[resolution + 'p'] === true && videoFileResolution > resolution) { |
06215f15 C |
97 | resolutionsEnabled.push(resolution) |
98 | } | |
99 | } | |
100 | ||
101 | return resolutionsEnabled | |
102 | } | |
103 | ||
52201311 | 104 | async function getVideoStreamSize (path: string) { |
5ba49f26 | 105 | const videoStream = await getVideoStreamFromFile(path) |
056aa7f2 | 106 | |
3a149e9f C |
107 | return videoStream === null |
108 | ? { width: 0, height: 0 } | |
109 | : { width: videoStream.width, height: videoStream.height } | |
09209296 C |
110 | } |
111 | ||
52201311 C |
112 | async function getVideoStreamCodec (path: string) { |
113 | const videoStream = await getVideoStreamFromFile(path) | |
114 | ||
115 | if (!videoStream) return '' | |
116 | ||
117 | const videoCodec = videoStream.codec_tag_string | |
118 | ||
119 | const baseProfileMatrix = { | |
a1587156 C |
120 | High: '6400', |
121 | Main: '4D40', | |
122 | Baseline: '42E0' | |
52201311 C |
123 | } |
124 | ||
125 | let baseProfile = baseProfileMatrix[videoStream.profile] | |
126 | if (!baseProfile) { | |
127 | logger.warn('Cannot get video profile codec of %s.', path, { videoStream }) | |
128 | baseProfile = baseProfileMatrix['High'] // Fallback | |
129 | } | |
130 | ||
a2b6ec7c C |
131 | let level = videoStream.level.toString(16) |
132 | if (level.length === 1) level = `0${level}` | |
52201311 C |
133 | |
134 | return `${videoCodec}.${baseProfile}${level}` | |
135 | } | |
136 | ||
137 | async function getAudioStreamCodec (path: string) { | |
138 | const { audioStream } = await audio.get(path) | |
139 | ||
140 | if (!audioStream) return '' | |
141 | ||
142 | const audioCodec = audioStream.codec_name | |
49c3bf6f | 143 | if (audioCodec === 'aac') return 'mp4a.40.2' |
52201311 C |
144 | |
145 | logger.warn('Cannot get audio codec of %s.', path, { audioStream }) | |
146 | ||
147 | return 'mp4a.40.2' // Fallback | |
148 | } | |
149 | ||
09209296 | 150 | async function getVideoFileResolution (path: string) { |
52201311 | 151 | const size = await getVideoStreamSize(path) |
09209296 C |
152 | |
153 | return { | |
154 | videoFileResolution: Math.min(size.height, size.width), | |
155 | isPortraitMode: size.height > size.width | |
056aa7f2 | 156 | } |
73c69591 | 157 | } |
14d3270f | 158 | |
73c69591 | 159 | async function getVideoFileFPS (path: string) { |
5ba49f26 | 160 | const videoStream = await getVideoStreamFromFile(path) |
3a149e9f | 161 | if (videoStream === null) return 0 |
5c7d6508 | 162 | |
ef04ae20 | 163 | for (const key of [ 'avg_frame_rate', 'r_frame_rate' ]) { |
a1587156 | 164 | const valuesText: string = videoStream[key] |
73c69591 C |
165 | if (!valuesText) continue |
166 | ||
167 | const [ frames, seconds ] = valuesText.split('/') | |
168 | if (!frames || !seconds) continue | |
169 | ||
170 | const result = parseInt(frames, 10) / parseInt(seconds, 10) | |
3a6f351b | 171 | if (result > 0) return Math.round(result) |
73c69591 C |
172 | } |
173 | ||
174 | return 0 | |
14d3270f C |
175 | } |
176 | ||
7b81edc8 | 177 | async function getMetadataFromFile <T> (path: string, cb = metadata => metadata) { |
8319d6ae | 178 | return new Promise<T>((res, rej) => { |
edb4ffc7 FA |
179 | ffmpeg.ffprobe(path, (err, metadata) => { |
180 | if (err) return rej(err) | |
181 | ||
8319d6ae | 182 | return res(cb(new VideoFileMetadata(metadata))) |
edb4ffc7 FA |
183 | }) |
184 | }) | |
185 | } | |
186 | ||
8319d6ae RK |
187 | async function getVideoFileBitrate (path: string) { |
188 | return getMetadataFromFile<number>(path, metadata => metadata.format.bit_rate) | |
189 | } | |
190 | ||
14d3270f | 191 | function getDurationFromVideoFile (path: string) { |
8319d6ae RK |
192 | return getMetadataFromFile<number>(path, metadata => Math.floor(metadata.format.duration)) |
193 | } | |
14d3270f | 194 | |
8319d6ae RK |
195 | function getVideoStreamFromFile (path: string) { |
196 | return getMetadataFromFile<any>(path, metadata => metadata.streams.find(s => s.codec_type === 'video') || null) | |
14d3270f C |
197 | } |
198 | ||
26670720 C |
199 | async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) { |
200 | const pendingImageName = 'pending-' + imageName | |
201 | ||
14d3270f | 202 | const options = { |
26670720 | 203 | filename: pendingImageName, |
14d3270f C |
204 | count: 1, |
205 | folder | |
206 | } | |
207 | ||
26670720 | 208 | const pendingImagePath = join(folder, pendingImageName) |
6fdc553a C |
209 | |
210 | try { | |
211 | await new Promise<string>((res, rej) => { | |
7160878c | 212 | ffmpeg(fromPath, { niceness: FFMPEG_NICE.THUMBNAIL }) |
6fdc553a C |
213 | .on('error', rej) |
214 | .on('end', () => res(imageName)) | |
215 | .thumbnail(options) | |
216 | }) | |
217 | ||
218 | const destination = join(folder, imageName) | |
2fb5b3a5 | 219 | await processImage(pendingImagePath, destination, size) |
6fdc553a | 220 | } catch (err) { |
d5b7d911 | 221 | logger.error('Cannot generate image from video %s.', fromPath, { err }) |
6fdc553a C |
222 | |
223 | try { | |
62689b94 | 224 | await remove(pendingImagePath) |
6fdc553a | 225 | } catch (err) { |
d5b7d911 | 226 | logger.debug('Cannot remove pending image path after generation error.', { err }) |
6fdc553a C |
227 | } |
228 | } | |
14d3270f C |
229 | } |
230 | ||
3a149e9f | 231 | type TranscodeOptionsType = 'hls' | 'quick-transcode' | 'video' | 'merge-audio' | 'only-audio' |
536598cf C |
232 | |
233 | interface BaseTranscodeOptions { | |
234 | type: TranscodeOptionsType | |
14d3270f C |
235 | inputPath: string |
236 | outputPath: string | |
09209296 | 237 | resolution: VideoResolution |
056aa7f2 | 238 | isPortraitMode?: boolean |
536598cf | 239 | } |
09209296 | 240 | |
536598cf C |
241 | interface HLSTranscodeOptions extends BaseTranscodeOptions { |
242 | type: 'hls' | |
d7a25329 | 243 | copyCodecs: boolean |
536598cf | 244 | hlsPlaylist: { |
4c280004 C |
245 | videoFilename: string |
246 | } | |
14d3270f C |
247 | } |
248 | ||
536598cf C |
249 | interface QuickTranscodeOptions extends BaseTranscodeOptions { |
250 | type: 'quick-transcode' | |
251 | } | |
252 | ||
253 | interface VideoTranscodeOptions extends BaseTranscodeOptions { | |
254 | type: 'video' | |
255 | } | |
256 | ||
257 | interface MergeAudioTranscodeOptions extends BaseTranscodeOptions { | |
258 | type: 'merge-audio' | |
259 | audioPath: string | |
260 | } | |
261 | ||
3a149e9f C |
262 | interface OnlyAudioTranscodeOptions extends BaseTranscodeOptions { |
263 | type: 'only-audio' | |
5c7d6508 | 264 | } |
265 | ||
a1587156 C |
266 | type TranscodeOptions = |
267 | HLSTranscodeOptions | |
3a149e9f C |
268 | | VideoTranscodeOptions |
269 | | MergeAudioTranscodeOptions | |
270 | | OnlyAudioTranscodeOptions | |
271 | | QuickTranscodeOptions | |
536598cf | 272 | |
14d3270f | 273 | function transcode (options: TranscodeOptions) { |
73c69591 | 274 | return new Promise<void>(async (res, rej) => { |
cdf4cb9e | 275 | try { |
c6c0fa6c | 276 | let command = getFFmpeg(options.inputPath) |
cdf4cb9e | 277 | .output(options.outputPath) |
14aed608 | 278 | |
536598cf | 279 | if (options.type === 'quick-transcode') { |
a1587156 | 280 | command = buildQuickTranscodeCommand(command) |
536598cf | 281 | } else if (options.type === 'hls') { |
c6c0fa6c | 282 | command = await buildHLSVODCommand(command, options) |
536598cf C |
283 | } else if (options.type === 'merge-audio') { |
284 | command = await buildAudioMergeCommand(command, options) | |
3a149e9f | 285 | } else if (options.type === 'only-audio') { |
a1587156 | 286 | command = buildOnlyAudioCommand(command, options) |
14aed608 C |
287 | } else { |
288 | command = await buildx264Command(command, options) | |
289 | } | |
7160878c | 290 | |
cdf4cb9e C |
291 | command |
292 | .on('error', (err, stdout, stderr) => { | |
293 | logger.error('Error in transcoding job.', { stdout, stderr }) | |
294 | return rej(err) | |
295 | }) | |
7f8f8bdb | 296 | .on('end', () => { |
536598cf | 297 | return fixHLSPlaylistIfNeeded(options) |
7f8f8bdb C |
298 | .then(() => res()) |
299 | .catch(err => rej(err)) | |
300 | }) | |
cdf4cb9e C |
301 | .run() |
302 | } catch (err) { | |
303 | return rej(err) | |
304 | } | |
14d3270f C |
305 | }) |
306 | } | |
307 | ||
7ed2c1a4 | 308 | async function canDoQuickTranscode (path: string): Promise<boolean> { |
5ba49f26 FA |
309 | // NOTE: This could be optimized by running ffprobe only once (but it runs fast anyway) |
310 | const videoStream = await getVideoStreamFromFile(path) | |
311 | const parsedAudio = await audio.get(path) | |
312 | const fps = await getVideoFileFPS(path) | |
313 | const bitRate = await getVideoFileBitrate(path) | |
314 | const resolution = await getVideoFileResolution(path) | |
315 | ||
316 | // check video params | |
5c7d6508 | 317 | if (videoStream == null) return false |
a1587156 C |
318 | if (videoStream['codec_name'] !== 'h264') return false |
319 | if (videoStream['pix_fmt'] !== 'yuv420p') return false | |
1600235a C |
320 | if (fps < VIDEO_TRANSCODING_FPS.MIN || fps > VIDEO_TRANSCODING_FPS.MAX) return false |
321 | if (bitRate > getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)) return false | |
5ba49f26 | 322 | |
3a149e9f | 323 | // check audio params (if audio stream exists) |
5ba49f26 | 324 | if (parsedAudio.audioStream) { |
a1587156 | 325 | if (parsedAudio.audioStream['codec_name'] !== 'aac') return false |
1600235a | 326 | |
a1587156 C |
327 | const maxAudioBitrate = audio.bitrate['aac'](parsedAudio.audioStream['bit_rate']) |
328 | if (maxAudioBitrate !== -1 && parsedAudio.audioStream['bit_rate'] > maxAudioBitrate) return false | |
5ba49f26 | 329 | } |
7ed2c1a4 | 330 | |
5ba49f26 FA |
331 | return true |
332 | } | |
333 | ||
c7f36e4f C |
334 | function getClosestFramerateStandard (fps: number, type: 'HD_STANDARD' | 'STANDARD'): number { |
335 | return VIDEO_TRANSCODING_FPS[type].slice(0) | |
336 | .sort((a, b) => fps % a - fps % b)[0] | |
837666fe RK |
337 | } |
338 | ||
18782242 C |
339 | function convertWebPToJPG (path: string, destination: string): Promise<void> { |
340 | return new Promise<void>(async (res, rej) => { | |
341 | try { | |
342 | const command = ffmpeg(path).output(destination) | |
343 | ||
344 | command.on('error', (err, stdout, stderr) => { | |
345 | logger.error('Error in ffmpeg webp convert process.', { stdout, stderr }) | |
346 | return rej(err) | |
347 | }) | |
348 | .on('end', () => res()) | |
349 | .run() | |
350 | } catch (err) { | |
351 | return rej(err) | |
352 | } | |
353 | }) | |
354 | } | |
355 | ||
68e70a74 | 356 | function runLiveTranscoding (rtmpUrl: string, outPath: string, resolutions: number[], fps, deleteSegments: boolean) { |
c6c0fa6c C |
357 | const command = getFFmpeg(rtmpUrl) |
358 | command.inputOption('-fflags nobuffer') | |
359 | ||
360 | const varStreamMap: string[] = [] | |
361 | ||
362 | command.complexFilter([ | |
363 | { | |
364 | inputs: '[v:0]', | |
365 | filter: 'split', | |
366 | options: resolutions.length, | |
367 | outputs: resolutions.map(r => `vtemp${r}`) | |
368 | }, | |
369 | ||
370 | ...resolutions.map(r => ({ | |
371 | inputs: `vtemp${r}`, | |
372 | filter: 'scale', | |
373 | options: `w=-2:h=${r}`, | |
374 | outputs: `vout${r}` | |
375 | })) | |
376 | ]) | |
377 | ||
c6c0fa6c C |
378 | command.outputOption('-b_strategy 1') |
379 | command.outputOption('-bf 16') | |
380 | command.outputOption('-preset superfast') | |
381 | command.outputOption('-level 3.1') | |
382 | command.outputOption('-map_metadata -1') | |
383 | command.outputOption('-pix_fmt yuv420p') | |
77e9f859 | 384 | command.outputOption('-max_muxing_queue_size 1024') |
68e70a74 | 385 | command.outputOption('-g ' + (fps * 2)) |
c6c0fa6c C |
386 | |
387 | for (let i = 0; i < resolutions.length; i++) { | |
388 | const resolution = resolutions[i] | |
389 | ||
390 | command.outputOption(`-map [vout${resolution}]`) | |
391 | command.outputOption(`-c:v:${i} libx264`) | |
68e70a74 | 392 | command.outputOption(`-b:v:${i} ${getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS)}`) |
c6c0fa6c C |
393 | |
394 | command.outputOption(`-map a:0`) | |
395 | command.outputOption(`-c:a:${i} aac`) | |
396 | ||
397 | varStreamMap.push(`v:${i},a:${i}`) | |
398 | } | |
399 | ||
fb719404 | 400 | addDefaultLiveHLSParams(command, outPath, deleteSegments) |
c6c0fa6c C |
401 | |
402 | command.outputOption('-var_stream_map', varStreamMap.join(' ')) | |
403 | ||
404 | command.run() | |
405 | ||
406 | return command | |
407 | } | |
408 | ||
fb719404 | 409 | function runLiveMuxing (rtmpUrl: string, outPath: string, deleteSegments: boolean) { |
c6c0fa6c C |
410 | const command = getFFmpeg(rtmpUrl) |
411 | command.inputOption('-fflags nobuffer') | |
412 | ||
413 | command.outputOption('-c:v copy') | |
414 | command.outputOption('-c:a copy') | |
415 | command.outputOption('-map 0:a?') | |
416 | command.outputOption('-map 0:v?') | |
417 | ||
fb719404 | 418 | addDefaultLiveHLSParams(command, outPath, deleteSegments) |
c6c0fa6c C |
419 | |
420 | command.run() | |
421 | ||
422 | return command | |
423 | } | |
424 | ||
31c82cd9 | 425 | async function hlsPlaylistToFragmentedMP4 (hlsDirectory: string, segmentFiles: string[], outputPath: string) { |
3bc68dfd C |
426 | const concatFilePath = join(hlsDirectory, 'concat.txt') |
427 | ||
428 | function cleaner () { | |
429 | remove(concatFilePath) | |
430 | .catch(err => logger.error('Cannot remove concat file in %s.', hlsDirectory, { err })) | |
431 | } | |
432 | ||
433 | // First concat the ts files to a mp4 file | |
31c82cd9 C |
434 | const content = segmentFiles.map(f => 'file ' + f) |
435 | .join('\n') | |
436 | ||
437 | await writeFile(concatFilePath, content + '\n') | |
438 | ||
439 | const command = getFFmpeg(concatFilePath) | |
440 | command.inputOption('-safe 0') | |
441 | command.inputOption('-f concat') | |
b5b68755 | 442 | |
3bc68dfd C |
443 | command.outputOption('-c:v copy') |
444 | command.audioFilter('aresample=async=1:first_pts=0') | |
b5b68755 C |
445 | command.output(outputPath) |
446 | ||
3bc68dfd C |
447 | return runCommand(command, cleaner) |
448 | } | |
b5b68755 | 449 | |
3bc68dfd C |
450 | async function runCommand (command: ffmpeg.FfmpegCommand, onEnd?: Function) { |
451 | command.run() | |
31c82cd9 | 452 | |
b5b68755 | 453 | return new Promise<string>((res, rej) => { |
31c82cd9 | 454 | command.on('error', err => { |
3bc68dfd | 455 | if (onEnd) onEnd() |
31c82cd9 C |
456 | |
457 | rej(err) | |
458 | }) | |
459 | ||
460 | command.on('end', () => { | |
3bc68dfd | 461 | if (onEnd) onEnd() |
31c82cd9 C |
462 | |
463 | res() | |
464 | }) | |
b5b68755 C |
465 | }) |
466 | } | |
467 | ||
14d3270f C |
468 | // --------------------------------------------------------------------------- |
469 | ||
470 | export { | |
52201311 C |
471 | getVideoStreamCodec, |
472 | getAudioStreamCodec, | |
c6c0fa6c | 473 | runLiveMuxing, |
18782242 | 474 | convertWebPToJPG, |
52201311 | 475 | getVideoStreamSize, |
056aa7f2 | 476 | getVideoFileResolution, |
8319d6ae | 477 | getMetadataFromFile, |
14d3270f | 478 | getDurationFromVideoFile, |
c6c0fa6c | 479 | runLiveTranscoding, |
14d3270f | 480 | generateImageFromVideoFile, |
536598cf C |
481 | TranscodeOptions, |
482 | TranscodeOptionsType, | |
73c69591 | 483 | transcode, |
7160878c | 484 | getVideoFileFPS, |
06215f15 | 485 | computeResolutionsToTranscode, |
edb4ffc7 | 486 | audio, |
b5b68755 | 487 | hlsPlaylistToFragmentedMP4, |
5ba49f26 FA |
488 | getVideoFileBitrate, |
489 | canDoQuickTranscode | |
73c69591 C |
490 | } |
491 | ||
492 | // --------------------------------------------------------------------------- | |
493 | ||
c6c0fa6c C |
494 | function addDefaultX264Params (command: ffmpeg.FfmpegCommand) { |
495 | command.outputOption('-level 3.1') // 3.1 is the minimal resource allocation for our highest supported resolution | |
496 | .outputOption('-b_strategy 1') // NOTE: b-strategy 1 - heuristic algorithm, 16 is optimal B-frames for it | |
497 | .outputOption('-bf 16') // NOTE: Why 16: https://github.com/Chocobozzz/PeerTube/pull/774. b-strategy 2 -> B-frames<16 | |
498 | .outputOption('-pix_fmt yuv420p') // allows import of source material with incompatible pixel formats (e.g. MJPEG video) | |
499 | .outputOption('-map_metadata -1') // strip all metadata | |
500 | } | |
501 | ||
fb719404 | 502 | function addDefaultLiveHLSParams (command: ffmpeg.FfmpegCommand, outPath: string, deleteSegments: boolean) { |
68e70a74 | 503 | command.outputOption('-hls_time ' + VIDEO_LIVE.SEGMENT_TIME_SECONDS) |
fb719404 C |
504 | command.outputOption('-hls_list_size ' + VIDEO_LIVE.SEGMENTS_LIST_SIZE) |
505 | ||
506 | if (deleteSegments === true) { | |
507 | command.outputOption('-hls_flags delete_segments') | |
508 | } | |
509 | ||
3bc68dfd | 510 | command.outputOption(`-hls_segment_filename ${join(outPath, '%v-%04d.ts')}`) |
c6c0fa6c C |
511 | command.outputOption('-master_pl_name master.m3u8') |
512 | command.outputOption(`-f hls`) | |
513 | ||
514 | command.output(join(outPath, '%v.m3u8')) | |
515 | } | |
516 | ||
d7a25329 | 517 | async function buildx264Command (command: ffmpeg.FfmpegCommand, options: TranscodeOptions) { |
14aed608 | 518 | let fps = await getVideoFileFPS(options.inputPath) |
14aed608 | 519 | if ( |
06bcfbd9 | 520 | // On small/medium resolutions, limit FPS |
14aed608 C |
521 | options.resolution !== undefined && |
522 | options.resolution < VIDEO_TRANSCODING_FPS.KEEP_ORIGIN_FPS_RESOLUTION_MIN && | |
c7f36e4f | 523 | fps > VIDEO_TRANSCODING_FPS.AVERAGE |
14aed608 | 524 | ) { |
06bcfbd9 | 525 | // Get closest standard framerate by modulo: downsampling has to be done to a divisor of the nominal fps value |
c7f36e4f | 526 | fps = getClosestFramerateStandard(fps, 'STANDARD') |
14aed608 C |
527 | } |
528 | ||
536598cf | 529 | command = await presetH264(command, options.inputPath, options.resolution, fps) |
14aed608 C |
530 | |
531 | if (options.resolution !== undefined) { | |
532 | // '?x720' or '720x?' for example | |
533 | const size = options.isPortraitMode === true ? `${options.resolution}x?` : `?x${options.resolution}` | |
534 | command = command.size(size) | |
535 | } | |
536 | ||
537 | if (fps) { | |
538 | // Hard FPS limits | |
c7f36e4f | 539 | if (fps > VIDEO_TRANSCODING_FPS.MAX) fps = getClosestFramerateStandard(fps, 'HD_STANDARD') |
14aed608 C |
540 | else if (fps < VIDEO_TRANSCODING_FPS.MIN) fps = VIDEO_TRANSCODING_FPS.MIN |
541 | ||
542 | command = command.withFPS(fps) | |
543 | } | |
544 | ||
545 | return command | |
546 | } | |
547 | ||
536598cf C |
548 | async function buildAudioMergeCommand (command: ffmpeg.FfmpegCommand, options: MergeAudioTranscodeOptions) { |
549 | command = command.loop(undefined) | |
550 | ||
551 | command = await presetH264VeryFast(command, options.audioPath, options.resolution) | |
552 | ||
553 | command = command.input(options.audioPath) | |
554 | .videoFilter('scale=trunc(iw/2)*2:trunc(ih/2)*2') // Avoid "height not divisible by 2" error | |
555 | .outputOption('-tune stillimage') | |
556 | .outputOption('-shortest') | |
557 | ||
558 | return command | |
559 | } | |
560 | ||
a1587156 C |
561 | function buildOnlyAudioCommand (command: ffmpeg.FfmpegCommand, options: OnlyAudioTranscodeOptions) { |
562 | command = presetOnlyAudio(command) | |
5c7d6508 | 563 | |
564 | return command | |
565 | } | |
566 | ||
a1587156 C |
567 | function buildQuickTranscodeCommand (command: ffmpeg.FfmpegCommand) { |
568 | command = presetCopy(command) | |
536598cf C |
569 | |
570 | command = command.outputOption('-map_metadata -1') // strip all metadata | |
571 | .outputOption('-movflags faststart') | |
572 | ||
573 | return command | |
574 | } | |
575 | ||
c6c0fa6c | 576 | async function buildHLSVODCommand (command: ffmpeg.FfmpegCommand, options: HLSTranscodeOptions) { |
14aed608 C |
577 | const videoPath = getHLSVideoPath(options) |
578 | ||
a1587156 | 579 | if (options.copyCodecs) command = presetCopy(command) |
1c320673 | 580 | else if (options.resolution === VideoResolution.H_NOVIDEO) command = presetOnlyAudio(command) |
d7a25329 | 581 | else command = await buildx264Command(command, options) |
14aed608 C |
582 | |
583 | command = command.outputOption('-hls_time 4') | |
584 | .outputOption('-hls_list_size 0') | |
585 | .outputOption('-hls_playlist_type vod') | |
586 | .outputOption('-hls_segment_filename ' + videoPath) | |
587 | .outputOption('-hls_segment_type fmp4') | |
588 | .outputOption('-f hls') | |
589 | .outputOption('-hls_flags single_file') | |
590 | ||
591 | return command | |
592 | } | |
593 | ||
536598cf | 594 | function getHLSVideoPath (options: HLSTranscodeOptions) { |
7f8f8bdb C |
595 | return `${dirname(options.outputPath)}/${options.hlsPlaylist.videoFilename}` |
596 | } | |
597 | ||
536598cf C |
598 | async function fixHLSPlaylistIfNeeded (options: TranscodeOptions) { |
599 | if (options.type !== 'hls') return | |
7f8f8bdb | 600 | |
7f8f8bdb C |
601 | const fileContent = await readFile(options.outputPath) |
602 | ||
603 | const videoFileName = options.hlsPlaylist.videoFilename | |
604 | const videoFilePath = getHLSVideoPath(options) | |
605 | ||
536598cf | 606 | // Fix wrong mapping with some ffmpeg versions |
7f8f8bdb C |
607 | const newContent = fileContent.toString() |
608 | .replace(`#EXT-X-MAP:URI="${videoFilePath}",`, `#EXT-X-MAP:URI="${videoFileName}",`) | |
609 | ||
610 | await writeFile(options.outputPath, newContent) | |
611 | } | |
612 | ||
4176e227 RK |
613 | /** |
614 | * A slightly customised version of the 'veryfast' x264 preset | |
615 | * | |
616 | * The veryfast preset is right in the sweet spot of performance | |
617 | * and quality. Superfast and ultrafast will give you better | |
618 | * performance, but then quality is noticeably worse. | |
619 | */ | |
536598cf C |
620 | async function presetH264VeryFast (command: ffmpeg.FfmpegCommand, input: string, resolution: VideoResolution, fps?: number) { |
621 | let localCommand = await presetH264(command, input, resolution, fps) | |
622 | ||
cdf4cb9e | 623 | localCommand = localCommand.outputOption('-preset:v veryfast') |
536598cf | 624 | |
4176e227 RK |
625 | /* |
626 | MAIN reference: https://slhck.info/video/2017/03/01/rate-control.html | |
627 | Our target situation is closer to a livestream than a stream, | |
628 | since we want to reduce as much a possible the encoding burden, | |
536598cf | 629 | although not to the point of a livestream where there is a hard |
4176e227 | 630 | constraint on the frames per second to be encoded. |
4176e227 | 631 | */ |
cdf4cb9e C |
632 | |
633 | return localCommand | |
4176e227 RK |
634 | } |
635 | ||
4176e227 RK |
636 | /** |
637 | * Standard profile, with variable bitrate audio and faststart. | |
638 | * | |
639 | * As for the audio, quality '5' is the highest and ensures 96-112kbps/channel | |
640 | * See https://trac.ffmpeg.org/wiki/Encode/AAC#fdk_vbr | |
641 | */ | |
536598cf | 642 | async function presetH264 (command: ffmpeg.FfmpegCommand, input: string, resolution: VideoResolution, fps?: number) { |
cdf4cb9e | 643 | let localCommand = command |
4176e227 RK |
644 | .format('mp4') |
645 | .videoCodec('libx264') | |
4176e227 | 646 | .outputOption('-movflags faststart') |
4176e227 | 647 | |
c6c0fa6c C |
648 | addDefaultX264Params(localCommand) |
649 | ||
536598cf | 650 | const parsedAudio = await audio.get(input) |
4176e227 | 651 | |
cdf4cb9e C |
652 | if (!parsedAudio.audioStream) { |
653 | localCommand = localCommand.noAudio() | |
654 | } else if ((await checkFFmpegEncoders()).get('libfdk_aac')) { // we favor VBR, if a good AAC encoder is available | |
655 | localCommand = localCommand | |
4176e227 RK |
656 | .audioCodec('libfdk_aac') |
657 | .audioQuality(5) | |
cdf4cb9e | 658 | } else { |
536598cf | 659 | // we try to reduce the ceiling bitrate by making rough matches of bitrates |
cdf4cb9e | 660 | // of course this is far from perfect, but it might save some space in the end |
536598cf C |
661 | localCommand = localCommand.audioCodec('aac') |
662 | ||
a1587156 | 663 | const audioCodecName = parsedAudio.audioStream['codec_name'] |
cdf4cb9e | 664 | |
a1587156 C |
665 | if (audio.bitrate[audioCodecName]) { |
666 | const bitrate = audio.bitrate[audioCodecName](parsedAudio.audioStream['bit_rate']) | |
64e3e270 | 667 | if (bitrate !== undefined && bitrate !== -1) localCommand = localCommand.audioBitrate(bitrate) |
cdf4cb9e | 668 | } |
4176e227 RK |
669 | } |
670 | ||
536598cf C |
671 | if (fps) { |
672 | // Constrained Encoding (VBV) | |
673 | // https://slhck.info/video/2017/03/01/rate-control.html | |
674 | // https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate | |
675 | const targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS) | |
676 | localCommand = localCommand.outputOptions([ `-maxrate ${targetBitrate}`, `-bufsize ${targetBitrate * 2}` ]) | |
677 | ||
678 | // Keyframe interval of 2 seconds for faster seeking and resolution switching. | |
679 | // https://streaminglearningcenter.com/blogs/whats-the-right-keyframe-interval.html | |
680 | // https://superuser.com/a/908325 | |
681 | localCommand = localCommand.outputOption(`-g ${fps * 2}`) | |
682 | } | |
bcf21a37 | 683 | |
cdf4cb9e | 684 | return localCommand |
4176e227 | 685 | } |
14aed608 | 686 | |
a1587156 | 687 | function presetCopy (command: ffmpeg.FfmpegCommand): ffmpeg.FfmpegCommand { |
14aed608 C |
688 | return command |
689 | .format('mp4') | |
690 | .videoCodec('copy') | |
691 | .audioCodec('copy') | |
692 | } | |
5c7d6508 | 693 | |
a1587156 | 694 | function presetOnlyAudio (command: ffmpeg.FfmpegCommand): ffmpeg.FfmpegCommand { |
5c7d6508 | 695 | return command |
696 | .format('mp4') | |
697 | .audioCodec('copy') | |
698 | .noVideo() | |
699 | } | |
c6c0fa6c C |
700 | |
701 | function getFFmpeg (input: string) { | |
702 | // We set cwd explicitly because ffmpeg appears to create temporary files when trancoding which fails in read-only file systems | |
703 | const command = ffmpeg(input, { niceness: FFMPEG_NICE.TRANSCODING, cwd: CONFIG.STORAGE.TMP_DIR }) | |
704 | ||
705 | if (CONFIG.TRANSCODING.THREADS > 0) { | |
706 | // If we don't set any threads ffmpeg will chose automatically | |
707 | command.outputOption('-threads ' + CONFIG.TRANSCODING.THREADS) | |
708 | } | |
709 | ||
710 | return command | |
711 | } |