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 🙏

© 2025 – Pkg Stats / Ryan Hefner

h264-parser

v0.2.1

Published

Full TypeScript H.264 bitstream parser with complete type definitions for SPS/PPS/SEI and NAL units.

Readme

h264-parser

Full TypeScript H.264 bitstream parser for SPS/PPS/SEI and NAL units with complete type definitions and modern ES modules support.

Installation

# Using npm
npm install h264-parser

# Using pnpm
pnpm add h264-parser

# Using yarn
yarn add h264-parser

NALUnit includes: raw bytes (rawData), RBSP after removing emulation-prevention bytes (rbspData), header info, and optional parses for SPS/PPS/SEI/AUD/SLICE when available.

API

  • parseH264AnnexB(data: Uint8Array): NALUnit[]
  • parseH264AVC(data: Uint8Array): NALUnit[]

Usage Examples

Working with Sample Files

The repository includes sample H.264 files in examples/samples/:

  • basic.h264 - Simple H.264 stream with SPS/PPS/IDR/SLICE
  • complex.h264 - More complex stream with multiple GOPs

You can run the example script to see the parser in action:

# Using pnpm
pnpm run example

# Using npm
npm run example

This will parse both sample files and show detailed information about each NAL unit.

Parsing Different NAL Unit Types

import { H264Parser } from 'h264-parser';
import { readFileSync } from 'fs';

const parser = new H264Parser();
const data = readFileSync('examples/samples/basic.h264');
const nalus = parser.parseH264AnnexB(data);

// Find specific NAL unit types
const spsNalus = nalus.filter((nalu) => nalu.nalUnitType === 7); // SPS
const ppsNalus = nalus.filter((nalu) => nalu.nalUnitType === 8); // PPS
const idrNalus = nalus.filter((nalu) => nalu.nalUnitType === 5); // IDR
const sliceNalus = nalus.filter((nalu) => nalu.nalUnitType === 1); // P-slice

console.log(
  `SPS: ${spsNalus.length}, PPS: ${ppsNalus.length}, IDR: ${idrNalus.length}, Slices: ${sliceNalus.length}`
);

Extracting SPS Information

const spsNalu = nalus.find((nalu) => nalu.sps);
if (spsNalu?.sps) {
  const sps = spsNalu.sps;
  console.log(`Profile: ${sps.profileIdc}`);
  console.log(`Level: ${sps.levelIdc}`);
  console.log(`Width: ${sps.picWidthInMbs * 16}`);
  console.log(`Height: ${sps.picHeightInMbs * 16}`);
  console.log(`Frame Rate: ${sps.frameRate}`);
}

Generate .h264 samples with FFmpeg

You can produce raw H.264 elementary streams in Annex B format using FFmpeg.

  • From an MP4 without re-encoding (bitstream copy to Annex B):
ffmpeg -i input.mp4 -c:v copy -an -bsf:v h264_mp4toannexb -f h264 output_basic.h264
  • Re-encode with x264 and fixed keyframe interval (useful for testing IDR/GOP):
ffmpeg -i input.mp4 \
  -c:v libx264 -profile:v baseline -level 3.0 \
  -x264-params keyint=60:min-keyint=60:no-scenecut=1 \
  -an -f h264 output_gop60.h264

Both outputs are Annex B. Feed their bytes directly to parseH264AnnexB. If you have length-prefixed NAL units (e.g., from some container extractions), use parseH264AVC.

TypeScript Support

This library is built with TypeScript from the ground up and provides:

  • Complete type definitions for all H.264 structures (SPS, PPS, SEI, Slice headers)
  • Full IntelliSense support in VS Code and other TypeScript editors
  • Type-safe API with comprehensive interfaces for all NAL unit types
  • Modern ES modules with proper TypeScript exports
  • Zero configuration - works out of the box with TypeScript projects

TypeScript Example

import { H264Parser, type NALUnit, type SPS, type PPS } from 'h264-parser'

const parser = new H264Parser()
const data = new Uint8Array([...]) // Your H.264 data
const nalus: NALUnit[] = parser.parseH264AnnexB(data)

// Full type safety and autocomplete
const spsNalu = nalus.find(nalu => nalu.sps)
if (spsNalu?.sps) {
  const sps: SPS = spsNalu.sps
  // TypeScript knows all available properties
  console.log(sps.profileIdc, sps.levelIdc, sps.picWidthInMbs)
}

License

MIT