简体   繁体   English

如何从ac #project上的单独图像创建视频文件

[英]How can I create a video file from separate images on a c# project

I would like to create a video file from a list in c# which can be in any format that media player can open. 我想从c#中的列表创建一个视频文件,它可以是媒体播放器可以打开的任何格式。

I have tried Aforge and Avi file wrapper, but unfortunately they only work in x86 and I have quite a lot of dependencies so that I can not change the project type. 我已经尝试过Aforge和Avi文件包装器,但遗憾的是它们只能在x86中工作,并且我有很多依赖项,因此我无法更改项目类型。 So, it has to be x64. 所以,它必须是x64。

All my Bitmaps are in a list (which is around 50 or so) public List tv_ImageData = new List(); 我的所有位图都在一个列表中(大约50左右)public List tv_ImageData = new List();

I am new to c# and don't know my way around much. 我是c#的新手,并不了解我的方式。 I have googled and could find no solution. 我用谷歌搜索,找不到任何解决方案。 I'd be grateful if someone can point me to the right direction (or library). 如果有人能指出我正确的方向(或图书馆),我将不胜感激。

(I feel like this would be better as a comment, but I don't have the reputation for that yet. I'm sorry if this is bad practice!) (我觉得这样评论会更好,但我还没有这个名声。如果这是不好的做法,我很抱歉!)

Since your only problem with AForge seems to be that it is compiled for x86, I'll mention that it looks like you can recompile it yourself for an x64 target. 因为你对AForge的唯一问题似乎是它是为x86编译的,所以我会提到看起来你可以自己为x64目标重新编译它。

https://github.com/andrewkirillov/AForge.NET https://github.com/andrewkirillov/AForge.NET

A quick search found this link to a recompile of AForge that includes a 64-bit version: 快速搜索发现此链接指向包含64位版本的AForge重新编译:

https://archive.codeplex.com/?p=aforgeffmpeg https://archive.codeplex.com/?p=aforgeffmpeg

I wouldn't know if that's up to date or not, so I might recommend compiling it yourself. 我不知道这是否是最新的,所以我可能会建议你自己编译。

I hope that helps! 我希望有所帮助!

After some mingling with SharpAvi I solved my problem. 在与SharpAvi混合之后,我解决了我的问题。 I had a List called 我有一个名为的名单

List<ushort[]> tv_data = new List<ushort> tv_data();

which contained the frames as raw data (values in 0-255 range). 其中包含帧作为原始数据(值在0-255范围内)。 I tried to use the example supplied by the documentation but it gave me an upside avi(I guess its because SharpAvi expects DIB bitmaps). 我试图使用文档提供的示例,但它给了我一个好处avi(我猜它是因为SharpAvi期望DIB位图)。 So I changed it a bit and borrowed a bit from here ( How to create bitmap from byte array? ) top get a working solution. 所以我改了一下,从这里借了一点( 如何从字节数组创建位图? )top得到一个有效的解决方案。

Here is my function: 这是我的功能:

using SharpAvi;
using SharpAvi.Output;

This may not be the best way to do it but it works. 这可能不是最好的方法,但它有效。 Hope someone will find it useful. 希望有人会觉得它很有用。

private void SaveAsVideo(object sender, RoutedEventArgs e)
{
    if (loadedFileName != "")
    {
        try
        {
            var writer = new AviWriter(string.Format("{0}.avi", fullPath))
            {
                FramesPerSecond = (decimal)VI.FrameRate,
                EmitIndex1 = true
            };
            var stream = writer.AddVideoStream();
            stream.Width = (int)VI.VideoWidth;
            stream.Height = (int)VI.VideoHeight;
            stream.Codec = KnownFourCCs.Codecs.Uncompressed;
            stream.BitsPerPixel = BitsPerPixel.Bpp8;

            var frameData = new byte[stream.Width * stream.Height];
            int frameNo = 0;
            foreach (ushort[] data in tv_Data)
            {
                byte[] byteData = tv_Data.ElementAt(frameNo);
                byte[] newbytes = PadLines(byteData, stream.Height, stream.Width);
                stream.WriteFrame(true, newbytes, 0, frameData.Length);
                frameNo++;
            }
            writer.Close();
            MessageBox.Show("Video file saved.");
        }
        catch (Exception ex)
        {
            MessageBox.Show(string.Format("Failed to save video. \n {0}", ex.Message));
        }
    }
}
static byte[] PadLines(byte[] bytes, int rows, int columns)
{
    int currentStride = columns;
    int newStride = columns;
    byte[] newBytes = new byte[newStride * rows];
    byte[] tempBytes = new byte[newStride];

    for (int i = 0; i < rows; i++)
    {
        Buffer.BlockCopy(bytes, currentStride * i, tempBytes, 0, currentStride);
        Array.Reverse(tempBytes);
        Buffer.BlockCopy(tempBytes, 0, newBytes, newStride * i, currentStride);
    }
    Array.Reverse(newBytes);
    return newBytes;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM