从字节读取视频 [英] Read video from bytes

查看:40
本文介绍了从字节读取视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Flask 编写简单的 REST API,它读取上传的视频并返回一些 JSON.视频通过 POST 请求作为文件发送,内容类型为 application/x-www-form-urlencoded.

I am writing simple REST API using Flask, which reads uploaded video and returns some JSON. Video is sent as file by POST request, with content type application/x-www-form-urlencoded.

我想使用 opencv 处理它,但我不知道如何从原始二进制字符串中读取它.我想出的唯一解决方案是将视频保存到文件并使用 opencv VideoCapture 类读取它.但是我想避免 IO 操作,因为我认为它会减慢我的 API.

I want to process it using opencv, but I do not know how to read it from raw binary string. The only solution I came up with, is to save video to file and read it using opencv VideoCapture class. However I would like to avoid IO operations, because I think it will slow down my API.

所以我的问题是:

如何将视频从原始字节表示转换为 opencv 表示(numpy 数组列表)?

How can I transform video from raw bytes representation to opencv representation (list of numpy arrays)?

推荐答案

如果您将视频作为文件上传到服务器,那么听起来您已经将其保存到文件中 - 在这种情况下只需从文件可能没有那么低效.

If you are uploading the video as a file to the server then it sounds like you are already saving it to a file - in this case simply reading it from the file may not be that inefficient.

视频通常太大而无法放入内存,因此无论如何它很可能会进入磁盘并返回.

Videos are typically much too big to fit in memory, so one way or another it is likely going to be going to the disk and back anyway.

如果你的意思是你希望你的 openCV 程序直接接受流式视频,进行处理,然后做任何你想做的事情(例如保存到文件,显示,流式传输到其他地方)那么通常最容易流式传输如果可能,将视频传输到 OpenCV 程序并使用 OpenCV 直接读取流:

If you mean that you would like your openCV program to directly accept the streamed video, do the processing and then do whatever you want next (e.g. save to file, display, stream to someplace else) then its generally easiest to stream the video to the OpenCV program if possible and use OpenCV to read the stream directly:

VideoCapture cap;
cap.open(yourStreamURL);

这篇关于从字节读取视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆