首页 > 解决方案 > 位图流到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每次调用时更新,但我不知道如何将该流保存在视频中。

谢谢。

标签: c#videobitmapstream

解决方案


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.


推荐阅读