Giter Club home page Giter Club logo

h26x-extractor's Introduction

h26x-extractor

PyPI version

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

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.8 or higher

Via pip:

pip3 install h26x-extractor

Status

Currently supported:

  • Parsing of H.264 bitstreams
  • Parsing of NALU
  • Parsing of AUD
  • Parsing of CodedSliceIDR
  • Parsing of CodedSliceNonIDR
  • Parsing of SPS
  • Parsing of PPS

Currently planned:

  • Parsing of SEI
  • Parsing of VUI
  • Parsing of H.265 bitstreams

Usage

If you installed the program via pip, you can run it directly:

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

Otherwise you can clone this repo and run it via:

python3 -m h264_extractor [options] <input-file>...

You can pass the -v flag to enable verbose output, e.g. the following. You will get, for each NAL unit:

  • 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

You can also use this library in your code, e.g.:

from h26x_extractor.h26x_parser import H26xParser

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

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

The raw data for all callbacks includes the RBSP.

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

from h26x_extractor.h26x_parser import H26xParser
from h26x_extractor.nalutypes import SPS

def parse_sps(bytes):
    sps = SPS(bytes)
    sps.print_verbose()

H26xParser.set_callback("sps", parse_sps)
H26xParser.parse()

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-2023 Werner Robitza

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.

h26x-extractor's People

Contributors

chemag avatar gqgs avatar slhck avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

h26x-extractor's Issues

Extractor vs. ffmpeg

Hi,

I'm wondering if the extractor would be able to point out errors. I'm having a stream, which is extracted w/o visible complains, but both - ffplay and ffprobe - complain like so:

[h264 @ 0x7ff926819000] non-existing PPS 0 referenced
Last message repeated 1 times
[h264 @ 0x7ff926819000] decode_slice_header error
[h264 @ 0x7ff926819000] no frame!

even though the video is presented (ffplay). Any idea, what they might have problems with?

Fix Emulation Prevention Bytes

This code does not handle emulation prevention bytes properly.

Idea from https://gist.github.com/figgis/fd509a02d4b1aa89f6ef:

# 7.3.1.1
# Convert NAL data (Annex B format) to RBSP data
NumBytesInRbsp = 0
rbsp_byte = BitStream()
for i in xrange(NumBytesInNalUnit):
    if (i+2) < NumBytesInNalUnit and s.peek('bits:24') == '0x000003':
        rbsp_byte.append(s.read('bits:8'))
        rbsp_byte.append(s.read('bits:8'))
        # emulation_prevention_three_byte
        s.read('bits:8')
    else:
        rbsp_byte.append(s.read('bits:8'))

NumBytesInRbsp = len(rbsp_byte)
s = rbsp_byte

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.