Skip to content

Conversation

@AmitMY
Copy link
Contributor

@AmitMY AmitMY commented Dec 22, 2025

Summary

Simplifies the playable video encoder by replacing the custom VideoEncoder implementation with mediabunny's built-in CanvasSource and VideoSampleSource. This makes the code cleaner, more maintainable, and leverages mediabunny's optimized encoding pipeline.

Changes

PlayableVideoEncoder Refactoring

  • Removed custom VideoEncoder: Replaced manual VideoEncoder + EncodedVideoPacketSource with mediabunny's built-in sources
  • 🎯 Two-mode support: Automatically detects 'canvas' mode (skeleton) or 'bitmap' mode (human)
  • 🖼️ CanvasSource integration: Direct canvas recording for skeleton viewer with automatic frame capture
  • 📸 VideoSampleSource integration: Bitmap-based recording for human viewer with pix2pix frames
  • Simplified frame handling: Automatic timestamp/duration calculation, no more manual frame buffering
  • 🧹 Code reduction: Removed 112 lines, added 95 lines - net reduction of 17 lines with better functionality

Skeleton Viewer Updates

  • Pass canvas element to addCacheFrame() to enable CanvasSource mode
  • Direct canvas capture eliminates need for intermediate VideoFrame creation
  • Improved performance with native canvas recording

Human Viewer Updates

  • Seamlessly works with bitmap mode using VideoSampleSource
  • Compatible with existing pix2pix translation pipeline
  • No changes required to the component itself

Benefits

  • 📦 Simpler codebase: Less custom encoding logic to maintain
  • 🚀 Better performance: Leverages mediabunny's optimized pipeline
  • 🎨 Cleaner separation: Canvas vs bitmap recording modes clearly defined
  • 🔄 Automatic backpressure: Built-in handling from mediabunny
  • 🐛 Fewer bugs: Less custom code = less potential for errors

Technical Details

Before: Custom VideoEncoder manually handled encoding chunks, frame buffering, and packet management.

After: Mediabunny sources handle encoding automatically:

  • CanvasSource: For skeleton viewer - captures canvas state directly
  • VideoSampleSource: For human viewer - encodes ImageBitmap frames

Both modes use the same MP4/H.264 output format with QUALITY_HIGH bitrate for maximum compatibility.

Testing

  • ✅ Build passes with no TypeScript errors
  • ✅ Skeleton viewer: Canvas-based recording pathway updated
  • ✅ Human viewer: Bitmap-based recording pathway unchanged
  • ⚠️ Manual testing recommended to verify video recording works correctly for both skeleton and human pose viewers

🤖 Generated with Claude Code

AmitMY and others added 4 commits December 22, 2025 15:48
Replace custom VideoEncoder implementation with mediabunny's built-in CanvasSource and VideoSampleSource for cleaner, more maintainable code.

## Changes

### PlayableVideoEncoder Refactoring
- Replace custom VideoEncoder + EncodedVideoPacketSource with mediabunny's CanvasSource and VideoSampleSource
- Support two modes: 'canvas' (for skeleton viewer) and 'bitmap' (for human viewer)
- Use CanvasSource for direct canvas recording with automatic frame capture
- Use VideoSampleSource for bitmap-based recording from ImageBitmaps
- Simplify frame addition with automatic timestamp/duration calculation
- Remove frame buffering logic (handled by mediabunny)

### Skeleton Viewer Updates
- Pass canvas element to enable CanvasSource mode
- Direct canvas capture for improved performance

### Human Viewer Updates
- Continue using bitmap mode with VideoSampleSource
- Works seamlessly with pix2pix translated frames

## Benefits
- Simpler, more maintainable code
- Leverages mediabunny's optimized encoding pipeline
- Better separation of concerns (canvas vs bitmap recording)
- Automatic backpressure handling
- Reduced custom encoding logic

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Remove canvas source support to minimize the PR scope. This change focuses on using only VideoSampleSource (bitmap mode) from mediabunny, making the refactor more incremental.

Canvas source support can be added in a future enhancement once bitmap mode is proven stable.

## Changes
- Remove CanvasSource import and usage
- Remove EncoderMode type and mode branching logic
- Simplify constructor to only accept ImageBitmap
- Remove canvas parameters from pose viewer methods
- Net reduction of 48 lines of code

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
H264 codec requires even-sized frames. When images have odd dimensions (e.g., 375x375), resize them to the nearest even dimensions (376x376) before creating VideoSamples.

Fixes error: "The dimensions 375x375 are not supported for codec 'avc'; both width and height must be even numbers."

## Changes
- Calculate even dimensions in createMP4Output()
- Resize ImageBitmap to even dimensions in addFrame() if needed
- Use high-quality resize for minimal quality loss
- Clean up resized bitmap after use to prevent memory leaks

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Remove unused isPlayable() method and simplify code structure now that we only support MP4/H264.

## Changes
- Remove unused `isPlayable()` method (was never called)
- Remove `VideoCodec` type import (hardcode 'avc')
- Remove `container` and `codec` instance properties (constants now)
- Merge `createMP4Output()` logic into `init()` method
- Hardcode MIME type to 'video/mp4' in finalize()
- Keep `getMediaSourceClass()` (used by signed-language-output component)
- Keep `isSupported()` (used for feature detection across codebase)

Net result: Simpler, more focused encoder implementation optimized for H264/MP4 compatibility.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@AmitMY AmitMY merged commit 7ecd0af into master Dec 22, 2025
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants