c# - 位图流到c#中的视频howto?
问题描述
我有一个通过 webrtc 库接收视频流的代码,它在其函数中将它们显示在 a 中PictureBox
,我的问题是..如何将该流从 传递PictureBox
到我计算机上的视频?
public unsafe void OnRenderRemote(byte* yuv, uint w, uint h)
{
lock (pictureBoxRemote)
{
if (0 == encoderRemote.EncodeI420toBGR24(yuv, w, h, ref bgrBuffremote, true))
{
if (remoteImg == null)
{
var bufHandle = GCHandle.Alloc(bgrBuffremote, GCHandleType.Pinned);
remoteImg = new Bitmap((int)w, (int)h, (int)w * 3, PixelFormat.Format24bppRgb, bufHandle.AddrOfPinnedObject());
}
}
}
try
{
Invoke(renderRemote, this);
}
catch // don't throw on form exit
{
}
}
此代码通过 webrtc 接收流并将其转换为图像,然后在PictureBox
调用此函数时显示.. 我的问题是:
如何保存remoteImg
图像数组或缓冲区,以便将其写入电脑上的视频文件?
尝试做这样的事情:
FileWriter.Open ("C:\\Users\\assa\\record.avi", (int) w, (int) h, (int) w * 3, VideoCodec.Default, 5000000);
FileWriter.WriteVideoFrame (remoteImg);
但只保存一个捕获而不是视频,有没有办法使用OnRenderRemote
功能(如上所述)保存流的图像,以便能够将它们保存在视频中?
OnRenderRemote
仅在PictureBox
每次调用时更新,但我不知道如何将该流保存在视频中。
谢谢。
解决方案
First: i do not know how the webrtc works exactly, but i can explain you how you must process the images to save them into a file.
Ok lets start: You currently have only full sized bitmaps of your own that are coming from the lib. That is just fine as long as you do not care about file size and you only want to show the "latest" frame. To store multiple frames into a file that we would call a "video" you need an encoder that processes those frames together.
Complicated things simple: An encoder takes 2 frames, call them Frame A and B and then compresses them in a way that only changes from Frame A to frame B are saved. This saves a lot of storage because in a video we only want to see "changes" aka movments from one frame to another. There are quite a lot of encoders out there but mostly you can see ffmpeg out there, its very popular and there are quite a lot c# wrappers for it so take a look.
Summery: to make 2-x images a "video" you have the process them with an encoder that processes the images in a format that can be played by a video player.
推荐阅读
- java - 评估异常:此命令处理器不支持方法“开始”
- r - 条件滞后函数
- android - Affectiva 是否支持外部 USB 摄像头进行情绪检测?
- sql - SQL Server:基于条件的备注
- swagger - 在导入时将 Swagger 参数名称转换为 Postman 变量
- javascript - 用 Jest 测试 Sagas 失败,主要对象收到的差异没有明确的解释
- forms - 图片。Symfony 4 中每种领域语言的 buildForm 中的 textfield 和 textarea
- django - 如何创建 django rest auth 社交登录?
- java - Change encryption algorithm in AWS Cognito access token JWT
- api - 监控 API 在 RTC 服务器上命中查询并在超时后终止它们