0

I would like to create a video file from a list in c# which can be in any format that media player can open.

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. So, it has to be x64.

All my Bitmaps are in a list (which is around 50 or so) public List tv_ImageData = new List();

I am new to c# and don't know my way around much. I have googled and could find no solution. I'd be grateful if someone can point me to the right direction (or library).

Karter
  • 13
  • 2

2 Answers2

0

(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.

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

A quick search found this link to a recompile of AForge that includes a 64-bit version:

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!

persona15
  • 53
  • 1
  • 7
  • Thank you, I have checked the link for the x64 and compiled for an x64 target. Still no good. I have found another library called SharpAvi (https://github.com/baSSiLL/SharpAvi) which seems to do what I want. I'll keep here posted. – Karter Jun 08 '19 at 07:34
0

After some mingling with SharpAvi I solved my problem. 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). 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). So I changed it a bit and borrowed a bit from here (How to create bitmap from byte array?) top get a working solution.

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;
}
Karter
  • 13
  • 2