aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/ffmpeg/ffmpeg-live.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-04-21 14:55:10 +0200
committerChocobozzz <chocobozzz@cpy.re>2023-05-09 08:57:34 +0200
commit0c9668f77901e7540e2c7045eb0f2974a4842a69 (patch)
tree226d3dd1565b0bb56588897af3b8530e6216e96b /shared/ffmpeg/ffmpeg-live.ts
parent6bcb854cdea8688a32240bc5719c7d139806e00b (diff)
downloadPeerTube-0c9668f77901e7540e2c7045eb0f2974a4842a69.tar.gz
PeerTube-0c9668f77901e7540e2c7045eb0f2974a4842a69.tar.zst
PeerTube-0c9668f77901e7540e2c7045eb0f2974a4842a69.zip
Implement remote runner jobs in server
Move ffmpeg functions to @shared
Diffstat (limited to 'shared/ffmpeg/ffmpeg-live.ts')
-rw-r--r--shared/ffmpeg/ffmpeg-live.ts184
1 files changed, 184 insertions, 0 deletions
diff --git a/shared/ffmpeg/ffmpeg-live.ts b/shared/ffmpeg/ffmpeg-live.ts
new file mode 100644
index 000000000..cca4c6474
--- /dev/null
+++ b/shared/ffmpeg/ffmpeg-live.ts
@@ -0,0 +1,184 @@
1import { FilterSpecification } from 'fluent-ffmpeg'
2import { join } from 'path'
3import { pick } from '@shared/core-utils'
4import { FFmpegCommandWrapper, FFmpegCommandWrapperOptions } from './ffmpeg-command-wrapper'
5import { buildStreamSuffix, getScaleFilter, StreamType } from './ffmpeg-utils'
6import { addDefaultEncoderGlobalParams, addDefaultEncoderParams, applyEncoderOptions } from './shared'
7
8export class FFmpegLive {
9 private readonly commandWrapper: FFmpegCommandWrapper
10
11 constructor (options: FFmpegCommandWrapperOptions) {
12 this.commandWrapper = new FFmpegCommandWrapper(options)
13 }
14
15 async getLiveTranscodingCommand (options: {
16 inputUrl: string
17
18 outPath: string
19 masterPlaylistName: string
20
21 toTranscode: {
22 resolution: number
23 fps: number
24 }[]
25
26 // Input information
27 bitrate: number
28 ratio: number
29 hasAudio: boolean
30
31 segmentListSize: number
32 segmentDuration: number
33 }) {
34 const {
35 inputUrl,
36 outPath,
37 toTranscode,
38 bitrate,
39 masterPlaylistName,
40 ratio,
41 hasAudio
42 } = options
43 const command = this.commandWrapper.buildCommand(inputUrl)
44
45 const varStreamMap: string[] = []
46
47 const complexFilter: FilterSpecification[] = [
48 {
49 inputs: '[v:0]',
50 filter: 'split',
51 options: toTranscode.length,
52 outputs: toTranscode.map(t => `vtemp${t.resolution}`)
53 }
54 ]
55
56 command.outputOption('-sc_threshold 0')
57
58 addDefaultEncoderGlobalParams(command)
59
60 for (let i = 0; i < toTranscode.length; i++) {
61 const streamMap: string[] = []
62 const { resolution, fps } = toTranscode[i]
63
64 const baseEncoderBuilderParams = {
65 input: inputUrl,
66
67 canCopyAudio: true,
68 canCopyVideo: true,
69
70 inputBitrate: bitrate,
71 inputRatio: ratio,
72
73 resolution,
74 fps,
75
76 streamNum: i,
77 videoType: 'live' as 'live'
78 }
79
80 {
81 const streamType: StreamType = 'video'
82 const builderResult = await this.commandWrapper.getEncoderBuilderResult({ ...baseEncoderBuilderParams, streamType })
83 if (!builderResult) {
84 throw new Error('No available live video encoder found')
85 }
86
87 command.outputOption(`-map [vout${resolution}]`)
88
89 addDefaultEncoderParams({ command, encoder: builderResult.encoder, fps, streamNum: i })
90
91 this.commandWrapper.debugLog(
92 `Apply ffmpeg live video params from ${builderResult.encoder} using ${this.commandWrapper.getProfile()} profile.`,
93 { builderResult, fps, toTranscode }
94 )
95
96 command.outputOption(`${buildStreamSuffix('-c:v', i)} ${builderResult.encoder}`)
97 applyEncoderOptions(command, builderResult.result)
98
99 complexFilter.push({
100 inputs: `vtemp${resolution}`,
101 filter: getScaleFilter(builderResult.result),
102 options: `w=-2:h=${resolution}`,
103 outputs: `vout${resolution}`
104 })
105
106 streamMap.push(`v:${i}`)
107 }
108
109 if (hasAudio) {
110 const streamType: StreamType = 'audio'
111 const builderResult = await this.commandWrapper.getEncoderBuilderResult({ ...baseEncoderBuilderParams, streamType })
112 if (!builderResult) {
113 throw new Error('No available live audio encoder found')
114 }
115
116 command.outputOption('-map a:0')
117
118 addDefaultEncoderParams({ command, encoder: builderResult.encoder, fps, streamNum: i })
119
120 this.commandWrapper.debugLog(
121 `Apply ffmpeg live audio params from ${builderResult.encoder} using ${this.commandWrapper.getProfile()} profile.`,
122 { builderResult, fps, resolution }
123 )
124
125 command.outputOption(`${buildStreamSuffix('-c:a', i)} ${builderResult.encoder}`)
126 applyEncoderOptions(command, builderResult.result)
127
128 streamMap.push(`a:${i}`)
129 }
130
131 varStreamMap.push(streamMap.join(','))
132 }
133
134 command.complexFilter(complexFilter)
135
136 this.addDefaultLiveHLSParams({ ...pick(options, [ 'segmentDuration', 'segmentListSize' ]), outPath, masterPlaylistName })
137
138 command.outputOption('-var_stream_map', varStreamMap.join(' '))
139
140 return command
141 }
142
143 getLiveMuxingCommand (options: {
144 inputUrl: string
145 outPath: string
146 masterPlaylistName: string
147
148 segmentListSize: number
149 segmentDuration: number
150 }) {
151 const { inputUrl, outPath, masterPlaylistName } = options
152
153 const command = this.commandWrapper.buildCommand(inputUrl)
154
155 command.outputOption('-c:v copy')
156 command.outputOption('-c:a copy')
157 command.outputOption('-map 0:a?')
158 command.outputOption('-map 0:v?')
159
160 this.addDefaultLiveHLSParams({ ...pick(options, [ 'segmentDuration', 'segmentListSize' ]), outPath, masterPlaylistName })
161
162 return command
163 }
164
165 private addDefaultLiveHLSParams (options: {
166 outPath: string
167 masterPlaylistName: string
168 segmentListSize: number
169 segmentDuration: number
170 }) {
171 const { outPath, masterPlaylistName, segmentListSize, segmentDuration } = options
172
173 const command = this.commandWrapper.getCommand()
174
175 command.outputOption('-hls_time ' + segmentDuration)
176 command.outputOption('-hls_list_size ' + segmentListSize)
177 command.outputOption('-hls_flags delete_segments+independent_segments+program_date_time')
178 command.outputOption(`-hls_segment_filename ${join(outPath, '%v-%06d.ts')}`)
179 command.outputOption('-master_pl_name ' + masterPlaylistName)
180 command.outputOption(`-f hls`)
181
182 command.output(join(outPath, '%v.m3u8'))
183 }
184}