]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/ffmpeg/ffmpeg-default-transcoding-profile.ts
Translated using Weblate (Persian)
[github/Chocobozzz/PeerTube.git] / shared / ffmpeg / ffmpeg-default-transcoding-profile.ts
CommitLineData
e7d8e2b2
C
1import { FfprobeData } from 'fluent-ffmpeg'
2import { getAverageTheoreticalBitrate, getMaxTheoreticalBitrate, getMinTheoreticalBitrate } from '@shared/core-utils'
3import {
4 buildStreamSuffix,
5 ffprobePromise,
6 getAudioStream,
7 getMaxAudioBitrate,
8 getVideoStream,
9 getVideoStreamBitrate,
10 getVideoStreamDimensionsInfo,
11 getVideoStreamFPS
12} from '@shared/ffmpeg'
1772b383
C
13import { EncoderOptionsBuilder, EncoderOptionsBuilderParams, VideoResolution } from '@shared/models'
14
15const defaultX264VODOptionsBuilder: EncoderOptionsBuilder = (options: EncoderOptionsBuilderParams) => {
16 const { fps, inputRatio, inputBitrate, resolution } = options
17
18 const targetBitrate = getTargetBitrate({ inputBitrate, ratio: inputRatio, fps, resolution })
19
20 return {
21 outputOptions: [
22 ...getCommonOutputOptions(targetBitrate),
23
24 `-r ${fps}`
25 ]
26 }
27}
28
29const defaultX264LiveOptionsBuilder: EncoderOptionsBuilder = (options: EncoderOptionsBuilderParams) => {
30 const { streamNum, fps, inputBitrate, inputRatio, resolution } = options
31
32 const targetBitrate = getTargetBitrate({ inputBitrate, ratio: inputRatio, fps, resolution })
33
34 return {
35 outputOptions: [
36 ...getCommonOutputOptions(targetBitrate, streamNum),
37
38 `${buildStreamSuffix('-r:v', streamNum)} ${fps}`,
39 `${buildStreamSuffix('-b:v', streamNum)} ${targetBitrate}`
40 ]
41 }
42}
43
44const defaultAACOptionsBuilder: EncoderOptionsBuilder = async ({ input, streamNum, canCopyAudio }) => {
45 const probe = await ffprobePromise(input)
46
e7d8e2b2
C
47 if (canCopyAudio && await canDoQuickAudioTranscode(input, probe)) {
48 return { copy: true, outputOptions: [ ] }
49 }
50
1772b383
C
51 const parsedAudio = await getAudioStream(input, probe)
52
53 // We try to reduce the ceiling bitrate by making rough matches of bitrates
54 // Of course this is far from perfect, but it might save some space in the end
55
56 const audioCodecName = parsedAudio.audioStream['codec_name']
57
58 const bitrate = getMaxAudioBitrate(audioCodecName, parsedAudio.bitrate)
59
60 // Force stereo as it causes some issues with HLS playback in Chrome
61 const base = [ '-channel_layout', 'stereo' ]
62
63 if (bitrate !== -1) {
64 return { outputOptions: base.concat([ buildStreamSuffix('-b:a', streamNum), bitrate + 'k' ]) }
65 }
66
67 return { outputOptions: base }
68}
69
70const defaultLibFDKAACVODOptionsBuilder: EncoderOptionsBuilder = ({ streamNum }) => {
71 return { outputOptions: [ buildStreamSuffix('-q:a', streamNum), '5' ] }
72}
73
ab14f0e0 74export function getDefaultAvailableEncoders () {
1772b383
C
75 return {
76 vod: {
77 libx264: {
78 default: defaultX264VODOptionsBuilder
79 },
80 aac: {
81 default: defaultAACOptionsBuilder
82 },
83 libfdk_aac: {
84 default: defaultLibFDKAACVODOptionsBuilder
85 }
86 },
87 live: {
88 libx264: {
89 default: defaultX264LiveOptionsBuilder
90 },
91 aac: {
92 default: defaultAACOptionsBuilder
93 }
94 }
95 }
96}
97
ab14f0e0 98export function getDefaultEncodersToTry () {
1772b383
C
99 return {
100 vod: {
101 video: [ 'libx264' ],
102 audio: [ 'libfdk_aac', 'aac' ]
103 },
104
105 live: {
106 video: [ 'libx264' ],
107 audio: [ 'libfdk_aac', 'aac' ]
108 }
109 }
110}
111
e7d8e2b2
C
112export async function canDoQuickAudioTranscode (path: string, probe?: FfprobeData): Promise<boolean> {
113 const parsedAudio = await getAudioStream(path, probe)
114
115 if (!parsedAudio.audioStream) return true
116
117 if (parsedAudio.audioStream['codec_name'] !== 'aac') return false
118
119 const audioBitrate = parsedAudio.bitrate
120 if (!audioBitrate) return false
121
122 const maxAudioBitrate = getMaxAudioBitrate('aac', audioBitrate)
123 if (maxAudioBitrate !== -1 && audioBitrate > maxAudioBitrate) return false
124
125 const channelLayout = parsedAudio.audioStream['channel_layout']
126 // Causes playback issues with Chrome
127 if (!channelLayout || channelLayout === 'unknown' || channelLayout === 'quad') return false
128
129 return true
130}
131
132export async function canDoQuickVideoTranscode (path: string, probe?: FfprobeData): Promise<boolean> {
133 const videoStream = await getVideoStream(path, probe)
134 const fps = await getVideoStreamFPS(path, probe)
135 const bitRate = await getVideoStreamBitrate(path, probe)
136 const resolutionData = await getVideoStreamDimensionsInfo(path, probe)
137
138 // If ffprobe did not manage to guess the bitrate
139 if (!bitRate) return false
140
141 // check video params
142 if (!videoStream) return false
143 if (videoStream['codec_name'] !== 'h264') return false
144 if (videoStream['pix_fmt'] !== 'yuv420p') return false
145 if (fps < 2 || fps > 65) return false
146 if (bitRate > getMaxTheoreticalBitrate({ ...resolutionData, fps })) return false
147
148 return true
149}
150
1772b383
C
151// ---------------------------------------------------------------------------
152
153function getTargetBitrate (options: {
154 inputBitrate: number
155 resolution: VideoResolution
156 ratio: number
157 fps: number
158}) {
159 const { inputBitrate, resolution, ratio, fps } = options
160
e7d8e2b2
C
161 const capped = capBitrate(inputBitrate, getAverageTheoreticalBitrate({ resolution, fps, ratio }))
162 const limit = getMinTheoreticalBitrate({ resolution, fps, ratio })
1772b383
C
163
164 return Math.max(limit, capped)
165}
166
167function capBitrate (inputBitrate: number, targetBitrate: number) {
168 if (!inputBitrate) return targetBitrate
169
170 // Add 30% margin to input bitrate
171 const inputBitrateWithMargin = inputBitrate + (inputBitrate * 0.3)
172
173 return Math.min(targetBitrate, inputBitrateWithMargin)
174}
175
176function getCommonOutputOptions (targetBitrate: number, streamNum?: number) {
177 return [
178 `-preset veryfast`,
179 `${buildStreamSuffix('-maxrate:v', streamNum)} ${targetBitrate}`,
180 `${buildStreamSuffix('-bufsize:v', streamNum)} ${targetBitrate * 2}`,
181
182 // NOTE: b-strategy 1 - heuristic algorithm, 16 is optimal B-frames for it
183 `-b_strategy 1`,
184 // NOTE: Why 16: https://github.com/Chocobozzz/PeerTube/pull/774. b-strategy 2 -> B-frames<16
185 `-bf 16`
186 ]
187}