Skip to main content

Migrating from FFmpeg CLI to node-webcodecs

Stop fighting with filter strings. Start writing actual code.
About These Examples: The patterns below show the conceptual approach for each FFmpeg operation. Helper functions like decodeVideo(), composite(), and frameToJPEG() represent application-specific logic you’d implement. For complete, runnable implementations, see the Cookbook recipes.

Why Migrate?

Debuggable

Breakpoints and stack traces instead of stderr parsing

Testable

Unit test your video logic with Jest/Mocha

Conditional Logic

Use if/else, loops, variables — not filter DSL

Type Safe

TypeScript catches errors before encoding starts

Pattern Comparison

1. Basic Transcoding

ffmpeg -i input.mp4 \
  -c:v libx264 \
  -preset medium \
  -crf 23 \
  output.mp4
Problems:
  • Can’t inspect frames
  • No programmatic control
  • Hard to test

2. Text Overlay (Dynamic)

# Static text only
ffmpeg -i input.mp4 \
  -vf "drawtext=text='Hello':x=100:y=100:fontsize=24" \
  output.mp4
Limitations:
  • Text is fixed at encode time
  • Can’t use variables
  • No conditional display
  • String escaping nightmare for special chars

3. Watermark Overlay

ffmpeg -i input.mp4 -i logo.png \
  -filter_complex "[0:v][1:v]overlay=x=10:y=10" \
  output.mp4
Limitations:
  • Fixed position only
  • Can’t animate
  • No conditional display

4. Extract Thumbnails

ffmpeg -i input.mp4 \
  -vf "select='eq(n,0)+eq(n,100)+eq(n,200)'" \
  -vsync 0 \
  thumb%03d.jpg
Limitations:
  • Frame numbers, not timestamps
  • Can’t inspect frame content
  • No quality validation

5. Resize Video

ffmpeg -i input.mp4 \
  -vf "scale=1280:720" \
  output.mp4

6. Concatenate Videos

# Create file list
echo "file 'video1.mp4'" > list.txt
echo "file 'video2.mp4'" >> list.txt

ffmpeg -f concat -safe 0 -i list.txt \
  -c copy output.mp4
Limitations:
  • Requires intermediate file
  • Must have same codec/dimensions
  • No transition effects

7. Speed Up/Slow Down

# 2x speed
ffmpeg -i input.mp4 \
  -filter:v "setpts=0.5*PTS" \
  -filter:a "atempo=2.0" \
  output.mp4
Limitations:
  • Fixed multiplier
  • Can’t have variable speed
  • PTS math is confusing

8. Extract Audio

ffmpeg -i input.mp4 \
  -vn -acodec copy \
  output.aac

9. Create Slideshow from Images

ffmpeg -framerate 1/3 \
  -pattern_type glob -i '*.jpg' \
  -c:v libx264 \
  output.mp4
Limitations:
  • All images same duration
  • No transitions
  • Glob patterns can be unreliable

10. Live Stream Recording

ffmpeg -i rtmp://stream.url/live \
  -c copy \
  output.mp4
Limitations:
  • Can’t process frames
  • No real-time analysis
  • Can’t create highlights

Migration Checklist

1

Install node-webcodecs

npm install node-webcodecs
2

Identify your FFmpeg patterns

List all your current FFmpeg commands and their purposes
3

Map to WebCodecs APIs

  • Encoding → VideoEncoder
  • Decoding → VideoDecoder
  • Filters → Canvas API or image processing libs
4

Refactor with real code

Replace filter strings with JavaScript/TypeScript logic
5

Add tests

Unit test your video logic with Jest
6

Add type safety

Use TypeScript for compile-time error checking
##Advantages Summary
FeatureFFmpeg CLInode-webcodecs
Debuggingstderr parsingBreakpoints, stack traces
Conditionals❌ Not possible✅ if/else, switch
VariablesLimited ENV vars✅ Full JavaScript
TestingShell scripts✅ Jest/Mocha unit tests
Type Safety❌ None✅ TypeScript
Frame Access❌ No✅ Every frame
API Integration❌ Hard✅ Easy (fetch, DB)
Error HandlingExit codestry/catch
IDE Support❌ None✅ Autocomplete

Next Steps