aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/video-transcoding-profiles.ts
blob: 03c26f236bda0654ff62959fbf822e80bb4d4551 (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
import { logger } from '@server/helpers/logger'
import { getTargetBitrate } from '../../shared/models/videos'
import { AvailableEncoders, buildStreamSuffix, EncoderOptionsBuilder } from '../helpers/ffmpeg-utils'
import {
  canDoQuickAudioTranscode,
  ffprobePromise,
  getAudioStream,
  getMaxAudioBitrate,
  getVideoFileBitrate,
  getVideoStreamFromFile
} from '../helpers/ffprobe-utils'
import { VIDEO_TRANSCODING_FPS } from '../initializers/constants'

/**
 *
 * Available encoders and profiles for the transcoding jobs
 * These functions are used by ffmpeg-utils that will get the encoders and options depending on the chosen profile
 *
 */

// Resources:
//  * https://slhck.info/video/2017/03/01/rate-control.html
//  * https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate

const defaultX264VODOptionsBuilder: EncoderOptionsBuilder = async ({ input, resolution, fps }) => {
  let targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS)

  const probe = await ffprobePromise(input)

  const videoStream = await getVideoStreamFromFile(input, probe)
  if (!videoStream) {
    return { outputOptions: [ ] }
  }

  // Don't transcode to an higher bitrate than the original file
  const fileBitrate = await getVideoFileBitrate(input, probe)
  targetBitrate = Math.min(targetBitrate, fileBitrate)

  return {
    outputOptions: [
      `-maxrate ${targetBitrate}`,
      `-bufsize ${targetBitrate * 2}`
    ]
  }
}

const defaultX264LiveOptionsBuilder: EncoderOptionsBuilder = async ({ resolution, fps, streamNum }) => {
  const targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS)

  return {
    outputOptions: [
      `${buildStreamSuffix('-b:v', streamNum)} ${targetBitrate}`,
      `-maxrate ${targetBitrate}`,
      `-bufsize ${targetBitrate * 2}`
    ]
  }
}

const defaultAACOptionsBuilder: EncoderOptionsBuilder = async ({ input, streamNum }) => {
  const probe = await ffprobePromise(input)

  if (await canDoQuickAudioTranscode(input, probe)) {
    logger.debug('Copy audio stream %s by AAC encoder.', input)
    return { copy: true, outputOptions: [] }
  }

  const parsedAudio = await getAudioStream(input, probe)

  // We try to reduce the ceiling bitrate by making rough matches of bitrates
  // Of course this is far from perfect, but it might save some space in the end

  const audioCodecName = parsedAudio.audioStream['codec_name']

  const bitrate = getMaxAudioBitrate(audioCodecName, parsedAudio.bitrate)

  logger.debug('Calculating audio bitrate of %s by AAC encoder.', input, { bitrate: parsedAudio.bitrate, audioCodecName })

  if (bitrate !== undefined && bitrate !== -1) {
    return { outputOptions: [ buildStreamSuffix('-b:a', streamNum), bitrate + 'k' ] }
  }

  return { outputOptions: [ ] }
}

const defaultLibFDKAACVODOptionsBuilder: EncoderOptionsBuilder = ({ streamNum }) => {
  return { outputOptions: [ buildStreamSuffix('-q:a', streamNum), '5' ] }
}

const availableEncoders: AvailableEncoders = {
  vod: {
    libx264: {
      default: defaultX264VODOptionsBuilder
    },
    aac: {
      default: defaultAACOptionsBuilder
    },
    libfdk_aac: {
      default: defaultLibFDKAACVODOptionsBuilder
    }
  },
  live: {
    libx264: {
      default: defaultX264LiveOptionsBuilder
    },
    aac: {
      default: defaultAACOptionsBuilder
    }
  }
}

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

export {
  availableEncoders
}

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