]>
Commit | Line | Data |
---|---|---|
c729caf6 C |
1 | import { FilterSpecification } from 'fluent-ffmpeg' |
2 | import { VIDEO_FILTERS } from '@server/initializers/constants' | |
3 | import { AvailableEncoders } from '@shared/models' | |
4 | import { logger, loggerTagsFactory } from '../logger' | |
5 | import { getFFmpeg, runCommand } from './ffmpeg-commons' | |
6 | import { presetCopy, presetVOD } from './ffmpeg-presets' | |
7 | import { ffprobePromise, getVideoStreamDimensionsInfo, getVideoStreamDuration, getVideoStreamFPS, hasAudioStream } from './ffprobe-utils' | |
8 | ||
9 | const lTags = loggerTagsFactory('ffmpeg') | |
10 | ||
11 | async function cutVideo (options: { | |
12 | inputPath: string | |
13 | outputPath: string | |
14 | start?: number | |
15 | end?: number | |
16 | }) { | |
17 | const { inputPath, outputPath } = options | |
18 | ||
19 | logger.debug('Will cut the video.', { options, ...lTags() }) | |
20 | ||
21 | let command = getFFmpeg(inputPath, 'vod') | |
22 | .output(outputPath) | |
23 | ||
24 | command = presetCopy(command) | |
25 | ||
4ea82707 C |
26 | if (options.start) { |
27 | // Using -ss as output option is more precise than using it as input option | |
28 | command.outputOption('-ss ' + options.start) | |
29 | } | |
c729caf6 C |
30 | |
31 | if (options.end) { | |
32 | const endSeeking = options.end - (options.start || 0) | |
33 | ||
34 | command.outputOption('-to ' + endSeeking) | |
35 | } | |
36 | ||
37 | await runCommand({ command }) | |
38 | } | |
39 | ||
40 | async function addWatermark (options: { | |
41 | inputPath: string | |
42 | watermarkPath: string | |
43 | outputPath: string | |
44 | ||
45 | availableEncoders: AvailableEncoders | |
46 | profile: string | |
47 | }) { | |
48 | const { watermarkPath, inputPath, outputPath, availableEncoders, profile } = options | |
49 | ||
50 | logger.debug('Will add watermark to the video.', { options, ...lTags() }) | |
51 | ||
52 | const videoProbe = await ffprobePromise(inputPath) | |
53 | const fps = await getVideoStreamFPS(inputPath, videoProbe) | |
54 | const { resolution } = await getVideoStreamDimensionsInfo(inputPath, videoProbe) | |
55 | ||
56 | let command = getFFmpeg(inputPath, 'vod') | |
57 | .output(outputPath) | |
58 | command.input(watermarkPath) | |
59 | ||
60 | command = await presetVOD({ | |
61 | command, | |
62 | input: inputPath, | |
63 | availableEncoders, | |
64 | profile, | |
65 | resolution, | |
66 | fps, | |
67 | canCopyAudio: true, | |
68 | canCopyVideo: false | |
69 | }) | |
70 | ||
71 | const complexFilter: FilterSpecification[] = [ | |
72 | // Scale watermark | |
73 | { | |
74 | inputs: [ '[1]', '[0]' ], | |
75 | filter: 'scale2ref', | |
76 | options: { | |
77 | w: 'oh*mdar', | |
78 | h: `ih*${VIDEO_FILTERS.WATERMARK.SIZE_RATIO}` | |
79 | }, | |
80 | outputs: [ '[watermark]', '[video]' ] | |
81 | }, | |
82 | ||
83 | { | |
84 | inputs: [ '[video]', '[watermark]' ], | |
85 | filter: 'overlay', | |
86 | options: { | |
87 | x: `main_w - overlay_w - (main_h * ${VIDEO_FILTERS.WATERMARK.HORIZONTAL_MARGIN_RATIO})`, | |
88 | y: `main_h * ${VIDEO_FILTERS.WATERMARK.VERTICAL_MARGIN_RATIO}` | |
89 | } | |
90 | } | |
91 | ] | |
92 | ||
93 | command.complexFilter(complexFilter) | |
94 | ||
95 | await runCommand({ command }) | |
96 | } | |
97 | ||
98 | async function addIntroOutro (options: { | |
99 | inputPath: string | |
100 | introOutroPath: string | |
101 | outputPath: string | |
102 | type: 'intro' | 'outro' | |
103 | ||
104 | availableEncoders: AvailableEncoders | |
105 | profile: string | |
106 | }) { | |
107 | const { introOutroPath, inputPath, outputPath, availableEncoders, profile, type } = options | |
108 | ||
109 | logger.debug('Will add intro/outro to the video.', { options, ...lTags() }) | |
110 | ||
111 | const mainProbe = await ffprobePromise(inputPath) | |
112 | const fps = await getVideoStreamFPS(inputPath, mainProbe) | |
113 | const { resolution } = await getVideoStreamDimensionsInfo(inputPath, mainProbe) | |
114 | const mainHasAudio = await hasAudioStream(inputPath, mainProbe) | |
115 | ||
116 | const introOutroProbe = await ffprobePromise(introOutroPath) | |
117 | const introOutroHasAudio = await hasAudioStream(introOutroPath, introOutroProbe) | |
118 | ||
119 | let command = getFFmpeg(inputPath, 'vod') | |
120 | .output(outputPath) | |
121 | ||
122 | command.input(introOutroPath) | |
123 | ||
124 | if (!introOutroHasAudio && mainHasAudio) { | |
125 | const duration = await getVideoStreamDuration(introOutroPath, introOutroProbe) | |
126 | ||
127 | command.input('anullsrc') | |
128 | command.withInputFormat('lavfi') | |
129 | command.withInputOption('-t ' + duration) | |
130 | } | |
131 | ||
132 | command = await presetVOD({ | |
133 | command, | |
134 | input: inputPath, | |
135 | availableEncoders, | |
136 | profile, | |
137 | resolution, | |
138 | fps, | |
139 | canCopyAudio: false, | |
140 | canCopyVideo: false | |
141 | }) | |
142 | ||
143 | // Add black background to correctly scale intro/outro with padding | |
144 | const complexFilter: FilterSpecification[] = [ | |
145 | { | |
146 | inputs: [ '1', '0' ], | |
147 | filter: 'scale2ref', | |
148 | options: { | |
149 | w: 'iw', | |
150 | h: `ih` | |
151 | }, | |
152 | outputs: [ 'intro-outro', 'main' ] | |
153 | }, | |
154 | { | |
155 | inputs: [ 'intro-outro', 'main' ], | |
156 | filter: 'scale2ref', | |
157 | options: { | |
158 | w: 'iw', | |
159 | h: `ih` | |
160 | }, | |
161 | outputs: [ 'to-scale', 'main' ] | |
162 | }, | |
163 | { | |
164 | inputs: 'to-scale', | |
165 | filter: 'drawbox', | |
166 | options: { | |
167 | t: 'fill' | |
168 | }, | |
169 | outputs: [ 'to-scale-bg' ] | |
170 | }, | |
171 | { | |
172 | inputs: [ '1', 'to-scale-bg' ], | |
173 | filter: 'scale2ref', | |
174 | options: { | |
175 | w: 'iw', | |
176 | h: 'ih', | |
177 | force_original_aspect_ratio: 'decrease', | |
178 | flags: 'spline' | |
179 | }, | |
180 | outputs: [ 'to-scale', 'to-scale-bg' ] | |
181 | }, | |
182 | { | |
183 | inputs: [ 'to-scale-bg', 'to-scale' ], | |
184 | filter: 'overlay', | |
185 | options: { | |
186 | x: '(main_w - overlay_w)/2', | |
187 | y: '(main_h - overlay_h)/2' | |
188 | }, | |
189 | outputs: 'intro-outro-resized' | |
190 | } | |
191 | ] | |
192 | ||
193 | const concatFilter = { | |
194 | inputs: [], | |
195 | filter: 'concat', | |
196 | options: { | |
197 | n: 2, | |
198 | v: 1, | |
199 | unsafe: 1 | |
200 | }, | |
201 | outputs: [ 'v' ] | |
202 | } | |
203 | ||
204 | const introOutroFilterInputs = [ 'intro-outro-resized' ] | |
205 | const mainFilterInputs = [ 'main' ] | |
206 | ||
207 | if (mainHasAudio) { | |
208 | mainFilterInputs.push('0:a') | |
209 | ||
210 | if (introOutroHasAudio) { | |
211 | introOutroFilterInputs.push('1:a') | |
212 | } else { | |
213 | // Silent input | |
214 | introOutroFilterInputs.push('2:a') | |
215 | } | |
216 | } | |
217 | ||
218 | if (type === 'intro') { | |
219 | concatFilter.inputs = [ ...introOutroFilterInputs, ...mainFilterInputs ] | |
220 | } else { | |
221 | concatFilter.inputs = [ ...mainFilterInputs, ...introOutroFilterInputs ] | |
222 | } | |
223 | ||
224 | if (mainHasAudio) { | |
225 | concatFilter.options['a'] = 1 | |
226 | concatFilter.outputs.push('a') | |
227 | ||
228 | command.outputOption('-map [a]') | |
229 | } | |
230 | ||
231 | command.outputOption('-map [v]') | |
232 | ||
233 | complexFilter.push(concatFilter) | |
234 | command.complexFilter(complexFilter) | |
235 | ||
236 | await runCommand({ command }) | |
237 | } | |
238 | ||
239 | // --------------------------------------------------------------------------- | |
240 | ||
241 | export { | |
242 | cutVideo, | |
243 | addIntroOutro, | |
244 | addWatermark | |
245 | } |