def parse_stream(inf): """ Parse KLV metadata from drone mpeg footage, taken as packets from a stream (ffmpeg) or a binary file containing the metadata. Can be called from a Bash pipeline, or directly on file from Python. ---------- inf : {stream from stdin, binary metadata file} Takes a stream of metadata from drone video mpeg file(s) or binary file(s) of drone metadata. """ metadata = collections.OrderedDict() for packet in klvdata.StreamParser(inf): metadata.update(packet.MetadataList()) #break #Capture only first packet metadata = dict(metadata) return metadata
def extract_mpegts(path): with open(path, 'rb') as stream: streams_packets = mpegtsdata.extract_streams(stream) for key in streams_packets: packets = streams_packets[key] print(f'key=0x{key:X}, packets: {len(packets)}') klv_data, pts_per_packet = reconstruct_klv_packets(packets) total_klv_packets = len(pts_per_packet) if total_klv_packets: print(f'Reconstructed packets: {total_klv_packets}') index = 0 for packet in klvdata.StreamParser(klv_data): print(f'Packet pts: {pts_per_packet[index]}') index += 1 packet.structure() packet.validate()
#!/usr/bin/env python import sys, klvdata for packet in klvdata.StreamParser(sys.stdin.buffer.read()): packet.structure() # python -c "import sys; import klvdata; for packet in klvdata.StreamParser(sys.stdin.buffer.read()): packet.structure()"
#!/usr/bin/env python3 import klvdata import sys import pickle ''' This script receives as command line parameter the name of the klvdata binary file and writes a Pickle file preserving the python object for reuse. This script ignore Unknown Elements that prevent its functioning ''' if __name__ == "__main__": args = sys.argv with open(args[1], 'rb') as f: output = open(args[1] + ".pkl", "wb") for packet in klvdata.StreamParser(f): if type(packet) is not klvdata.element.UnknownElement: metadata = packet.MetadataList() pickle.dump(metadata, output) output.close()
def extract_klv(path): with open(path, 'rb') as stream: for packet in klvdata.StreamParser(stream): packet.structure() packet.validate()