Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:0xsline OpenChatCut EncodeNormalized

From Leeroopedia
Knowledge Sources
Domains Media_Processing, Encoding, Media_Import
Last Updated 2026-10-22 14:00 GMT

Overview

Concrete function that transcodes a video file to a canonical H.264/AAC MP4 format using FFmpeg with hardware acceleration, or stream-copies when the source is already compatible.

Description

The encodeNormalized function builds and executes an FFmpeg command for format normalization:

  • Hardware decode: Resolves hardware decode args (`hwaccel`) based on the preferred encoder (NVENC requires `-hwaccel cuda`).
  • Stream plan: If `streamPlan.transcodeVideo` is false, copies the video stream (`-c:v copy`). If `streamPlan.transcodeAudio` is false, copies the audio stream.
  • Audio encoding: When transcoding, encodes to AAC at the configured bitrate.
  • Video encoding: When transcoding, encodes to H.264 at the target dimensions, bitrate, and frame rate, with CFR conversion if needed.
  • Output options: `-avoid_negative_ts make_zero`, `-movflags +faststart` for streaming-compatible MP4.
  • Abort support: Honors `signal` at every step.

The companion probeVideo function probes the source to produce `ProbeMeta` (dimensions, codecs, bitrate, frame rate, VFR detection) used to decide the normalization plan.

Usage

  • Media import normalization: Called during the normalization stage of the import pipeline.
  • Transcoding service: Used by the normalization runner for batch processing.

Code Reference

Source Location

  • Repository: OpenChatCut
  • File:*** server/media-normalization.ts

Signature

export async function probeVideo(path: string, signal?: AbortSignal): Promise<ProbeMeta>;

export async function encodeNormalized(
  inputPath: string,
  outputPath: string,
  meta: ProbeMeta,
  targetW: number,
  targetH: number,
  targetBitrate: number,
  targetFps: number,
  convertToCfr: boolean,
  streamPlan: NormalizeStreamPlan,
  signal?: AbortSignal,
): Promise<void>;

Import

import { probeVideo, encodeNormalized } from '../server/media-normalization.ts';

I/O Contract

Inputs

Name Type Required Description
inputPath string Yes Source media file path
outputPath string Yes Destination MP4 file path
meta ProbeMeta Yes Probed metadata from `probeVideo`
targetW number Yes Target width (max 1920)
targetH number Yes Target height
targetBitrate number Yes Target video bitrate (1.5–8 Mbps)
targetFps number Yes Target frame rate
convertToCfr boolean Yes Convert VFR to CFR
streamPlan NormalizeStreamPlan Yes { transcodeVideo, transcodeAudio }
signal AbortSignal No Cancellation signal

Outputs

Name Type Description
Side effect File Normalized MP4 at outputPath, or stream-copied file

Usage Examples

Normalize a Video

import { probeVideo, encodeNormalized } from '../server/media-normalization.ts';

const meta = await probeVideo('/media/uploads/source.mp4');
await encodeNormalized(
  '/media/uploads/source.mp4',
  '/media/uploads/normalized.mp4',
  meta,
  1920, 1080,     // target dimensions
  4_000_000,      // 4 Mbps bitrate
  30,             // 30 fps
  true,            // convert VFR to CFR
  { transcodeVideo: true, transcodeAudio: true },
);

Stream Copy (Already Compatible)

const meta = await probeVideo('/media/uploads/source.mp4');
// If codecs are already H.264/AAC, stream plan skips transcoding
await encodeNormalized(
  '/media/uploads/source.mp4',
  '/media/uploads/copied.mp4',
  meta,
  meta.width, meta.height,
  meta.sourceBitrate,
  meta.nominalFrameRate || 30,
  false,
  { transcodeVideo: false, transcodeAudio: false },
);
// FFmpeg copies streams without re-encoding — much faster

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment