Code coverage report for src/utils/parsing.ts

# All files / src/utils parsing.ts

79.64% Statements 223/280
100% Branches 51/51
66.66% Functions 8/12
79.64% Lines 223/280

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    2811x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    7x
    3x
    7x
    4x
    4x
    4x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    21x
    21x
    21x
    4x
    4x
    4x
    17x
    21x
    6x
    21x
    3x
    11x
    2x
    8x
    3x
    3x
    3x
    3x
    21x
    3x
    3x
    3x
    1x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    18x
    18x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    16x
    18x
    13x
    13x
    2x
    2x
    2x
    11x
    11x
    13x
    2x
    2x
    2x
    2x
    9x
    9x
    13x
    2x
    2x
    2x
    2x
    13x
    10x
    18x
    1x
    1x
    10x
    18x
    3x
    3x
    3x
    3x
    3x
    18x
    2x
    1x
    5x
    5x
    5x
    5x
    5x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    7x
    7x
    5x
    5x
    5x
    5x
    5x
    5x
    5x
    5x
    5x
    5x
    5x
    5x
    5x
    5x
    3x
    3x
    1x
    1x
    1x
    1x
    1x
    3x
    5x
    5x
    3x
    3x
    1x
    1x
    1x
    1x
    1x
    3x
    5x
    5x
    5x
    7x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    5x
    5x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    5x
    1x
    1x
    1x
    1x

    1x

    1x

    1x

     import {
      FfmpegCodec,
      FfmpegCodecType,
      FfmpegCodecs,
      FfmpegEncoderType,
      FfmpegEncoders,
      FfmpegFilterStreamType,
      FfmpegFilters,
      FfmpegFormats,
      InputCodecInformation,
      InputStreamCodecInformation,
      ProgressInformation
    } from './data-types'

    import {
      capCodecDecodersRegexp,
      capCodecEncodersRegexp,
      capCodecRegexp,
      capEncoderRegexp,
      capFilterRegexp,
      capFormatRegexp,
      codecAudioRegexp,
      codecDurRegexp,
      codecEndRegexp,
      codecInputRegexp,
      codecOutputRegexp,
      codecVideoRegexp
    } from './regexp'

    /**
     * Extract an error message from ffmpeg stderr
     *
     * @param stderrLines stderr output from ffmpeg as an array of lines
     * @returns error message
     */
    export function extractErrorMessage(stderrLines: string[]): string {
      // Return the last block of lines that don't start with a space or square bracket
      return stderrLines
        .reduce((messages: string[], message: string): string[] => {
          if (message.charAt(0) === ' ' || message.charAt(0) === '[') {
            return []
          } else {
            messages.push(message)
            return messages
          }
        }, [])
        .join('\n')
    }

    /**
     * Extract progress information from ffmpeg stderr
     *
     * @param stderrLine a line from ffmpeg stderr
     * @returns progress information
     */
    export function extractProgress(
      stderrLine: string
    ): ProgressInformation | undefined {
      let parts = stderrLine.replace(/=\s+/g, '=').trim().split(' ')
      let progress: ProgressInformation = {}

      for (let part of parts) {
        let [key, value] = part.split('=', 2)

        if (value === undefined) {
          // Not a progress line
          return
        }

        if (key === 'frame' || key === 'fps') {
          progress[key] = Number(value)
        } else if (key === 'bitrate') {
          progress.bitrate = Number(value.replace('kbits/s', ''))
        } else if (key === 'size' || key === 'Lsize') {
          progress.size = Number(value.replace('kB', ''))
        } else if (key === 'time') {
          progress.time = value
        } else if (key === 'speed') {
          progress.speed = Number(value.replace('x', ''))
        }
      }

      return progress
    }

    export class CodecDataExtractor {
      inputs: InputStreamCodecInformation[]
      index: number
      inInput: boolean
      done: boolean
      callback: (data: InputCodecInformation) => any

      constructor(callback: (data: InputCodecInformation) => any) {
        this.inputs = []
        this.index = -1
        this.inInput = false
        this.done = false
        this.callback = callback
      }

      // TODO better output for multiple inputs / multi-stream inputs !
      processLine(line: string) {
        let matchFormat = line.match(codecInputRegexp)
        if (matchFormat) {
          this.inInput = true
          this.index++
          this.inputs[this.index] = {
            format: matchFormat[1]
          }

          return
        }

        if (this.inInput) {
          let durationMatch = line.match(codecDurRegexp)
          if (durationMatch) {
            this.inputs[this.index].duration = durationMatch[1]
            return
          }

          let audioMatch = line.match(codecAudioRegexp)
          if (audioMatch) {
            this.inputs[this.index].audio = audioMatch[1].split(', ')[0]
            this.inputs[this.index].audioDetails = audioMatch[1]
            return
          }

          let videoMatch = line.match(codecVideoRegexp)
          if (videoMatch) {
            this.inputs[this.index].video = videoMatch[1].split(', ')[0]
            this.inputs[this.index].videoDetails = videoMatch[1]
            return
          }
        }

        if (codecOutputRegexp.test(line)) {
          this.inInput = false
        }

        if (codecEndRegexp.test(line)) {
          this.done = true
          let { callback } = this

          callback(this.inputs)
        }
      }
    }

    function parseCodecType(type: string): FfmpegCodecType {
      if (type === 'A') return 'audio'
      if (type === 'V') return 'video'
      if (type === 'S') return 'subtitle'
      if (type === 'D') return 'data'
      return 'attachment'
    }

    export function extractCodecs(lines: string[]): FfmpegCodecs {
      let codecs: FfmpegCodecs = {}

      for (let line of lines) {
        let match = line.match(capCodecRegexp)
        if (match) {
          let [, decode, encode, type, intra, lossy, lossless, name, description] =
            match

          let codec: FfmpegCodec = {
            description,
            type: parseCodecType(type),
            canEncode: encode === 'E',
            canDecode: decode === 'D',
            intraFrame: intra === 'I',
            lossy: lossy === 'L',
            lossless: lossless === 'S'
          }

          if (decode === 'D') {
            let decoders = description.match(capCodecDecodersRegexp)
            if (decoders) {
              codec.decoders = decoders[1].trim().split(' ')
              codec.description = codec.description
                .replace(capCodecDecodersRegexp, '')
                .trim()
            }
          }

          if (encode === 'E') {
            let encoders = description.match(capCodecEncodersRegexp)
            if (encoders) {
              codec.encoders = encoders[1].trim().split(' ')
              codec.description = codec.description
                .replace(capCodecEncodersRegexp, '')
                .trim()
            }
          }

          codecs[name] = codec
        }
      }

      return codecs
    }

    export function extractFormats(lines: string[]): FfmpegFormats {
      let formats: FfmpegFormats = {}

      for (let line of lines) {
        let match = line.match(capFormatRegexp)
        if (match) {
          let [, demux, mux, name, description] = match
          formats[name] = {
            description,
            canMux: mux === 'E',
            canDemux: demux === 'D'
          }
        }
      }

      return formats
    }

    function parseFilterStreams(
      streams: string
    ): FfmpegFilterStreamType[] | 'dynamic' {
      if (streams === '|') {
        return []
      } else if (streams === 'N') {
        return 'dynamic'
      } else {
        return [...streams].map((s) => (s === 'A' ? 'audio' : 'video'))
      }
    }

    export function extractFilters(lines: string[]): FfmpegFilters {
      let filters: FfmpegFilters = {}

      for (let line of lines) {
        let match = line.match(capFilterRegexp)
        if (match) {
          let [, timeline, slice, command, name, inputs, outputs, description] =
            match

          filters[name] = {
            description,
            inputs: parseFilterStreams(inputs),
            outputs: parseFilterStreams(outputs)
          }
        }
      }

      return filters
    }

    function parseEncoderType(type: string): FfmpegEncoderType {
      if (type === 'A') return 'audio'
      if (type === 'V') return 'video'
      return 'subtitle'
    }

    export function extractEncoders(lines: string[]): FfmpegEncoders {
      let encoders: FfmpegEncoders = {}

      for (let line of lines) {
        let match = line.match(capEncoderRegexp)
        if (match) {
          let [, type, frame, slice, exp, band, direct, name, description] = match

          encoders[name] = {
            description,
            type: parseEncoderType(type),
            frameMultithreading: frame === 'F',
            sliceMultithreading: slice === 'S',
            experimental: exp === 'X',
            drawHorizBand: band === 'B',
            directRendering: direct === 'D'
          }
        }
      }

      return encoders
    }