Skip to content
Advertisement

Simultaneously map video and data streams to one subprocess pipeline in real-time

I need to process the video stream and the klvdata streams simultaneously in real-time in OpenCV/Python. I’m using FFMPEG to read the file or stream as OpenCV does not retain the klvdata. I pass the data to OpenCV with the subprocess module.

My problem is I cannot figure out how to map both the video and klvdata to the same subprocess pipe simultaneously?

My code:

JavaScript

Produces the below error:

JavaScript

Any help is greatly appreciated.

Advertisement

Answer

For splitting the video and data, we may map the video stream to stderr pipe and map the KLV data stream to to stdout pipe.

The same technique is used for separating video and audio in my following answer.

Accurate synchronization between the the video frame and the corresponding data is relatively simple when each video frame has private KLV data (synchronize by sequential order).

The Day Flight.mpg sample file has much fewer data packets than frames, and accurate synchronization is not possible using the suggested solution (I don’t think it is possible using the pipes approach).
We may still apply some coarse synchronization – assume the data and the frame are read in time proximity.

Suggested way for splitting the video and the data:

JavaScript

The video and the data are read in two separate threads:

  • Video reader thread – read raw video frames (in BGR) format.
  • Data reader thread – read and parse KLV data.

According to Wikipedia, the KLV format is not well defined:

Keys can be 1, 2, 4, or 16 bytes in length.
Presumably in a separate specification document you would agree on a key length for a given application.

In the sample video, the key length is 16 bytes, but it is not guaranteed…


Reading the KLV data from stdout pipe:
When reading data from a pipe (in real-time like manner), we need to know the expected number of bytes to read.
That forces us to do partial parsing of the KLV data:

  • Read the “key” (assume 16 bytes length).
  • Read the “length” – there is some challenge with “BER length” standard.
  • Read the the “data” (size to read is defined by the length).

After reading the key, length and data, we have one “KLV data packet”, we can send to the KLV data parser.


Here is a code sample that woks with Day Flight.mpg sample input file:

JavaScript

The above code saves the data to data.bin (for testing).
data.bin may be used for consistency check.
Execute FFmpeg CLI for extracting the data stream:

JavaScript

Verify that raw.bin and data.bin files are equal.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement