npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

filtergraph

v0.2.2

Published

Build FFmpeg filter_complex graphs with named nodes and edges

Readme

filtergraph

Build FFmpeg -filter_complex graphs with named nodes and edges instead of raw pad indices.

Why

FFmpeg filter_complex strings are write-only. A graph with 4 inputs, overlays, wipes, and audio mixing becomes an unreadable wall of [0:v]scale=... references. This library lets you think in named nodes and edges, and generates correct filter_complex output.

Install

npm install filtergraph

Zero dependencies. ESM only. Node.js 18+.

Quick Start

import { Graph } from 'filtergraph';

const g = new Graph();

// Named inputs (declaration order = ffmpeg -i order)
g.input('clip', 'video.mp4');
g.input('overlay', 'logo.png');

// Filter nodes
g.node('scaled', 'scale', { w: 1920, h: 1080 });
g.node('fmt', 'format', { pix_fmts: 'rgba' });
g.node('comp', 'overlay', { format: 'auto' });

// Edges connect them
g.edge('clip:v', 'scaled');
g.edge('overlay:v', 'fmt');
g.edge('scaled', 'comp:0');
g.edge('fmt', 'comp:1');

g.output('comp', 'outv');

// Generate the filter_complex string
console.log(g.toFilterComplex());
// → [0:v]scale=w=1920:h=1080[scaled];[1:v]format=pix_fmts=rgba[fmt];[scaled][fmt]overlay=format=auto[outv]

// Or generate a full ffmpeg command array
const cmd = g.toCommand('output.mp4');
// → ['-y', '-i', 'video.mp4', '-i', 'logo.png', '-filter_complex', '...', '-map', '[outv]', ...]

API

g.input(name, path, flags?)

Add a named file input. Declaration order determines the -i index.

g.input('clip', 'video.mp4');
g.input('title', 'title.png', { loop: 1, t: 3, r: 30 }); // per-input flags

g.source(name, filter, args?)

Add a generated source (no file). Renders inline in the filter_complex.

g.source('silence', 'anullsrc', { r: 48000, cl: 'stereo' });
g.source('black', 'color', { c: 'black', s: '1920x1080', r: 30 });

g.node(id, filter, args?)

Add a filter node. Args can be an object, a string (positional), or empty.

g.node('scaled', 'scale', { w: 1920, h: 1080 });     // → scale=w=1920:h=1080
g.node('pts', 'setpts', 'PTS-STARTPTS');               // → setpts=PTS-STARTPTS
g.node('pts', 'setpts', { expr: 'N/30/TB' });          // → setpts=N/30/TB
g.node('audio', 'acopy', {});                           // → acopy

g.edge(from, to)

Connect a source pad to a destination pad.

g.edge('clip:v', 'scaled');       // input video → filter
g.edge('clip:a', 'audio');        // input audio → filter
g.edge('scaled', 'comp:0');       // filter → multi-input pad 0
g.edge('fmt', 'comp:1');          // filter → multi-input pad 1
g.edge('split:0', 'branch_a');    // multi-output pad → filter

Input refs default to :v if no stream type specified.

g.output(nodeId, padName)

Mark a node's output as a final output. Becomes -map [padName] in the command.

g.output('comp', 'outv');
g.output('audio_trim', 'outa');

g.toFilterComplex()

Returns the -filter_complex string. Validates the graph first (throws on errors). Automatically optimizes linear chains into comma-separated sequences.

g.toCommand(outputPath, options?)

Returns a complete ffmpeg args array for child_process.spawn.

const cmd = g.toCommand('out.mp4', {
  videoCodec: 'libx264',  // default
  crf: 23,                // default
  preset: 'fast',         // default
  pixFmt: 'yuv420p',      // default
  audioCodec: 'aac',      // default
  audioBitrate: '128k',   // default
  audioRate: 48000,        // default
  audioChannels: 2,        // default
  extraArgs: ['-profile:v', 'high'],
});

spawn('ffmpeg', cmd);

Use { videoCopy: true } and/or { audioCopy: true } for stream copy.

g.toJSON()

Returns a JSON-serializable graph for visualization (e.g. with Cytoscape.js).

const json = g.toJSON();
// → { nodes: [...], edges: [...] }

Validation

The graph is validated before generation. Errors include:

  • Duplicate node id: 'base'
  • Duplicate input name: 'clip'
  • Unknown reference 'missing' in edge
  • Cycle detected: a → b → a
  • Node 'orphan' has no incoming edges
  • No outputs defined
  • Output references unknown node 'typo'
  • Node id 'clip' conflicts with input name

Chain Optimization

Linear sequences of single-in/single-out filters are automatically collapsed:

// What you write:
g.edge('clip:v', 'scaled');
g.edge('scaled', 'cropped');
g.edge('cropped', 'trimmed');

// Generated (optimized):
[0:v]scale=...,crop=...,trim=...[trimmed]

// Not the naive version:
[0:v]scale=...[scaled];[scaled]crop=...[cropped];[cropped]trim=...[trimmed]

License

MIT