Unable to extract KLV data from .mpg file

166 views Asked by At

I need to extract the klv data embedded in the following file: https://samples.ffmpeg.org/MPEG2/mpegts-klv/Day%20Flight.mpg

Currently, I am doing it using ffmpeg and python. The code works for .ts files like the example given below, but not the above mpg file.: https://www.arcgis.com/home/item.html?id=55ec6f32d5e342fcbfba376ca2cc409a

I used the following python command, using subprocess, ffmpeg to extract klv data in a binary file and then using klvdata library to tranlate to a readable text file.

#Extract klv data and output as binary file
command=['ffmpeg', '-i', input_video, '-map', 'd','-codec','copy','-f', 'data','out.bin']
process=subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()

print(stdout)
print(stderr)

#Open text file to write json data
outjson=open("./outjson.txt","w")
print("Flag 1")

# Open the out.bin file for reading as binary
with open("./out.bin", "rb") as f:
    sample=[]
    cnt=0
    for packet in klvdata.streamparser.StreamParser(f):
        pack=[]
        metadata = packet.MetadataList()
        for i in (5,6,7,13,14,15,18,19,23,24,25,26,27,28,29,30,31,32,33):#Only extracting required data
            pack.append(metadata[i][-1])
        sample.append(pack)
    sampleLength=(len(sample))
    json.dump(sample,outjson,indent=4) # Convert the metadata to a string and write it to outtext.txt

When doing it for "Day Flight.mpg", the following error occurs:

58. 19.100 / 58. 19.100\r\n  libavcodec     60. 26.100 / 60. 26.100\r\n  libavformat    60. 11.100 / 60. 11.100\r\n  libavdevice    60.  2.101 / 60.  2.101\r\n  libavfilter     9. 11.100 /  9. 11.100\r\n  libswscale      7.  3.100 /  7.  3.100\r\n  libswresample   4. 11.100 /  4. 11.100\r\n  libpostproc    57.  2.100 / 57.  2.100\r\n[mpegts @ 0000026bb99387c0] start time for stream 1 is not set in estimate_timings_from_pts\r\nInput #0, mpegts, from 'C:/Users/ashastry/Downloads/Day Flight.mpg':\r\n  Duration: 00:03:14.88, start: 10.000000, bitrate: 4187 kb/s\r\n  Program 1 \r\n  Stream #0:0[0x1e1]: Video: h264 (Main) ([27][0][0][0] / 0x001B), yuv420p(progressive), 1280x720, 60 fps, 60 tbr, 90k tbn\r\n  Stream #0:1[0x1f1]: Data: klv (KLVA / 0x41564C4B)\r\nOutput #0, data, to 'out.bin':\r\n  Metadata:\r\n    encoder         : Lavf60.11.100\r\n  Stream #0:0: Data: klv (KLVA / 0x41564C4B)\r\nStream mapping:\r\n  Stream #0:1 -> #0:0 (copy)\r\nPress [q] to stop, [?] for help\r\nsize=       0kB time=00:00:00.00 bitrate=N/A speed=N/A    \rsize=       0kB time=00:00:00.00 bitrate=N/A speed=   0x    \rsize=       1kB time=00:00:00.00 bitrate=N/A speed=   0x    \r[out#0/data @ 0000026bbb61b300] video:0kB audio:0kB subtitle:0kB other streams:1kB global headers:0kB muxing overhead: 0.000000%\r\nsize=       1kB time=00:00:00.00 bitrate=N/A speed=   0x    \r\n"
Flag 1
Traceback (most recent call last):

  File C:\ProgramData\anaconda3\Lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\users\ashastry\desktop\gis\javascript\extract.py:34
    metadata = packet.MetadataList()

AttributeError: 'UnknownElement' object has no attribute 'MetadataList'
0

There are 0 answers