Skip to content
Advertisement

How to read MKV bytes as video?

I am receiving fragment of MKV video in bytes. I need to take 1st frame of it for later processing without saving video in disk. For similar problem with image I use OpenCV or PIL and everything works fine, however, I am not able to do the same with video data. Any tips how to read video from bytes to memory object that I could use for later processing with OpenCV or some other library?

Advertisement

Answer

As I don’t have your bytes buffer, I just created an MKV video file with ffmpeg like this:

JavaScript

I then installed imageio with:

JavaScript

Then I loaded the entire MKV video into memory so I have something that must look pretty much the same as the bytes object you receive stored in my variable content:

JavaScript

Now I should be set up and looking the same as you. Just for reference, the first few bytes of content look like this:

JavaScript

So, let’s continue.

JavaScript

I can now print the mean of each frame like this:

JavaScript

Or get the metadata and print it like this:

JavaScript

Keywords: Python, video, frame, individual frame, FFmpeg, imageio, single frame, BytesIO, bytes, MKV, Matroska.

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