]>
Commit | Line | Data |
---|---|---|
3b01f4c0 | 1 | import { Job } from 'bull' |
14d3270f | 2 | import * as ffmpeg from 'fluent-ffmpeg' |
053aed43 | 3 | import { readFile, remove, writeFile } from 'fs-extra' |
09209296 | 4 | import { dirname, join } from 'path' |
529b3752 | 5 | import { FFMPEG_NICE, VIDEO_LIVE } from '@server/initializers/constants' |
1896bca0 | 6 | import { AvailableEncoders, EncoderOptionsBuilder, EncoderProfile, VideoResolution } from '../../shared/models/videos' |
c6c0fa6c | 7 | import { CONFIG } from '../initializers/config' |
1896bca0 | 8 | import { promisify0 } from './core-utils' |
884d2c39 | 9 | import { computeFPS, getAudioStream, getVideoFileFPS } from './ffprobe-utils' |
26670720 | 10 | import { processImage } from './image-utils' |
6fdc553a | 11 | import { logger } from './logger' |
14d3270f | 12 | |
6b67897e C |
13 | /** |
14 | * | |
15 | * Functions that run transcoding/muxing ffmpeg processes | |
16 | * Mainly called by lib/video-transcoding.ts and lib/live-manager.ts | |
17 | * | |
18 | */ | |
19 | ||
9252a33d C |
20 | // --------------------------------------------------------------------------- |
21 | // Encoder options | |
22 | // --------------------------------------------------------------------------- | |
23 | ||
1896bca0 | 24 | type StreamType = 'audio' | 'video' |
9252a33d | 25 | |
1896bca0 C |
26 | // --------------------------------------------------------------------------- |
27 | // Encoders support | |
28 | // --------------------------------------------------------------------------- | |
9252a33d | 29 | |
1896bca0 C |
30 | // Detect supported encoders by ffmpeg |
31 | let supportedEncoders: Map<string, boolean> | |
32 | async function checkFFmpegEncoders (peertubeAvailableEncoders: AvailableEncoders): Promise<Map<string, boolean>> { | |
33 | if (supportedEncoders !== undefined) { | |
34 | return supportedEncoders | |
35 | } | |
9252a33d | 36 | |
1896bca0 C |
37 | const getAvailableEncodersPromise = promisify0(ffmpeg.getAvailableEncoders) |
38 | const availableFFmpegEncoders = await getAvailableEncodersPromise() | |
9252a33d | 39 | |
1896bca0 C |
40 | const searchEncoders = new Set<string>() |
41 | for (const type of [ 'live', 'vod' ]) { | |
42 | for (const streamType of [ 'audio', 'video' ]) { | |
43 | for (const encoder of peertubeAvailableEncoders.encodersToTry[type][streamType]) { | |
44 | searchEncoders.add(encoder) | |
45 | } | |
46 | } | |
47 | } | |
9252a33d | 48 | |
1896bca0 | 49 | supportedEncoders = new Map<string, boolean>() |
9252a33d | 50 | |
1896bca0 C |
51 | for (const searchEncoder of searchEncoders) { |
52 | supportedEncoders.set(searchEncoder, availableFFmpegEncoders[searchEncoder] !== undefined) | |
529b3752 C |
53 | } |
54 | ||
1896bca0 | 55 | logger.info('Built supported ffmpeg encoders.', { supportedEncoders, searchEncoders }) |
529b3752 | 56 | |
1896bca0 | 57 | return supportedEncoders |
9252a33d C |
58 | } |
59 | ||
1896bca0 C |
60 | function resetSupportedEncoders () { |
61 | supportedEncoders = undefined | |
62 | } | |
529b3752 | 63 | |
9252a33d C |
64 | // --------------------------------------------------------------------------- |
65 | // Image manipulation | |
66 | // --------------------------------------------------------------------------- | |
67 | ||
68 | function convertWebPToJPG (path: string, destination: string): Promise<void> { | |
a4a8cd39 | 69 | const command = ffmpeg(path, { niceness: FFMPEG_NICE.THUMBNAIL }) |
9252a33d C |
70 | .output(destination) |
71 | ||
72 | return runCommand(command) | |
73 | } | |
74 | ||
75 | function processGIF ( | |
76 | path: string, | |
77 | destination: string, | |
f619de0e | 78 | newSize: { width: number, height: number } |
9252a33d | 79 | ): Promise<void> { |
a4a8cd39 | 80 | const command = ffmpeg(path, { niceness: FFMPEG_NICE.THUMBNAIL }) |
f619de0e C |
81 | .fps(20) |
82 | .size(`${newSize.width}x${newSize.height}`) | |
83 | .output(destination) | |
9252a33d | 84 | |
f619de0e | 85 | return runCommand(command) |
9252a33d C |
86 | } |
87 | ||
26670720 C |
88 | async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) { |
89 | const pendingImageName = 'pending-' + imageName | |
90 | ||
14d3270f | 91 | const options = { |
26670720 | 92 | filename: pendingImageName, |
14d3270f C |
93 | count: 1, |
94 | folder | |
95 | } | |
96 | ||
26670720 | 97 | const pendingImagePath = join(folder, pendingImageName) |
6fdc553a C |
98 | |
99 | try { | |
100 | await new Promise<string>((res, rej) => { | |
7160878c | 101 | ffmpeg(fromPath, { niceness: FFMPEG_NICE.THUMBNAIL }) |
6fdc553a C |
102 | .on('error', rej) |
103 | .on('end', () => res(imageName)) | |
104 | .thumbnail(options) | |
105 | }) | |
106 | ||
107 | const destination = join(folder, imageName) | |
2fb5b3a5 | 108 | await processImage(pendingImagePath, destination, size) |
6fdc553a | 109 | } catch (err) { |
d5b7d911 | 110 | logger.error('Cannot generate image from video %s.', fromPath, { err }) |
6fdc553a C |
111 | |
112 | try { | |
62689b94 | 113 | await remove(pendingImagePath) |
6fdc553a | 114 | } catch (err) { |
d5b7d911 | 115 | logger.debug('Cannot remove pending image path after generation error.', { err }) |
6fdc553a C |
116 | } |
117 | } | |
14d3270f C |
118 | } |
119 | ||
daf6e480 C |
120 | // --------------------------------------------------------------------------- |
121 | // Transcode meta function | |
122 | // --------------------------------------------------------------------------- | |
123 | ||
2650d6d4 | 124 | type TranscodeOptionsType = 'hls' | 'hls-from-ts' | 'quick-transcode' | 'video' | 'merge-audio' | 'only-audio' |
536598cf C |
125 | |
126 | interface BaseTranscodeOptions { | |
127 | type: TranscodeOptionsType | |
9252a33d | 128 | |
14d3270f C |
129 | inputPath: string |
130 | outputPath: string | |
9252a33d C |
131 | |
132 | availableEncoders: AvailableEncoders | |
133 | profile: string | |
134 | ||
09209296 | 135 | resolution: VideoResolution |
9252a33d | 136 | |
056aa7f2 | 137 | isPortraitMode?: boolean |
3b01f4c0 C |
138 | |
139 | job?: Job | |
536598cf | 140 | } |
09209296 | 141 | |
536598cf C |
142 | interface HLSTranscodeOptions extends BaseTranscodeOptions { |
143 | type: 'hls' | |
d7a25329 | 144 | copyCodecs: boolean |
536598cf | 145 | hlsPlaylist: { |
4c280004 C |
146 | videoFilename: string |
147 | } | |
14d3270f C |
148 | } |
149 | ||
2650d6d4 C |
150 | interface HLSFromTSTranscodeOptions extends BaseTranscodeOptions { |
151 | type: 'hls-from-ts' | |
152 | ||
e772bdf1 C |
153 | isAAC: boolean |
154 | ||
2650d6d4 C |
155 | hlsPlaylist: { |
156 | videoFilename: string | |
157 | } | |
158 | } | |
159 | ||
536598cf C |
160 | interface QuickTranscodeOptions extends BaseTranscodeOptions { |
161 | type: 'quick-transcode' | |
162 | } | |
163 | ||
164 | interface VideoTranscodeOptions extends BaseTranscodeOptions { | |
165 | type: 'video' | |
166 | } | |
167 | ||
168 | interface MergeAudioTranscodeOptions extends BaseTranscodeOptions { | |
169 | type: 'merge-audio' | |
170 | audioPath: string | |
171 | } | |
172 | ||
3a149e9f C |
173 | interface OnlyAudioTranscodeOptions extends BaseTranscodeOptions { |
174 | type: 'only-audio' | |
5c7d6508 | 175 | } |
176 | ||
a1587156 C |
177 | type TranscodeOptions = |
178 | HLSTranscodeOptions | |
2650d6d4 | 179 | | HLSFromTSTranscodeOptions |
3a149e9f C |
180 | | VideoTranscodeOptions |
181 | | MergeAudioTranscodeOptions | |
182 | | OnlyAudioTranscodeOptions | |
183 | | QuickTranscodeOptions | |
536598cf | 184 | |
daf6e480 C |
185 | const builders: { |
186 | [ type in TranscodeOptionsType ]: (c: ffmpeg.FfmpegCommand, o?: TranscodeOptions) => Promise<ffmpeg.FfmpegCommand> | ffmpeg.FfmpegCommand | |
187 | } = { | |
188 | 'quick-transcode': buildQuickTranscodeCommand, | |
189 | 'hls': buildHLSVODCommand, | |
2650d6d4 | 190 | 'hls-from-ts': buildHLSVODFromTSCommand, |
daf6e480 C |
191 | 'merge-audio': buildAudioMergeCommand, |
192 | 'only-audio': buildOnlyAudioCommand, | |
9252a33d | 193 | 'video': buildx264VODCommand |
daf6e480 C |
194 | } |
195 | ||
196 | async function transcode (options: TranscodeOptions) { | |
9e2b2e76 C |
197 | logger.debug('Will run transcode.', { options }) |
198 | ||
55223d65 | 199 | let command = getFFmpeg(options.inputPath, 'vod') |
daf6e480 | 200 | .output(options.outputPath) |
14d3270f | 201 | |
daf6e480 | 202 | command = await builders[options.type](command, options) |
7ed2c1a4 | 203 | |
3b01f4c0 | 204 | await runCommand(command, options.job) |
5ba49f26 | 205 | |
daf6e480 | 206 | await fixHLSPlaylistIfNeeded(options) |
837666fe RK |
207 | } |
208 | ||
9252a33d C |
209 | // --------------------------------------------------------------------------- |
210 | // Live muxing/transcoding functions | |
211 | // --------------------------------------------------------------------------- | |
123f6193 | 212 | |
5a547f69 C |
213 | async function getLiveTranscodingCommand (options: { |
214 | rtmpUrl: string | |
215 | outPath: string | |
216 | resolutions: number[] | |
217 | fps: number | |
5a547f69 C |
218 | |
219 | availableEncoders: AvailableEncoders | |
220 | profile: string | |
221 | }) { | |
937581b8 | 222 | const { rtmpUrl, outPath, resolutions, fps, availableEncoders, profile } = options |
5a547f69 C |
223 | const input = rtmpUrl |
224 | ||
55223d65 | 225 | const command = getFFmpeg(input, 'live') |
c6c0fa6c C |
226 | |
227 | const varStreamMap: string[] = [] | |
228 | ||
229 | command.complexFilter([ | |
230 | { | |
231 | inputs: '[v:0]', | |
232 | filter: 'split', | |
233 | options: resolutions.length, | |
234 | outputs: resolutions.map(r => `vtemp${r}`) | |
235 | }, | |
236 | ||
237 | ...resolutions.map(r => ({ | |
238 | inputs: `vtemp${r}`, | |
239 | filter: 'scale', | |
240 | options: `w=-2:h=${r}`, | |
241 | outputs: `vout${r}` | |
242 | })) | |
243 | ]) | |
244 | ||
c6c0fa6c | 245 | command.outputOption('-preset superfast') |
49bcdb0d | 246 | command.outputOption('-sc_threshold 0') |
c6c0fa6c | 247 | |
ce4a50b9 C |
248 | addDefaultEncoderGlobalParams({ command }) |
249 | ||
c6c0fa6c C |
250 | for (let i = 0; i < resolutions.length; i++) { |
251 | const resolution = resolutions[i] | |
884d2c39 C |
252 | const resolutionFPS = computeFPS(fps, resolution) |
253 | ||
254 | const baseEncoderBuilderParams = { | |
255 | input, | |
529b3752 | 256 | |
884d2c39 C |
257 | availableEncoders, |
258 | profile, | |
529b3752 | 259 | |
884d2c39 C |
260 | fps: resolutionFPS, |
261 | resolution, | |
262 | streamNum: i, | |
263 | videoType: 'live' as 'live' | |
264 | } | |
5a547f69 C |
265 | |
266 | { | |
529b3752 C |
267 | const streamType: StreamType = 'video' |
268 | const builderResult = await getEncoderBuilderResult(Object.assign({}, baseEncoderBuilderParams, { streamType })) | |
5a547f69 C |
269 | if (!builderResult) { |
270 | throw new Error('No available live video encoder found') | |
271 | } | |
272 | ||
273 | command.outputOption(`-map [vout${resolution}]`) | |
274 | ||
884d2c39 | 275 | addDefaultEncoderParams({ command, encoder: builderResult.encoder, fps: resolutionFPS, streamNum: i }) |
5a547f69 | 276 | |
1896bca0 | 277 | logger.debug('Apply ffmpeg live video params from %s using %s profile.', builderResult.encoder, profile, builderResult) |
c6c0fa6c | 278 | |
5a547f69 C |
279 | command.outputOption(`${buildStreamSuffix('-c:v', i)} ${builderResult.encoder}`) |
280 | command.addOutputOptions(builderResult.result.outputOptions) | |
281 | } | |
282 | ||
283 | { | |
529b3752 C |
284 | const streamType: StreamType = 'audio' |
285 | const builderResult = await getEncoderBuilderResult(Object.assign({}, baseEncoderBuilderParams, { streamType })) | |
5a547f69 C |
286 | if (!builderResult) { |
287 | throw new Error('No available live audio encoder found') | |
288 | } | |
c6c0fa6c | 289 | |
5a547f69 C |
290 | command.outputOption('-map a:0') |
291 | ||
884d2c39 | 292 | addDefaultEncoderParams({ command, encoder: builderResult.encoder, fps: resolutionFPS, streamNum: i }) |
5a547f69 | 293 | |
1896bca0 | 294 | logger.debug('Apply ffmpeg live audio params from %s using %s profile.', builderResult.encoder, profile, builderResult) |
5a547f69 C |
295 | |
296 | command.outputOption(`${buildStreamSuffix('-c:a', i)} ${builderResult.encoder}`) | |
297 | command.addOutputOptions(builderResult.result.outputOptions) | |
298 | } | |
c6c0fa6c C |
299 | |
300 | varStreamMap.push(`v:${i},a:${i}`) | |
301 | } | |
302 | ||
937581b8 | 303 | addDefaultLiveHLSParams(command, outPath) |
c6c0fa6c C |
304 | |
305 | command.outputOption('-var_stream_map', varStreamMap.join(' ')) | |
306 | ||
c6c0fa6c C |
307 | return command |
308 | } | |
309 | ||
937581b8 | 310 | function getLiveMuxingCommand (rtmpUrl: string, outPath: string) { |
55223d65 | 311 | const command = getFFmpeg(rtmpUrl, 'live') |
c6c0fa6c C |
312 | |
313 | command.outputOption('-c:v copy') | |
314 | command.outputOption('-c:a copy') | |
315 | command.outputOption('-map 0:a?') | |
316 | command.outputOption('-map 0:v?') | |
317 | ||
937581b8 | 318 | addDefaultLiveHLSParams(command, outPath) |
c6c0fa6c | 319 | |
c6c0fa6c C |
320 | return command |
321 | } | |
322 | ||
5a547f69 C |
323 | function buildStreamSuffix (base: string, streamNum?: number) { |
324 | if (streamNum !== undefined) { | |
325 | return `${base}:${streamNum}` | |
326 | } | |
327 | ||
328 | return base | |
329 | } | |
330 | ||
9252a33d C |
331 | // --------------------------------------------------------------------------- |
332 | // Default options | |
333 | // --------------------------------------------------------------------------- | |
334 | ||
ce4a50b9 C |
335 | function addDefaultEncoderGlobalParams (options: { |
336 | command: ffmpeg.FfmpegCommand | |
337 | }) { | |
338 | const { command } = options | |
339 | ||
340 | // avoid issues when transcoding some files: https://trac.ffmpeg.org/ticket/6375 | |
341 | command.outputOption('-max_muxing_queue_size 1024') | |
342 | // strip all metadata | |
343 | .outputOption('-map_metadata -1') | |
344 | // NOTE: b-strategy 1 - heuristic algorithm, 16 is optimal B-frames for it | |
345 | .outputOption('-b_strategy 1') | |
346 | // NOTE: Why 16: https://github.com/Chocobozzz/PeerTube/pull/774. b-strategy 2 -> B-frames<16 | |
347 | .outputOption('-bf 16') | |
348 | // allows import of source material with incompatible pixel formats (e.g. MJPEG video) | |
349 | .outputOption('-pix_fmt yuv420p') | |
350 | } | |
351 | ||
5a547f69 C |
352 | function addDefaultEncoderParams (options: { |
353 | command: ffmpeg.FfmpegCommand | |
354 | encoder: 'libx264' | string | |
355 | streamNum?: number | |
356 | fps?: number | |
357 | }) { | |
358 | const { command, encoder, fps, streamNum } = options | |
359 | ||
360 | if (encoder === 'libx264') { | |
361 | // 3.1 is the minimal resource allocation for our highest supported resolution | |
ce4a50b9 | 362 | command.outputOption(buildStreamSuffix('-level:v', streamNum) + ' 3.1') |
5a547f69 C |
363 | |
364 | if (fps) { | |
365 | // Keyframe interval of 2 seconds for faster seeking and resolution switching. | |
366 | // https://streaminglearningcenter.com/blogs/whats-the-right-keyframe-interval.html | |
367 | // https://superuser.com/a/908325 | |
ce4a50b9 | 368 | command.outputOption(buildStreamSuffix('-g:v', streamNum) + ' ' + (fps * 2)) |
5a547f69 C |
369 | } |
370 | } | |
c6c0fa6c C |
371 | } |
372 | ||
937581b8 | 373 | function addDefaultLiveHLSParams (command: ffmpeg.FfmpegCommand, outPath: string) { |
68e70a74 | 374 | command.outputOption('-hls_time ' + VIDEO_LIVE.SEGMENT_TIME_SECONDS) |
fb719404 | 375 | command.outputOption('-hls_list_size ' + VIDEO_LIVE.SEGMENTS_LIST_SIZE) |
49bcdb0d | 376 | command.outputOption('-hls_flags delete_segments+independent_segments') |
21226063 | 377 | command.outputOption(`-hls_segment_filename ${join(outPath, '%v-%06d.ts')}`) |
c6c0fa6c C |
378 | command.outputOption('-master_pl_name master.m3u8') |
379 | command.outputOption(`-f hls`) | |
380 | ||
381 | command.output(join(outPath, '%v.m3u8')) | |
382 | } | |
383 | ||
9252a33d C |
384 | // --------------------------------------------------------------------------- |
385 | // Transcode VOD command builders | |
386 | // --------------------------------------------------------------------------- | |
387 | ||
388 | async function buildx264VODCommand (command: ffmpeg.FfmpegCommand, options: TranscodeOptions) { | |
14aed608 | 389 | let fps = await getVideoFileFPS(options.inputPath) |
884d2c39 | 390 | fps = computeFPS(fps, options.resolution) |
14aed608 | 391 | |
9252a33d | 392 | command = await presetVideo(command, options.inputPath, options, fps) |
14aed608 C |
393 | |
394 | if (options.resolution !== undefined) { | |
395 | // '?x720' or '720x?' for example | |
5a547f69 C |
396 | const size = options.isPortraitMode === true |
397 | ? `${options.resolution}x?` | |
398 | : `?x${options.resolution}` | |
399 | ||
14aed608 C |
400 | command = command.size(size) |
401 | } | |
402 | ||
14aed608 C |
403 | return command |
404 | } | |
405 | ||
536598cf C |
406 | async function buildAudioMergeCommand (command: ffmpeg.FfmpegCommand, options: MergeAudioTranscodeOptions) { |
407 | command = command.loop(undefined) | |
408 | ||
9252a33d C |
409 | command = await presetVideo(command, options.audioPath, options) |
410 | ||
9252a33d | 411 | command.outputOption('-preset:v veryfast') |
536598cf C |
412 | |
413 | command = command.input(options.audioPath) | |
414 | .videoFilter('scale=trunc(iw/2)*2:trunc(ih/2)*2') // Avoid "height not divisible by 2" error | |
415 | .outputOption('-tune stillimage') | |
416 | .outputOption('-shortest') | |
417 | ||
418 | return command | |
419 | } | |
420 | ||
daf6e480 | 421 | function buildOnlyAudioCommand (command: ffmpeg.FfmpegCommand, _options: OnlyAudioTranscodeOptions) { |
a1587156 | 422 | command = presetOnlyAudio(command) |
5c7d6508 | 423 | |
424 | return command | |
425 | } | |
426 | ||
a1587156 C |
427 | function buildQuickTranscodeCommand (command: ffmpeg.FfmpegCommand) { |
428 | command = presetCopy(command) | |
536598cf C |
429 | |
430 | command = command.outputOption('-map_metadata -1') // strip all metadata | |
431 | .outputOption('-movflags faststart') | |
432 | ||
433 | return command | |
434 | } | |
435 | ||
2650d6d4 C |
436 | function addCommonHLSVODCommandOptions (command: ffmpeg.FfmpegCommand, outputPath: string) { |
437 | return command.outputOption('-hls_time 4') | |
438 | .outputOption('-hls_list_size 0') | |
439 | .outputOption('-hls_playlist_type vod') | |
440 | .outputOption('-hls_segment_filename ' + outputPath) | |
441 | .outputOption('-hls_segment_type fmp4') | |
442 | .outputOption('-f hls') | |
443 | .outputOption('-hls_flags single_file') | |
444 | } | |
445 | ||
c6c0fa6c | 446 | async function buildHLSVODCommand (command: ffmpeg.FfmpegCommand, options: HLSTranscodeOptions) { |
14aed608 C |
447 | const videoPath = getHLSVideoPath(options) |
448 | ||
a1587156 | 449 | if (options.copyCodecs) command = presetCopy(command) |
1c320673 | 450 | else if (options.resolution === VideoResolution.H_NOVIDEO) command = presetOnlyAudio(command) |
9252a33d | 451 | else command = await buildx264VODCommand(command, options) |
14aed608 | 452 | |
2650d6d4 C |
453 | addCommonHLSVODCommandOptions(command, videoPath) |
454 | ||
455 | return command | |
456 | } | |
457 | ||
458 | async function buildHLSVODFromTSCommand (command: ffmpeg.FfmpegCommand, options: HLSFromTSTranscodeOptions) { | |
459 | const videoPath = getHLSVideoPath(options) | |
460 | ||
3851e732 | 461 | command.outputOption('-c copy') |
e772bdf1 C |
462 | |
463 | if (options.isAAC) { | |
464 | // Required for example when copying an AAC stream from an MPEG-TS | |
465 | // Since it's a bitstream filter, we don't need to reencode the audio | |
466 | command.outputOption('-bsf:a aac_adtstoasc') | |
467 | } | |
2650d6d4 C |
468 | |
469 | addCommonHLSVODCommandOptions(command, videoPath) | |
14aed608 C |
470 | |
471 | return command | |
472 | } | |
473 | ||
536598cf | 474 | async function fixHLSPlaylistIfNeeded (options: TranscodeOptions) { |
2650d6d4 | 475 | if (options.type !== 'hls' && options.type !== 'hls-from-ts') return |
7f8f8bdb | 476 | |
7f8f8bdb C |
477 | const fileContent = await readFile(options.outputPath) |
478 | ||
479 | const videoFileName = options.hlsPlaylist.videoFilename | |
480 | const videoFilePath = getHLSVideoPath(options) | |
481 | ||
536598cf | 482 | // Fix wrong mapping with some ffmpeg versions |
7f8f8bdb C |
483 | const newContent = fileContent.toString() |
484 | .replace(`#EXT-X-MAP:URI="${videoFilePath}",`, `#EXT-X-MAP:URI="${videoFileName}",`) | |
485 | ||
486 | await writeFile(options.outputPath, newContent) | |
487 | } | |
488 | ||
2650d6d4 | 489 | function getHLSVideoPath (options: HLSTranscodeOptions | HLSFromTSTranscodeOptions) { |
9252a33d | 490 | return `${dirname(options.outputPath)}/${options.hlsPlaylist.videoFilename}` |
4176e227 RK |
491 | } |
492 | ||
9252a33d C |
493 | // --------------------------------------------------------------------------- |
494 | // Transcoding presets | |
495 | // --------------------------------------------------------------------------- | |
496 | ||
529b3752 C |
497 | // Run encoder builder depending on available encoders |
498 | // Try encoders by priority: if the encoder is available, run the chosen profile or fallback to the default one | |
499 | // If the default one does not exist, check the next encoder | |
5a547f69 | 500 | async function getEncoderBuilderResult (options: { |
529b3752 | 501 | streamType: 'video' | 'audio' |
5a547f69 C |
502 | input: string |
503 | ||
504 | availableEncoders: AvailableEncoders | |
505 | profile: string | |
506 | ||
507 | videoType: 'vod' | 'live' | |
508 | ||
509 | resolution: number | |
510 | fps?: number | |
511 | streamNum?: number | |
512 | }) { | |
513 | const { availableEncoders, input, profile, resolution, streamType, fps, streamNum, videoType } = options | |
514 | ||
1896bca0 C |
515 | const encodersToTry = availableEncoders.encodersToTry[videoType][streamType] |
516 | const encoders = availableEncoders.available[videoType] | |
5a547f69 C |
517 | |
518 | for (const encoder of encodersToTry) { | |
1896bca0 C |
519 | if (!(await checkFFmpegEncoders(availableEncoders)).get(encoder)) { |
520 | logger.debug('Encoder %s not available in ffmpeg, skipping.', encoder) | |
521 | continue | |
522 | } | |
523 | ||
524 | if (!encoders[encoder]) { | |
525 | logger.debug('Encoder %s not available in peertube encoders, skipping.', encoder) | |
526 | continue | |
527 | } | |
5a547f69 | 528 | |
529b3752 C |
529 | // An object containing available profiles for this encoder |
530 | const builderProfiles: EncoderProfile<EncoderOptionsBuilder> = encoders[encoder] | |
5a547f69 C |
531 | let builder = builderProfiles[profile] |
532 | ||
533 | if (!builder) { | |
534 | logger.debug('Profile %s for encoder %s not available. Fallback to default.', profile, encoder) | |
535 | builder = builderProfiles.default | |
529b3752 C |
536 | |
537 | if (!builder) { | |
538 | logger.debug('Default profile for encoder %s not available. Try next available encoder.', encoder) | |
539 | continue | |
540 | } | |
5a547f69 C |
541 | } |
542 | ||
543 | const result = await builder({ input, resolution: resolution, fps, streamNum }) | |
544 | ||
545 | return { | |
546 | result, | |
547 | ||
548 | // If we don't have output options, then copy the input stream | |
549 | encoder: result.copy === true | |
550 | ? 'copy' | |
551 | : encoder | |
552 | } | |
553 | } | |
554 | ||
555 | return null | |
556 | } | |
557 | ||
9252a33d C |
558 | async function presetVideo ( |
559 | command: ffmpeg.FfmpegCommand, | |
560 | input: string, | |
561 | transcodeOptions: TranscodeOptions, | |
562 | fps?: number | |
563 | ) { | |
cdf4cb9e | 564 | let localCommand = command |
4176e227 | 565 | .format('mp4') |
4176e227 | 566 | .outputOption('-movflags faststart') |
4176e227 | 567 | |
ce4a50b9 C |
568 | addDefaultEncoderGlobalParams({ command }) |
569 | ||
9252a33d | 570 | // Audio encoder |
daf6e480 | 571 | const parsedAudio = await getAudioStream(input) |
4176e227 | 572 | |
529b3752 | 573 | let streamsToProcess: StreamType[] = [ 'audio', 'video' ] |
9252a33d | 574 | |
cdf4cb9e C |
575 | if (!parsedAudio.audioStream) { |
576 | localCommand = localCommand.noAudio() | |
1896bca0 | 577 | streamsToProcess = [ 'video' ] |
9252a33d | 578 | } |
536598cf | 579 | |
5a547f69 C |
580 | for (const streamType of streamsToProcess) { |
581 | const { profile, resolution, availableEncoders } = transcodeOptions | |
582 | ||
583 | const builderResult = await getEncoderBuilderResult({ | |
584 | streamType, | |
585 | input, | |
586 | resolution, | |
587 | availableEncoders, | |
588 | profile, | |
589 | fps, | |
590 | videoType: 'vod' as 'vod' | |
591 | }) | |
9252a33d | 592 | |
5a547f69 C |
593 | if (!builderResult) { |
594 | throw new Error('No available encoder found for stream ' + streamType) | |
595 | } | |
9252a33d | 596 | |
1896bca0 C |
597 | logger.debug( |
598 | 'Apply ffmpeg params from %s for %s stream of input %s using %s profile.', | |
599 | builderResult.encoder, streamType, input, profile, builderResult | |
600 | ) | |
9252a33d | 601 | |
529b3752 | 602 | if (streamType === 'video') { |
5a547f69 | 603 | localCommand.videoCodec(builderResult.encoder) |
529b3752 | 604 | } else if (streamType === 'audio') { |
5a547f69 | 605 | localCommand.audioCodec(builderResult.encoder) |
9252a33d C |
606 | } |
607 | ||
5a547f69 C |
608 | command.addOutputOptions(builderResult.result.outputOptions) |
609 | addDefaultEncoderParams({ command: localCommand, encoder: builderResult.encoder, fps }) | |
536598cf | 610 | } |
bcf21a37 | 611 | |
cdf4cb9e | 612 | return localCommand |
4176e227 | 613 | } |
14aed608 | 614 | |
a1587156 | 615 | function presetCopy (command: ffmpeg.FfmpegCommand): ffmpeg.FfmpegCommand { |
14aed608 C |
616 | return command |
617 | .format('mp4') | |
618 | .videoCodec('copy') | |
619 | .audioCodec('copy') | |
620 | } | |
5c7d6508 | 621 | |
a1587156 | 622 | function presetOnlyAudio (command: ffmpeg.FfmpegCommand): ffmpeg.FfmpegCommand { |
5c7d6508 | 623 | return command |
624 | .format('mp4') | |
625 | .audioCodec('copy') | |
626 | .noVideo() | |
627 | } | |
c6c0fa6c | 628 | |
9252a33d C |
629 | // --------------------------------------------------------------------------- |
630 | // Utils | |
631 | // --------------------------------------------------------------------------- | |
632 | ||
55223d65 | 633 | function getFFmpeg (input: string, type: 'live' | 'vod') { |
c6c0fa6c | 634 | // We set cwd explicitly because ffmpeg appears to create temporary files when trancoding which fails in read-only file systems |
7abb6060 RK |
635 | const command = ffmpeg(input, { |
636 | niceness: type === 'live' ? FFMPEG_NICE.LIVE : FFMPEG_NICE.VOD, | |
637 | cwd: CONFIG.STORAGE.TMP_DIR | |
638 | }) | |
c6c0fa6c | 639 | |
55223d65 C |
640 | const threads = type === 'live' |
641 | ? CONFIG.LIVE.TRANSCODING.THREADS | |
642 | : CONFIG.TRANSCODING.THREADS | |
643 | ||
644 | if (threads > 0) { | |
c6c0fa6c | 645 | // If we don't set any threads ffmpeg will chose automatically |
55223d65 | 646 | command.outputOption('-threads ' + threads) |
c6c0fa6c C |
647 | } |
648 | ||
649 | return command | |
650 | } | |
9252a33d | 651 | |
3b01f4c0 | 652 | async function runCommand (command: ffmpeg.FfmpegCommand, job?: Job) { |
9252a33d C |
653 | return new Promise<void>((res, rej) => { |
654 | command.on('error', (err, stdout, stderr) => { | |
9252a33d C |
655 | logger.error('Error in transcoding job.', { stdout, stderr }) |
656 | rej(err) | |
657 | }) | |
658 | ||
dd9c7929 C |
659 | command.on('end', (stdout, stderr) => { |
660 | logger.debug('FFmpeg command ended.', { stdout, stderr }) | |
661 | ||
9252a33d C |
662 | res() |
663 | }) | |
664 | ||
3b01f4c0 C |
665 | if (job) { |
666 | command.on('progress', progress => { | |
667 | if (!progress.percent) return | |
668 | ||
669 | job.progress(Math.round(progress.percent)) | |
670 | .catch(err => logger.warn('Cannot set ffmpeg job progress.', { err })) | |
671 | }) | |
672 | } | |
673 | ||
9252a33d C |
674 | command.run() |
675 | }) | |
676 | } | |
09849603 RK |
677 | |
678 | // --------------------------------------------------------------------------- | |
679 | ||
680 | export { | |
681 | getLiveTranscodingCommand, | |
682 | getLiveMuxingCommand, | |
683 | buildStreamSuffix, | |
684 | convertWebPToJPG, | |
685 | processGIF, | |
686 | generateImageFromVideoFile, | |
687 | TranscodeOptions, | |
688 | TranscodeOptionsType, | |
689 | transcode, | |
690 | runCommand, | |
691 | ||
1896bca0 C |
692 | resetSupportedEncoders, |
693 | ||
09849603 RK |
694 | // builders |
695 | buildx264VODCommand | |
696 | } |