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

open-video-player-wasm

v0.1.1

Published

一个基于WebAssembly和Rust的自定义视频播放器,专门解决原生HTML5 video标签在快速拖拽进度条时画面不连贯的问题。

Readme

自定义视频播放器 (Custom Video Player)

一个基于WebAssembly和Rust的自定义视频播放器,专门解决原生HTML5 video标签在快速拖拽进度条时画面不连贯的问题。

🚀 核心特性

  • 完全自定义渲染:不依赖浏览器原生video标签,使用Canvas进行帧级别控制
  • 流畅的进度条拖拽:智能帧缓存机制,支持快速seek而不会出现画面卡顿
  • 高性能:基于Rust和WebAssembly,提供原生级别的性能
  • 自定义控制器:完全可定制的播放控制界面
  • 帧缓存优化:LRU缓存算法,优化内存使用和渲染速度

🛠️ 技术架构

核心组件

  1. VideoFrame: 视频帧数据结构,包含时间戳和RGBA像素数据
  2. VideoTrack: 视频轨道,包含所有帧和元数据
  3. FrameCache: 智能帧缓存,使用LRU算法管理内存
  4. CustomVideoPlayer: 主播放器类,提供完整的播放控制API

技术栈

  • Rust: 核心逻辑和性能关键代码
  • WebAssembly: 在浏览器中运行Rust代码
  • Canvas API: 用于帧渲染和显示
  • JavaScript: 前端交互和控制逻辑

📋 快速开始

1. 安装依赖

# 安装 wasm-pack (如果还没有安装)
cargo install wasm-pack

2. 构建项目

# 构建 WebAssembly 模块
wasm-pack build --target web

3. 启动开发服务器

# 使用Python启动HTTP服务器
python3 -m http.server 8000

# 或者使用Node.js
npx serve .

4. 打开浏览器

访问 http://localhost:8000/example.html 查看演示

🎮 API 使用

基本用法

import init, { create_test_player, create_player_from_url } from './pkg/video_player.js';

// 初始化WebAssembly模块
await init();

// 方法1: 创建测试播放器
const player = create_test_player("player-container");

// 方法2: 从URL创建播放器
const player2 = create_player_from_url("player-container", "https://example.com/video.mp4");

// 方法3: 运行时加载视频URL
const player3 = create_test_player("player-container");
player3.load_from_url("https://sample-videos.com/zip/10/mp4/SampleVideo_640x360_1mb.mp4");

// 播放控制
player.play();
player.pause();
player.stop();

// 跳转到指定时间(秒)
player.seek(5.0);

// 设置播放速度
player.set_playback_rate(1.5);

// 音量控制
player.set_volume(0.8);
player.set_muted(true);

CDN视频加载

播放器支持多种视频源:

// CDN视频链接
player.load_from_url("https://sample-videos.com/zip/10/mp4/SampleVideo_1280x720_1mb.mp4");

// 本地文件 (通过File API)
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.accept = 'video/*';
fileInput.onchange = (e) => {
    const file = e.target.files[0];
    const url = URL.createObjectURL(file);
    player.load_from_url(url);
};

// Base64编码的视频
player.load_from_url("data:video/mp4;base64,AAAAHGZ0eXBpc29tAAACAGlzb21pc28y...");

高级配置

// 创建自定义配置
const options = new CustomVideoPlayerOptions();
options.set_width(1280);
options.set_height(720);
options.set_frame_cache_size(200); // 缓存200帧
options.set_seek_accuracy(0.05);   // 0.05秒精度

// 使用自定义配置创建播放器
const player = new CustomVideoPlayer("container-id", options);

🔧 核心优势

1. 解决原生video标签的问题

原生HTML5 video标签在快速拖拽进度条时存在以下问题:

  • 画面卡顿和不连贯
  • seek操作延迟高
  • 缓存机制不可控

2. 自定义解决方案

我们的播放器通过以下方式解决这些问题:

// 智能帧缓存
impl FrameCache {
    fn get(&mut self, frame_index: u32) -> Option<&VideoFrame> {
        // LRU缓存算法,快速访问常用帧
    }
    
    fn put(&mut self, frame_index: u32, frame: VideoFrame) {
        // 智能缓存管理,避免内存溢出
    }
}

// 精确的时间控制
pub fn seek(&mut self, time: f64) -> Result<(), JsValue> {
    let clamped_time = time.max(0.0).min(video_track.duration);
    self.current_time = clamped_time;
    self.render_current_frame()?; // 立即渲染
    Ok(())
}

3. 性能优化

  • WebAssembly性能:核心逻辑用Rust实现,接近原生性能
  • Canvas渲染:直接操作像素数据,无中间层开销
  • 内存管理:Rust的零开销抽象和内存安全

📊 性能对比

| 特性 | 原生Video | 自定义播放器 | |------|-----------|-------------| | Seek延迟 | 100-500ms | <16ms | | 内存使用 | 不可控 | 精确控制 | | 渲染质量 | 依赖浏览器 | 完全自定义 | | 拖拽体验 | 卡顿 | 流畅 |

🎯 应用场景

  • 视频编辑工具:需要精确帧控制的应用
  • 教育平台:频繁跳转和回放的学习视频
  • 监控系统:需要快速浏览大量视频片段
  • 游戏录像:高精度的回放和分析工具

⚠️ 当前版本限制

重要说明: 当前版本(v0.1.0)是概念验证版本,具有以下限制:

模拟视频数据

  • 当前版本使用主题化的模拟视频数据进行演示
  • 支持通过URL识别并创建不同主题的动画:
    • Big Buck Bunny URL → 蓝色主题动画
    • Sample Video URL → 绿色主题动画
    • 本地文件(blob:) → 紫色主题动画
    • 其他URL → 彩虹渐变动画

视频格式支持

  • 暂不支持真实的MP4/WebM等格式解码
  • 所有视频源都会转换为程序生成的主题动画
  • 保持了完整的播放器控制接口和流畅的用户体验

为什么使用模拟数据?

  1. 专注核心问题: 优先解决进度条拖拽卡顿问题
  2. 架构验证: 验证自定义渲染架构的可行性
  3. 性能测试: 测试Canvas渲染和帧缓存机制
  4. 用户界面: 完善播放控制和用户交互

🔮 下一步计划

短期目标 (v0.2.0)

  • [ ] 集成真实的MP4解析器 (使用mp4parse-rs)
  • [ ] 实现基础的H.264视频帧解码
  • [ ] 支持常见的MP4容器格式

中期目标 (v0.3.0)

  • [ ] 添加音频轨道支持和同步
  • [ ] 实现硬件加速解码 (WebCodecs API)
  • [ ] 支持WebM和其他格式
  • [ ] 优化大文件的流式加载

长期目标 (v1.0.0)

  • [ ] 完整的编解码器支持
  • [ ] 字幕和多轨道支持
  • [ ] 移动端优化
  • [ ] 专业级视频编辑功能

📝 许可证

MIT License

🤝 贡献

欢迎提交Issue和Pull Request!


注意:当前版本使用模拟视频数据进行演示。在生产环境中,需要集成真实的视频解码器来处理MP4等格式的文件。