Skip to main content

Extract NAL units from H.264 bitstreams

Project description

h26x-extractor

PyPI version

Author: Werner Robitza, with contributions from @chemag, Paulo Sherring, Thomas Jammet.

Extracts NAL units from H.264 bitstreams and decodes their type and content, if supported.

⚠️ h26x-extractor is neither fast nor robust to bitstream errors. It's rather a playground for parsing bitstreams. Use with caution! This program is no longer maintained, PRs are welcome.

Contents:

Installation

Requirements: Python 3.9 or higher

Via uv:

uvx h26x-extractor

Via pipx:

pipx install h26x-extractor

Via pip:

pip3 install --user h26x_extractor

For development, clone the repository and install with uv:

git clone https://github.com/slhck/h26x-extractor
cd h26x-extractor
uv sync

Status

Currently supported:

  • Parsing of H.264 bitstreams
  • Parsing of NALU
  • Parsing of AUD
  • Parsing of CodedSlice(s)
  • Parsing of SPS
  • Parsing of PPS
  • Parsing of Prefix for Scalable Video Coding

Currently planned:

  • Parsing of SEI
  • Parsing of H.265 bitstreams

Usage

If you use uvx you can run it directly without installation:

uvx h26x-extractor [options] <input-file>...

For development, you can also run it via:

uv run h26x-extractor [options] <input-file>...

Detailed usage options:

h26x-extractor

Usage:
  h26x-extractor [options] <input-file>...

Options:
  -v --verbose                   Enable human-readable output to stderr for all NALU types.
  -t --verbose-types TYPES       Comma-separated list of NALU types for human-readable output.
  -o --output-file FILE          Write JSON output to FILE instead of stdout.

Output:
  By default, JSON output is written to stdout.
  Use -v or -t to additionally print human-readable output to stderr.

Example:
  h26x-extractor file1.264 file2.264
  h26x-extractor -v file.264 2>/dev/null  # JSON only
  h26x-extractor -v file.264 > /dev/null  # Human-readable only
  h26x-extractor -o output.json file.264

Output Format

By default, h26x-extractor outputs JSON to stdout, which is suitable for automation and piping to other tools:

h26x-extractor video.264

Example JSON output:

[
  {
    "input_file": "video.264",
    "position": {
      "start": 0,
      "end": 28,
      "length": 29
    },
    "type": 7,
    "type_name": "Sequence parameter set",
    "nalu_bytes": "0000000167...",
    "rbsp_bytes": "67...",
    "fields": {
      "profile_idc": 100,
      "level_idc": 31,
      ...
    }
  },
  ...
]

Options

  • -o FILE, --output-file FILE: Write JSON output to a file instead of stdout
  • -v, --verbose: Enable human-readable output to stderr for all NALU types
  • -t TYPES, --verbose-types TYPES: Comma-separated list of NALU types for human-readable output to stderr

Examples

# JSON to stdout (default)
h26x-extractor video.264

# JSON to file
h26x-extractor -o output.json video.264

# JSON to stdout + human-readable tables to stderr
h26x-extractor -v video.264

# JSON only (discard human-readable output)
h26x-extractor -v video.264 2>/dev/null

# Human-readable only (discard JSON)
h26x-extractor -v video.264 > /dev/null

# Only show specific NALU types (7=SPS, 8=PPS) in human-readable output
h26x-extractor -t 7,8 video.264

Human-Readable Output

When using -v or -t, human-readable output is written to stderr. For each NAL unit, you will get:

  • The byte position range
  • The offset from the start of the stream
  • The overall length including start code
  • The type (also translated in plaintext)
  • Its content in raw bytes, encoded as hex
  • Its RBSP content
  • A table with its content decoded, if supported

Example:

NALU bytepos:   [0, 28]
NALU offset:    0 Bytes
NALU length:    29 Bytes (including start code)
NALU type:      7 (Sequence parameter set)
NALU bytes:     0x0000000167f4000d919b28283f6022000003000200000300641e28532c
NALU RBSP:      0xf4000d919b28283f602200000002000000641e28532c

SPS (payload size: 22.0 Bytes)
+--------------------------------------+---------+
| field                                | value   |
+======================================+=========+
| constraint_set0_flag                 | 0       |
+--------------------------------------+---------+
| constraint_set1_flag                 | 0       |
+--------------------------------------+---------+
....

Programmatic usage (API)

API

This program has a simple API that can be used to integrate it into other Python programs.

For more information see the API documentation.

Here is an example:

from h26x_extractor.h26x_parser import H26xParser

def do_something(nalu):
    pass
    # do something with the NALU instance

H26xParser.set_callback("nalu", do_something)
H26xParser.parse()

The callback is called for each type of info found. Valid callbacks are:

  • sps
  • pps
  • slice
  • aud
  • nalu
  • prefix

The callback returns an instance of the NALU type. You can access the s property to get the whole data including the RBSP. You can use the set_allcallbacks method to set callbacks for all types. You can also call to_dict() on any NALU instance to get a dictionary representation suitable for JSON serialization.

You can also call the nalutypes classes to decode the individual fields, e.g. nalutypes.SPS:

from h26x_extractor.nalutypes import SPS
from bitstring import BitStream

nal_payload = "0x0000000167f4000d919b28283f6022000003000200000300641e28532c"
sps = SPS(BitStream(nal_payload))
sps.print_verbose()

See the tests/test_h26x_extractor.py file for more examples.

Alternatives

h264bitstream is a proper H.264 parser.

FFmpeg can also parse bitstream data:

ffmpeg -i video.h264 -c copy -bsf:v trace_headers -f null - 2> output.txt

License

The MIT License (MIT)

Copyright (c) 2017-2025 h26x-extractor contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

h26x_extractor-0.11.0.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

h26x_extractor-0.11.0-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file h26x_extractor-0.11.0.tar.gz.

File metadata

  • Download URL: h26x_extractor-0.11.0.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for h26x_extractor-0.11.0.tar.gz
Algorithm Hash digest
SHA256 c0bbdb2e0b15bfbb346ccf76645237979dc52c2b912932a0002b85a97df3a668
MD5 5fcd25c83d76414a56be64acf49f4f04
BLAKE2b-256 bee309a43f15b54f2d83e367eda816a559580dd8bce0eaf21cfa582da603e394

See more details on using hashes here.

File details

Details for the file h26x_extractor-0.11.0-py3-none-any.whl.

File metadata

File hashes

Hashes for h26x_extractor-0.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 52ccba3b0fa8d117495d8a1f0599dde84765b3e9ff97503f7a4593d9358c7c79
MD5 99812e0bdc389ad6549fb37ba041e64a
BLAKE2b-256 5d4c8683b36ab1855a5995019c3f8bf775c73f69d4399dec07311722449f4442

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page