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

@eplayer/emp4-cli

v0.0.3

Published

EMP4 视频转换命令行工具

Readme

EMP4 视频转换工具

将常见视频格式转换为 EMP4 加密格式的命令行工具,支持生成 EPL 流媒体清单。

特性

  • 支持多种视频格式:MP4、AVI、MKV、MOV、WMV、FLV、WebM
  • AES-256-GCM 加密,确保内容安全
  • PIN 码保护,播放时验证用户身份
  • 支持批量转换
  • 多码率输出(1080p/720p/480p)
  • 生成 EPL 流媒体清单
  • 可配置 FFmpeg/FFprobe 路径
  • 进度显示

安装

cd converter/cli
npm install
npm run build

配置

创建配置文件

运行以下命令生成默认配置文件:

node dist/cli.js --init-config

这将在当前目录创建 emp4-convert.config.json 文件。

配置文件格式

{
  "ffmpegPath": "ffmpeg",
  "ffprobePath": "ffprobe",
  "defaultPassword": "default-encryption-key",
  "defaultChunkSize": 1048576,
  "defaultSegmentDuration": 10,
  "defaultPreset": "medium",
  "defaultConcurrency": 4
}

配置项说明

| 配置项 | 类型 | 默认值 | 说明 | |--------|------|--------|------| | ffmpegPath | string | "ffmpeg" | FFmpeg 可执行文件路径,可以是绝对路径或系统 PATH 中的命令 | | ffprobePath | string | "ffprobe" | FFprobe 可执行文件路径 | | defaultPassword | string | "default-encryption-key" | 默认加密密码 | | defaultChunkSize | number | 1048576 | 默认分片大小(字节),1MB | | defaultSegmentDuration | number | 10 | 默认分片时长(秒) | | defaultPreset | string | "medium" | 默认编码预设 | | defaultConcurrency | number | 4 | 批量转换时的并发数 |

配置文件搜索路径

工具会按以下顺序搜索配置文件:

  1. 命令行 --config 参数指定的路径
  2. 当前工作目录下的 emp4-convert.config.json
  3. 当前工作目录下的 .emp4/config.json
  4. 用户主目录下的 .emp4/config.json
  5. 用户主目录下的 .emp4-convert.json

使用方法

基本用法

# 基本转换
emp4-convert video.mp4

# 指定输出路径
emp4-convert video.mp4 output.emp4

命令行参数

Usage: emp4-convert [options] [input] [output]

Arguments:
  input                     输入视频文件路径
  output                    输出 EMP4 文件路径(可选)

Options:
  -V, --version             显示版本号
  -c, --codec <codec>       视频编码器 (libx264, libx265, copy) (default: "libx264")
  -r, --resolution <res>    输出分辨率 (例: 1920x1080)
  -b, --bitrate <bitrate>   视频码率 (例: 8M)
  -f, --fps <fps>           帧率
  --fragmented              生成分片 MP4 格式 (default: true)
  --chunk-size <size>       分片大小 (default: "1M", 例: 512K, 2M, 5M)
  --segment-duration <sec>  每个分片时长 (default: 10秒)
  --password <pwd>          加密密码(不提供则使用配置文件中的默认密码)
  --pin <pin>               PIN 码(用于播放时的访问控制)
  --title <title>           视频标题
  --batch <dir>             批量转换目录
  --concurrency <n>         并发数 (default: 4)
  --preset <preset>         编码预设 (default: "medium")
  --overwrite               覆盖已存在的文件
  --multi-bitrate           生成多码率版本 (1080p/720p/480p)
  --epl                     生成 EPL 流媒体清单
  --key-server <url>        密钥服务器地址
  -v, --verbose             详细输出
  --config <path>           配置文件路径
  --init-config             创建默认配置文件
  -h, --help                显示帮助信息

使用示例

基本转换

emp4-convert video.mp4

指定编码参数

emp4-convert video.avi output.emp4 -c libx264 -r 1920x1080 -b 8M

生成分片格式

emp4-convert video.mp4 --fragmented --chunk-size 2M --title "课程第一章"

生成多码率版本和 EPL 清单

emp4-convert video.mp4 --multi-bitrate --epl --segment-duration 10 --key-server https://api.example.com/keys


node cli.js D:\BaiduNetdiskDownload\4.视频\003_数学基础_基本求导法则.mp4 --multi-bitrate --epl --segment-duration 10 --key-server http://localhost:3005/api/keys

批量转换

emp4-convert --batch ./videos --concurrency 8

使用自定义密码加密

emp4-convert video.mp4 --password "my-secret-key"

使用 PIN 码保护

PIN 码用于播放时的访问控制,播放器会在播放前要求用户输入 PIN 码验证:

# 单文件转换带 PIN 保护
emp4-convert video.mp4 --pin 123456

# EPL 流媒体模式带 PIN 保护
emp4-convert video.mp4 --epl --pin 123456

# 多码率版本带 PIN 保护
emp4-convert video.mp4 --multi-bitrate --epl --pin 123456

注意:PIN 码保护是独立于数据加密的访问控制机制。所有 EMP4 文件使用相同的默认密码加密,PIN 码仅用于播放时的身份验证。

使用指定配置文件

emp4-convert video.mp4 --config ./my-config.json

输出结构

单文件输出

output/
├── video.emp4          # 加密的 EMP4 文件
└── manifest.epl        # EPL 清单(使用 --epl 时生成)

多码率输出

output/
├── manifest.epl                # EPL 流媒体清单
├── thumbnails/                 # 缩略图目录
│   ├── thumb-0.jpg
│   └── thumb-1.jpg
└── streams/                    # 码流目录
    ├── 1080p/
    │   ├── segment-0.emp4
    │   └── segment-1.emp4
    ├── 720p/
    │   ├── segment-0.emp4
    │   └── segment-1.emp4
    └── 480p/
        ├── segment-0.emp4
        └── segment-1.emp4

EMP4 文件格式

EMP4 文件采用以下结构:

┌─────────────────────────────────────┐
│           文件头 (128 字节)           │
├─────────────────────────────────────┤
│   魔数: "EMP4" (4 bytes)             │
│   版本: "1.0.0" (8 bytes)            │
│   头部长度 (4 bytes)                  │
│   加密算法标识 (1 byte)               │
│   密钥派生方法标识 (1 byte)           │
│   Salt (32 bytes)                    │
│   IV (12 bytes)                      │
│   原始格式 (4 bytes)                  │
│   原始大小 (8 bytes)                  │
│   校验和 (32 bytes)                   │
│   元数据长度 (4 bytes)                │
│   保留字段 (18 bytes)                 │
├─────────────────────────────────────┤
│           元数据 (JSON)               │
├─────────────────────────────────────┤
│           加密数据块                   │
│   ┌─────────────────────────────┐   │
│   │ 块大小 (4 bytes)             │   │
│   │ 加密数据 (N bytes)           │   │
│   │ 认证标签 (16 bytes)          │   │
│   └─────────────────────────────┘   │
│   ... 更多数据块 ...                  │
├─────────────────────────────────────┤
│           文件尾 (12 字节)            │
│   魔数: "EMP4" (4 bytes)             │
│   文件总大小 (8 bytes)               │
└─────────────────────────────────────┘

加密规范

  • 加密算法: AES-256-GCM
  • 密钥派生: PBKDF2
    • 迭代次数: 100,000
    • 哈希算法: SHA-256
    • 密钥长度: 32 字节 (256 位)
  • IV 长度: 12 字节 (96 位,GCM 推荐值)
  • Salt 长度: 32 字节
  • 认证标签长度: 16 字节 (128 位)

PIN 码保护

PIN 码保护是独立于数据加密的访问控制机制:

| 特性 | 说明 | |------|------| | 存储位置 | 元数据中的 pinSaltpinHash 字段 | | 哈希算法 | PBKDF2-SHA256,迭代 100,000 次 | | 盐值长度 | 16 字节 | | 哈希长度 | 32 字节 | | 用途 | 播放时验证用户身份 |

PIN 码验证流程:

  1. 用户打开 EMP4 文件
  2. 检查元数据中 hasPin 字段
  3. 如果 hasPin 为 true,提示用户输入 PIN 码
  4. 使用 pinSalt 对输入进行哈希
  5. pinHash 比较验证
  6. 验证通过后开始解密播放

API 使用

除了命令行工具,也可以作为 Node.js 模块使用:

import { 
  FFmpegWrapper, 
  EMP4Encoder, 
  EPLGenerator,
  CryptoEngine,
  loadConfig 
} from 'emp4-convert'

async function convertVideo(input: string, output: string) {
  const config = loadConfig()
  const ffmpeg = new FFmpegWrapper(config.ffmpegPath, config.ffprobePath)
  const encoder = new EMP4Encoder(config.defaultPassword)

  // 获取视频信息
  const videoInfo = await ffmpeg.getVideoInfo(input)
  
  // 检查是否需要转码
  const decision = ffmpeg.needsTranscode(videoInfo)
  
  // 生成 PIN 码保护信息
  let pinOptions = {}
  if (pinCode) {
    const cryptoEngine = new CryptoEngine()
    const pinProtection = cryptoEngine.generatePinProtection(pinCode)
    pinOptions = {
      hasPin: true,
      pinSalt: pinProtection.pinSalt,
      pinHash: pinProtection.pinHash
    }
  }
  
  // 加密
  await encoder.encode(input, output, videoInfo, {
    title: 'My Video',
    chunkSize: 1024 * 1024,
    ...pinOptions
  })
}

开发

构建

npm run build

开发模式(监听文件变化)

npm run dev

清理构建产物

npm run clean

系统要求

  • Node.js >= 18.0.0
  • FFmpeg 6.0+(需要支持 libx264 和 AAC 编码)

许可证

MIT