如何使用 .NET Core 解析 MPEG 视频流 [英] How to Parse an MPEG Video Stream using .NET Core
问题描述
对于个人项目,我试图从 IP 摄像机的 MPEG 流中读取数据,并对接收到的各个帧执行一些计算机视觉任务(使用 .NET Core 2.2).
For a personal project, I am attempting to read from a IP Camera's MPEG stream and perform some computer-vision tasks on the individual frames I receive (Using .NET Core 2.2).
我向摄像机的 MPEG 端点执行 GET
请求,然后返回一个 multipart/x-mixed-replace
响应,该响应连续流式传输看似单个帧的内容作为 JPEG 图像.
I perform a GET
request to the Camera's MPEG endpoint and I'm returned a multipart/x-mixed-replace
response that continually streams what appear to be individual frames as JPEG images.
我无法理解的是从多部分响应中解析出帧的正确方法 - 如何检索帧对我来说并不重要,如果有更好的解析方法这种流,我当然愿意改变 - 视频检索/处理对我来说是一个全新的世界:)
What I'm having trouble understanding is the correct way to parse out the frames from the multi-part response - how the frames are retrieved is unimportant to me, if there's a better way to parse this kind of stream, I'm certainly open to change - video retrieval/processing is a whole new world for me :)
供参考:
MPEG 端点:
GET http://192.168.0.14/mjpeg.cgi
示例响应头:
Content-Type: multipart/x-mixed-replace;boundary=--video boundary--
示例响应正文:
Content-length: 41142
Date: 02-02-2019 12:43:19 AM
Content-type: image/jpeg
[payload]
--video boundary--
Content-length: 41220
Date: 02-02-2019 12:43:19 AM
Content-type: image/jpeg
[payload]
--video boundary--
我目前所拥有的:
var client = new HttpClient() {
BaseAddress = new Uri(streamUrl)
};
var stream = await client.GetStreamAsync(resource);
using (var streamReader = new StreamReader(stream)) {
while(true) {
await GetFrameStart(streamReader);
var frame = await ReadFrame(streamReader);
// Get byte[] from returned frame above and construct image
};
}
// ---
// Fast forward to the start of the Frame's bytes
static async Task GetFrameStart(StreamReader reader) {
string buffer = null;
while (buffer != string.Empty) {
buffer = await reader.ReadLineAsync();
}
}
// Read entire byte array until the video boundary is met
static async Task<String> ReadFrame(StreamReader reader) {
string result = string.Empty;
string line = null;
while(!isBoundary(line)) {
line = await reader.ReadLineAsync();
if (!isBoundary(line)) result += line;
};
return result;
}
上面似乎给了我每个帧的单独 [payload],但是如果我将字符串转换为字节并写入磁盘时,图像不是有效的 jpeg:
The above seems to be giving me back the individual [payload] of each frame, however if I when I convert the string to bytes and write to disk, the image is not a valid jpeg:
var bytes = Encoding.UTF8.GetBytes(frame);
using (var fs = new FileStream("test.jpeg", FileMode.Create, FileAccess.Write)) {
fs.Write(bytes, 0, bytes.Length);
}
推荐答案
您可以使用 Microsoft.AspNetCore.WebUtilities.MultipartReader
var boundary = GetBoundary(context.Request.ContentType);
var reader = new MultipartReader(boundary, context.Request.Body);
var section = await reader.ReadNextSectionAsync();
有关详细信息,请参阅此答案:
See this Answer for more info:
希望这会有所帮助.
这篇关于如何使用 .NET Core 解析 MPEG 视频流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!