]>
Commit | Line | Data |
---|---|---|
6b67897e | 1 | import { logger } from '@server/helpers/logger' |
5a547f69 C |
2 | import { getTargetBitrate } from '../../shared/models/videos' |
3 | import { AvailableEncoders, buildStreamSuffix, EncoderOptionsBuilder } from '../helpers/ffmpeg-utils' | |
6b67897e C |
4 | import { |
5 | canDoQuickAudioTranscode, | |
6 | ffprobePromise, | |
7 | getAudioStream, | |
8 | getMaxAudioBitrate, | |
9 | getVideoFileBitrate, | |
10 | getVideoStreamFromFile | |
11 | } from '../helpers/ffprobe-utils' | |
5a547f69 C |
12 | import { VIDEO_TRANSCODING_FPS } from '../initializers/constants' |
13 | ||
6b67897e C |
14 | /** |
15 | * | |
16 | * Available encoders and profiles for the transcoding jobs | |
17 | * These functions are used by ffmpeg-utils that will get the encoders and options depending on the chosen profile | |
18 | * | |
19 | */ | |
5a547f69 C |
20 | |
21 | // Resources: | |
22 | // * https://slhck.info/video/2017/03/01/rate-control.html | |
23 | // * https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate | |
24 | ||
25 | const defaultX264VODOptionsBuilder: EncoderOptionsBuilder = async ({ input, resolution, fps }) => { | |
26 | let targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS) | |
27 | ||
28 | const probe = await ffprobePromise(input) | |
29 | ||
30 | const videoStream = await getVideoStreamFromFile(input, probe) | |
31 | if (!videoStream) { | |
32 | return { outputOptions: [ ] } | |
33 | } | |
34 | ||
35 | // Don't transcode to an higher bitrate than the original file | |
36 | const fileBitrate = await getVideoFileBitrate(input, probe) | |
37 | targetBitrate = Math.min(targetBitrate, fileBitrate) | |
38 | ||
39 | return { | |
40 | outputOptions: [ | |
6b67897e C |
41 | `-maxrate ${targetBitrate}`, |
42 | `-bufsize ${targetBitrate * 2}` | |
5a547f69 C |
43 | ] |
44 | } | |
45 | } | |
46 | ||
47 | const defaultX264LiveOptionsBuilder: EncoderOptionsBuilder = async ({ resolution, fps, streamNum }) => { | |
48 | const targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS) | |
49 | ||
50 | return { | |
51 | outputOptions: [ | |
52 | `${buildStreamSuffix('-b:v', streamNum)} ${targetBitrate}`, | |
ca5c612b C |
53 | `-maxrate ${targetBitrate}`, |
54 | `-bufsize ${targetBitrate * 2}` | |
5a547f69 C |
55 | ] |
56 | } | |
57 | } | |
58 | ||
59 | const defaultAACOptionsBuilder: EncoderOptionsBuilder = async ({ input, streamNum }) => { | |
6b67897e C |
60 | const probe = await ffprobePromise(input) |
61 | ||
62 | if (await canDoQuickAudioTranscode(input, probe)) { | |
63 | logger.debug('Copy audio stream %s by AAC encoder.', input) | |
64 | return { copy: true, outputOptions: [] } | |
65 | } | |
66 | ||
67 | const parsedAudio = await getAudioStream(input, probe) | |
5a547f69 C |
68 | |
69 | // We try to reduce the ceiling bitrate by making rough matches of bitrates | |
70 | // Of course this is far from perfect, but it might save some space in the end | |
71 | ||
72 | const audioCodecName = parsedAudio.audioStream['codec_name'] | |
73 | ||
74 | const bitrate = getMaxAudioBitrate(audioCodecName, parsedAudio.bitrate) | |
75 | ||
6b67897e C |
76 | logger.debug('Calculating audio bitrate of %s by AAC encoder.', input, { bitrate: parsedAudio.bitrate, audioCodecName }) |
77 | ||
5a547f69 C |
78 | if (bitrate !== undefined && bitrate !== -1) { |
79 | return { outputOptions: [ buildStreamSuffix('-b:a', streamNum), bitrate + 'k' ] } | |
80 | } | |
81 | ||
6b67897e | 82 | return { outputOptions: [ ] } |
5a547f69 C |
83 | } |
84 | ||
85 | const defaultLibFDKAACVODOptionsBuilder: EncoderOptionsBuilder = ({ streamNum }) => { | |
86 | return { outputOptions: [ buildStreamSuffix('-q:a', streamNum), '5' ] } | |
87 | } | |
88 | ||
89 | const availableEncoders: AvailableEncoders = { | |
90 | vod: { | |
91 | libx264: { | |
92 | default: defaultX264VODOptionsBuilder | |
93 | }, | |
94 | aac: { | |
95 | default: defaultAACOptionsBuilder | |
96 | }, | |
97 | libfdk_aac: { | |
98 | default: defaultLibFDKAACVODOptionsBuilder | |
99 | } | |
100 | }, | |
101 | live: { | |
102 | libx264: { | |
103 | default: defaultX264LiveOptionsBuilder | |
104 | }, | |
105 | aac: { | |
106 | default: defaultAACOptionsBuilder | |
107 | } | |
108 | } | |
109 | } | |
110 | ||
111 | // --------------------------------------------------------------------------- | |
112 | ||
113 | export { | |
114 | availableEncoders | |
115 | } | |
116 | ||
117 | // --------------------------------------------------------------------------- |