C# 位图到 AVI/WMV 的压缩

标签 c# bitmap ffmpeg avi wmv

前奏:

我将以此作为开头,我在工作的业余时间一直在学习 C#,并且我已经盯着代码整整两天试图解决这个问题。我正在开发一些与通过 USB 连接到标准台式 PC 的可视化器一起使用的软件,该软件检测捕获设备并使用新帧事件将帧加载到位图中,然后在“图片框”中显示为实时视频流。存在的问题是试图结合记录流和保存到文件的能力,最好是 WMV 或压缩的 AVI。

已尝试:

我已经考虑并研究了以下内容:

SharpAVI - 似乎无法正确压缩或保存帧,因为它似乎主要查看现有的 AVI 文件。

AForge.Video.VFW - 可以创建 AVI 文件,但由于对将使用该软件的个人的用户区域的限制,它太大而无法使用。

AForge.Video.FFMPEG - 再次考虑到使用此软件的用户,我不能将非托管 DLL 与可执行文件放在输出文件夹中,不幸的是,无法使用 Costura Fody 正确编译此特定 DLL。

AVIFile 库包装器(来自代码项目) - 再次似乎无法从新帧事件的位图中正确压缩流。

DirectShow - 似乎使用 C++,不幸的是目前超出了我的技能水平。

相关代码片段:

当前引用:

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Resources;
using System.Drawing.Imaging;
using System.IO;
//Aforge Video DLL's
using AForge.Video;
using AForge.Video.VFW;
using AForge.Video.DirectShow;
//Aforge Image DLL's
using AForge.Imaging;
using AForge.Imaging.Formats;
using AForge.Imaging.Filters;
//AviLibrary
using AviFile;

全局变量:
    #region Global Variables

    private FilterInfoCollection CaptureDevice; // list of available devices
    private VideoCaptureDevice videoSource;
    public System.Drawing.Image CapturedImage;

    bool toggleMic = false;

    bool toggleRec = false;
    //aforge
    AVIWriter aviWriter;
    Bitmap image;


    #endregion

显示流的代码
    #region Displays the Stream

    void videoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        picBoxStream.SizeMode = PictureBoxSizeMode.Zoom;
        picBoxStream.Image = (Bitmap)eventArgs.Frame.Clone();// clones the bitmap


        if (toggleRec == true)
        {
            image = (Bitmap)eventArgs.Frame.Clone();
            aviWriter.AddFrame(image);
        }
    }

    #endregion

录制流的当前代码
    #region Record Button

    private void btnRecord_Click(object sender, EventArgs e)
    {
        if (toggleRec == false)
        {
            saveAVI = new SaveFileDialog();
            saveAVI.Filter = "AVI Files (*.avi)|*.avi";

            if (saveAVI.ShowDialog() == DialogResult.OK)
            {
                aviWriter = new AVIWriter();
                aviWriter.Open(saveAVI.FileName, 1280, 720);

                toggleRec = true;
                Label lblRec = new Label();
            }

        }
        else if (toggleRec == true)
        {
            aviWriter.Close();
            toggleRec = false;
        }
    }

    #endregion

如果上面的代码看起来不太正确,我很抱歉,我一直在交换、更改和重新编码这三个部分以找到一个有效的组合。这意味着它相当不整洁,但在我让代码工作之前,我没有看到清理它的意义。话虽这么说,你能提供的任何帮助都会得到很大的返回,即使这是我想做的事情无法完成的情况。

先感谢您。

编辑:2019:

自从我发布这个已经有一段时间了,它仍然在这里和那里引起了奇怪的兴趣。当我发布这篇文章时,我正在自学编码,我有一个奇怪的怪癖,如果我可以避免的话,我不喜欢使用 3rd 方库,我想做自己的工作,从那时起我学到了很多东西其中之一是开源世界是巨大的、令人印象深刻的和善良的。因此,如果有一个 3rd 方库可以满足您的需求,只需使用它,它就会为您节省时间。

最佳答案

和我刚刚解决的问题一样:)这是我的代码,如何使用 AviFile 库 dll 制作 avi 文件:)

    private void MakeAvi(List<Bitmap> maps)
    {
        AviManager mana = new AviManager("local.avi", false);

        //false means do not show the diag of the Compression 
        //21 means the fps of the video
        //maplist[0] cover of the video  the maplist is the val you should insert 
        VideoStream avistream = mana.AddVideoStream(false, 21, maplist[0]);

        for (int i = 1; i < maps.Count; i++)
        {
            avistream.AddFrame(maplist[i]);
        }

        mana.Close();
        MessageBox.Show("AddOk");
    }

关于C# 位图到 AVI/WMV 的压缩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29826316/

相关文章:

ffmpeg - 为什么 FFMPEG 错误地将 .M4A 猜测为 WAV pcm_s16le 文件?

c# - 适用于 Windows 的简单 XNA Gui 框架

c++ - 如何从 HBITMAP 获取 RGBQUAD?

android - 快速将图像 url 转换为位图

Android Glide : How to download bitmap at specific size?

linux - 使用 pkg-config 找不到错误 : dav1d >= 0. 2.1

python - 使用带有 ffmpeg 的 Popen 时的空行输出

c# - 我可以让我的程序集引用另一个程序集的任何版本吗?

c# - 通过机器人框架的电报聊天键盘

c# - 比较时为什么要将字符串转换为大写?