Skip to content

A full-featured download manager.

5.5 KB
Raw Blame
import { type ChildProcess, spawn as nodeSpawn } from 'node:child_process'

export interface FfmpegProgress {
  progress: number | null
}

export interface RemuxJob {
  binaryPath: string
  videoPath: string
  audioPath?: string
  output: string
  fromMpegts: boolean
  durationSec?: number
  /**
   * Explicit output muxer (`-f`). Required when `output` does not end in a
   * media extension ffmpeg can infer a muxer from — e.g. the `.motrix`
   * placeholder path a media task muxes into before the finalize rename.
   */
  format?: string
}

/**
 * The muxer ffmpeg would pick from the extension of the FINAL output name.
 * Must stay in sync with mediaFinalName's KNOWN_MEDIA_EXT: every extension
 * ensureMediaExtension can emit or pass through needs an entry here, so that
 * muxing to the extension-less `.motrix` path via `-f` produces the same
 * container the final name advertises.
 */
const EXT_TO_MUXER: Record<string, string> = {
  mp4: 'mp4',
  m4v: 'ipod',
  mov: 'mov',
  mkv: 'matroska',
  webm: 'webm',
  ts: 'mpegts',
  flv: 'flv',
  m4a: 'ipod',
  aac: 'adts',
  mp3: 'mp3',
  opus: 'opus',
}

export function muxerForOutputName(name: string): string {
  const ext = /\.([a-z0-9]{2,8})$/i.exec(name)?.[1]?.toLowerCase()
  return (ext && EXT_TO_MUXER[ext]) || 'mp4'
}

type SpawnImpl = (cmd: string, args: string[]) => ChildProcess

export class FfmpegService {
  private child: ChildProcess | null = null
  private aborted = false

  buildArgs(job: RemuxJob): string[] {
    const args: string[] = []

    args.push('-i', job.videoPath)
    if (job.audioPath) {
      args.push('-i', job.audioPath)
    }

    args.push('-c', 'copy')

    if (job.audioPath) {
      args.push('-map', '0:v:0', '-map', '1:a:0')
    }

    if (job.fromMpegts) {
      args.push('-bsf:a', 'aac_adtstoasc')
    }

    args.push('-movflags', '+faststart')
    if (job.format) {
      args.push('-f', job.format)
    }
    args.push('-progress', 'pipe:1', '-nostats', '-y', job.output)

    return args
  }

  run(
    job: RemuxJob,
    onProgress: (p: FfmpegProgress) => void,
    spawnImpl: SpawnImpl = nodeSpawn as SpawnImpl
  ): Promise<void> {
    this.aborted = false
    const args = this.buildArgs(job)
    const child = spawnImpl(job.binaryPath, args)
    this.child = child
    const durMs = (job.durationSec ?? 0) * 1000

    // Buffer a bounded tail of stderr so a non-zero exit can report WHY ffmpeg
    // failed. ffmpeg names the offending input here — e.g. for exit 234
    // (EINVAL) it prints "Stream map '0:v:0' matches no streams" (a leg is the
    // wrong stream type) or "...Invalid data found... in 'audio.mp4'". Cap to
    // the last ~8 KiB so this never grows unbounded.
    const STDERR_TAIL_LIMIT = 8192
    let stderrTail = ''
    child.stderr?.on('data', (chunk: Buffer) => {
      stderrTail += chunk.toString()
      if (stderrTail.length > STDERR_TAIL_LIMIT) {
        stderrTail = stderrTail.slice(-STDERR_TAIL_LIMIT)
      }
    })

    let closed = false
    let pending = ''
    let droppingLine = false
    let outTimeUs = 0
    const MAX_PROGRESS_LINE = 4096
    const acceptLine = (line: string) => {
      const eq = line.indexOf('=')
      if (eq < 0) return
      const key = line.slice(0, eq)
      const value = line.slice(eq + 1).trim()
      if (key === 'out_time_ms') {
        const time = Number(value)
        if (Number.isFinite(time) && time >= 0)
          outTimeUs = Math.max(outTimeUs, time)
      }
      if (key === 'progress' && (value === 'continue' || value === 'end')) {
        onProgress({
          progress:
            Number.isFinite(durMs) && durMs > 0
              ? Math.min(0.9999, outTimeUs / 1000 / durMs)
              : null,
        })
      }
    }
    child.stdout?.on('data', (chunk: Buffer) => {
      if (closed || this.aborted) return
      const text = chunk.toString()
      let start = 0
      for (let index = 0; index < text.length; index++) {
        if (text[index] !== '\n') continue
        const part = text.slice(start, index)
        if (!droppingLine && pending.length + part.length <= MAX_PROGRESS_LINE)
          acceptLine(pending + part)
        pending = ''
        droppingLine = false
        start = index + 1
      }
      if (!droppingLine) {
        const tail = text.slice(start)
        if (pending.length + tail.length <= MAX_PROGRESS_LINE) pending += tail
        else {
          pending = ''
          droppingLine = true
        }
      }
    })

    return new Promise<void>((resolve, reject) => {
      child.on('error', (err) => {
        closed = true
        reject(new Error(`mux-failed: ${err.message}`))
      })
      child.on('close', (code) => {
        if (closed) return
        closed = true
        this.child = null
        if (this.aborted) return reject(new Error('mux-aborted'))
        if (code === 0) {
          onProgress({ progress: 1 })
          return resolve()
        }
        // Append the last few non-empty stderr lines — ffmpeg names the
        // offending input/stream here. The message MUST still start with
        // "mux-failed" (MediaTaskCoordinator.mapError matches that prefix to
        // normalize task.errorMessage), so the detail goes after the code.
        const tail = stderrTail
          .split(/\r?\n/)
          .map((l) => l.trim())
          .filter(Boolean)
          .slice(-6)
          .join(' | ')
        const detail = tail ? `: ${tail}` : ''
        reject(new Error(`mux-failed: ffmpeg exited ${code}${detail}`))
      })
    })
  }

  kill(): void {
    this.aborted = true
    this.child?.kill('SIGKILL')
  }
}