def parse_packetized_elementary_stream(data):
    try:
        pes = bitstring.ConstBitStream(data[data.index(b'\x00\x00\x01'):])
    except __HOLE__:
        return

    while pes.pos < len(pes):
        packet = {}

        if pes.read(24) != '0x000001':
            break
        packet.update({
            'stream_id': pes.read(8),
            'packet_length': pes.read(16).uint,
        })

        if pes.peek(2) == '0b10':
            pes.read(2)

            packet.update({
                'scrambling_control': pes.read(2),
                'priority': pes.read(1).bool,
                'data_alignment_indicator': pes.read(1).bool,
                'copyright': pes.read(1).bool,
                'original_or_copy': pes.read(1).bool,
                'pts_dts_indicator': pes.read(2),
                'escr_flag': pes.read(1).bool,
                'es_rate_flag': pes.read(1).bool,
                'dsm_trick_mode_flag': pes.read(1).bool,
                'additional_copy_info_flag': pes.read(1).bool,
                'crc_flag': pes.read(1).bool,
                'extension_flag': pes.read(1).bool,
                'pes_header_length': pes.read(8).uint,
            })

            pes.read(8 * packet['pes_header_length'])

        remaining_length = packet['packet_length']
        if 'scrambling_control' in packet:
            remaining_length -= 3 + packet['pes_header_length']

        packet.update({
            'payload': pes.read(8 * remaining_length).bytes
        })
        yield packet
dataset/ETHPy150Open schlarpc/SeriousCast/mpegutils.py/parse_packetized_elementary_stream