All files / src output.ts

97.97% Statements 194/198
93.75% Branches 30/32
100% Functions 9/9
97.97% Lines 194/198

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 1991x 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 1x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 5x 5x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 8x 8x 18x 18x 8x 2x 8x 6x 6x 6x 8x 18x 18x 10x 10x 10x 1x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 10x 10x 10x 18x 18x 10x 10x 10x 1x 10x 2x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 2x 2x 1x 1x 2x 2x     2x 2x 2x 10x 10x 10x 18x 18x 10x 10x 10x 1x 1x 10x 10x 1x 1x 10x 10x 2x 2x 10x 10x 1x 1x 10x 10x 10x 18x 18x 10x 9x 10x 1x 1x 10x 18x 18x 10x 10x 10x 10x 10x 10x 10x 10x 18x  
import { Writable } from 'node:stream'
 
import { protocolRegexp } from './utils/regexp'
import { formatBitrate } from './utils/formatting'
import { FilterChain, generateFilterChain } from './utils/filters'
 
export type OutputAudioOptions = {
  codec?: string
  bitrate?: string | number
  channels?: number
  frequency?: number
  quality?: number
  filters?: FilterChain
}
 
export type OutputVideoOptions = {
  codec?: string
  bitrate?: string | number
  constantBitrate?: boolean
  fps?: number
  frames?: number
  filters?: FilterChain
}
 
type OutputTarget = string | Writable
 
export type OutputOptions = {
  target: OutputTarget
  seek?: string | number
  duration?: string | number
  format?: string
  map?: string
  customArgs?: string[]
 
  audio?: false | OutputAudioOptions
  video?: false | OutputVideoOptions
}
 
export type OutputDefinition = OutputTarget | OutputOptions
 
export class FfmpegOutput implements OutputOptions {
  target: OutputTarget
  seek?: string | number
  duration?: string | number
  format?: string
  map?: string
  customArgs: string[]
 
  audio?: false | OutputAudioOptions
  video?: false | OutputVideoOptions
 
  constructor(options: OutputDefinition) {
    if (typeof options === 'string' || options instanceof Writable) {
      options = { target: options }
    }
 
    this.target = options.target
    this.seek = options.seek
    this.duration = options.duration
    this.format = options.format
    this.map = options.map
    this.customArgs = options.customArgs || []
 
    this.audio = options.audio
    this.video = options.video
  }
 
  get isStream(): boolean {
    return this.target instanceof Writable
  }
 
  get isLocalFile(): boolean {
    if (this.target instanceof Writable) {
      return false
    } else {
      let protocol = this.target.match(protocolRegexp)
      return !protocol || protocol[1] === 'file'
    }
  }
 
  #getAudioOptions(): string[] {
    let options: string[] = []
 
    if (this.audio === false) {
      options.push('-an')
    } else if (this.audio) {
      if (this.audio.codec) {
        options.push('-acodec', this.audio.codec)
      }
 
      if (this.audio.bitrate) {
        options.push('-b:a', formatBitrate(this.audio.bitrate))
      }
 
      if (this.audio.channels) {
        options.push('-ac', this.audio.channels.toString())
      }
 
      if (this.audio.frequency) {
        options.push('-ar', this.audio.frequency.toString())
      }
 
      if (this.audio.quality) {
        options.push('-aq', this.audio.quality.toString())
      }
 
      if (this.audio.filters) {
        options.push('-filter:a', generateFilterChain(this.audio.filters))
      }
    }
 
    return options
  }
 
  #getVideoOptions(): string[] {
    let options: string[] = []
 
    if (this.video === false) {
      options.push('-vn')
    } else if (this.video) {
      if (this.video.codec) {
        options.push('-vcodec', this.video.codec)
      }
 
      if (this.video.bitrate) {
        let bitrate = formatBitrate(this.video.bitrate)
        options.push('-b:v', bitrate)
 
        if (this.video.constantBitrate) {
          options.push(
            '-minrate',
            bitrate,
            '-maxrate',
            bitrate,
            '-bufsize',
            bitrate
          )
        }
      }
 
      if (this.video.fps) {
        options.push('-r', this.video.fps.toString())
      }
 
      if (this.video.frames) {
        options.push('-vframes', this.video.frames.toString())
      }
 
      if (this.video.filters) {
        options.push('-filter:v', generateFilterChain(this.video.filters))
      }
 
      // todo size filters
    }
 
    return options
  }
 
  #getOptions(): string[] {
    let options: string[] = []
 
    if (this.seek) {
      options.push('-ss', this.seek.toString())
    }
 
    if (this.duration) {
      options.push('-t', this.duration.toString())
    }
 
    if (this.format) {
      options.push('-f', this.format)
    }
 
    if (this.map) {
      options.push('-map', this.map)
    }
 
    return options
  }
 
  #getOutputString(): string {
    if (typeof this.target === 'string') {
      return this.target
    } else {
      return 'pipe:1'
    }
  }
 
  getFfmpegArguments(): string[] {
    return [
      ...this.#getAudioOptions(),
      ...this.#getVideoOptions(),
      ...this.#getOptions(),
      ...this.customArgs,
      this.#getOutputString()
    ]
  }
}