aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/ffmpeg/ffmpeg-edition.ts
blob: 78de8f3b35632d7bc213a7e3aa65bfe40c4b24a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import { FilterSpecification } from 'fluent-ffmpeg'
import { VIDEO_FILTERS } from '@server/initializers/constants'
import { AvailableEncoders } from '@shared/models'
import { logger, loggerTagsFactory } from '../logger'
import { getFFmpeg, runCommand } from './ffmpeg-commons'
import { presetCopy, presetVOD } from './ffmpeg-presets'
import { ffprobePromise, getVideoStreamDimensionsInfo, getVideoStreamDuration, getVideoStreamFPS, hasAudioStream } from './ffprobe-utils'

const lTags = loggerTagsFactory('ffmpeg')

async function cutVideo (options: {
  inputPath: string
  outputPath: string
  start?: number
  end?: number
}) {
  const { inputPath, outputPath } = options

  logger.debug('Will cut the video.', { options, ...lTags() })

  let command = getFFmpeg(inputPath, 'vod')
    .output(outputPath)

  command = presetCopy(command)

  if (options.start) {
    // Using -ss as output option is more precise than using it as input option
    command.outputOption('-ss ' + options.start)
  }

  if (options.end) {
    const endSeeking = options.end - (options.start || 0)

    command.outputOption('-to ' + endSeeking)
  }

  await runCommand({ command })
}

async function addWatermark (options: {
  inputPath: string
  watermarkPath: string
  outputPath: string

  availableEncoders: AvailableEncoders
  profile: string
}) {
  const { watermarkPath, inputPath, outputPath, availableEncoders, profile } = options

  logger.debug('Will add watermark to the video.', { options, ...lTags() })

  const videoProbe = await ffprobePromise(inputPath)
  const fps = await getVideoStreamFPS(inputPath, videoProbe)
  const { resolution } = await getVideoStreamDimensionsInfo(inputPath, videoProbe)

  let command = getFFmpeg(inputPath, 'vod')
    .output(outputPath)
  command.input(watermarkPath)

  command = await presetVOD({
    command,
    input: inputPath,
    availableEncoders,
    profile,
    resolution,
    fps,
    canCopyAudio: true,
    canCopyVideo: false
  })

  const complexFilter: FilterSpecification[] = [
    // Scale watermark
    {
      inputs: [ '[1]', '[0]' ],
      filter: 'scale2ref',
      options: {
        w: 'oh*mdar',
        h: `ih*${VIDEO_FILTERS.WATERMARK.SIZE_RATIO}`
      },
      outputs: [ '[watermark]', '[video]' ]
    },

    {
      inputs: [ '[video]', '[watermark]' ],
      filter: 'overlay',
      options: {
        x: `main_w - overlay_w - (main_h * ${VIDEO_FILTERS.WATERMARK.HORIZONTAL_MARGIN_RATIO})`,
        y: `main_h * ${VIDEO_FILTERS.WATERMARK.VERTICAL_MARGIN_RATIO}`
      }
    }
  ]

  command.complexFilter(complexFilter)

  await runCommand({ command })
}

async function addIntroOutro (options: {
  inputPath: string
  introOutroPath: string
  outputPath: string
  type: 'intro' | 'outro'

  availableEncoders: AvailableEncoders
  profile: string
}) {
  const { introOutroPath, inputPath, outputPath, availableEncoders, profile, type } = options

  logger.debug('Will add intro/outro to the video.', { options, ...lTags() })

  const mainProbe = await ffprobePromise(inputPath)
  const fps = await getVideoStreamFPS(inputPath, mainProbe)
  const { resolution } = await getVideoStreamDimensionsInfo(inputPath, mainProbe)
  const mainHasAudio = await hasAudioStream(inputPath, mainProbe)

  const introOutroProbe = await ffprobePromise(introOutroPath)
  const introOutroHasAudio = await hasAudioStream(introOutroPath, introOutroProbe)

  let command = getFFmpeg(inputPath, 'vod')
    .output(outputPath)

  command.input(introOutroPath)

  if (!introOutroHasAudio && mainHasAudio) {
    const duration = await getVideoStreamDuration(introOutroPath, introOutroProbe)

    command.input('anullsrc')
    command.withInputFormat('lavfi')
    command.withInputOption('-t ' + duration)
  }

  command = await presetVOD({
    command,
    input: inputPath,
    availableEncoders,
    profile,
    resolution,
    fps,
    canCopyAudio: false,
    canCopyVideo: false
  })

  // Add black background to correctly scale intro/outro with padding
  const complexFilter: FilterSpecification[] = [
    {
      inputs: [ '1', '0' ],
      filter: 'scale2ref',
      options: {
        w: 'iw',
        h: `ih`
      },
      outputs: [ 'intro-outro', 'main' ]
    },
    {
      inputs: [ 'intro-outro', 'main' ],
      filter: 'scale2ref',
      options: {
        w: 'iw',
        h: `ih`
      },
      outputs: [ 'to-scale', 'main' ]
    },
    {
      inputs: 'to-scale',
      filter: 'drawbox',
      options: {
        t: 'fill'
      },
      outputs: [ 'to-scale-bg' ]
    },
    {
      inputs: [ '1', 'to-scale-bg' ],
      filter: 'scale2ref',
      options: {
        w: 'iw',
        h: 'ih',
        force_original_aspect_ratio: 'decrease',
        flags: 'spline'
      },
      outputs: [ 'to-scale', 'to-scale-bg' ]
    },
    {
      inputs: [ 'to-scale-bg', 'to-scale' ],
      filter: 'overlay',
      options: {
        x: '(main_w - overlay_w)/2',
        y: '(main_h - overlay_h)/2'
      },
      outputs: 'intro-outro-resized'
    }
  ]

  const concatFilter = {
    inputs: [],
    filter: 'concat',
    options: {
      n: 2,
      v: 1,
      unsafe: 1
    },
    outputs: [ 'v' ]
  }

  const introOutroFilterInputs = [ 'intro-outro-resized' ]
  const mainFilterInputs = [ 'main' ]

  if (mainHasAudio) {
    mainFilterInputs.push('0:a')

    if (introOutroHasAudio) {
      introOutroFilterInputs.push('1:a')
    } else {
      // Silent input
      introOutroFilterInputs.push('2:a')
    }
  }

  if (type === 'intro') {
    concatFilter.inputs = [ ...introOutroFilterInputs, ...mainFilterInputs ]
  } else {
    concatFilter.inputs = [ ...mainFilterInputs, ...introOutroFilterInputs ]
  }

  if (mainHasAudio) {
    concatFilter.options['a'] = 1
    concatFilter.outputs.push('a')

    command.outputOption('-map [a]')
  }

  command.outputOption('-map [v]')

  complexFilter.push(concatFilter)
  command.complexFilter(complexFilter)

  await runCommand({ command })
}

// ---------------------------------------------------------------------------

export {
  cutVideo,
  addIntroOutro,
  addWatermark
}