[英]How can I create a video file from separate images on a c# project
我想从c#中的列表创建一个视频文件,它可以是媒体播放器可以打开的任何格式。
我已经尝试过Aforge和Avi文件包装器,但遗憾的是它们只能在x86中工作,并且我有很多依赖项,因此我无法更改项目类型。 所以,它必须是x64。
我的所有位图都在一个列表中(大约50左右)public List tv_ImageData = new List();
我是c#的新手,并不了解我的方式。 我用谷歌搜索,找不到任何解决方案。 如果有人能指出我正确的方向(或图书馆),我将不胜感激。
(我觉得这样评论会更好,但我还没有这个名声。如果这是不好的做法,我很抱歉!)
因为你对AForge的唯一问题似乎是它是为x86编译的,所以我会提到看起来你可以自己为x64目标重新编译它。
https://github.com/andrewkirillov/AForge.NET
快速搜索发现此链接指向包含64位版本的AForge重新编译:
https://archive.codeplex.com/?p=aforgeffmpeg
我不知道这是否是最新的,所以我可能会建议你自己编译。
我希望有所帮助!
在与SharpAvi混合之后,我解决了我的问题。 我有一个名为的名单
List<ushort[]> tv_data = new List<ushort> tv_data();
其中包含帧作为原始数据(值在0-255范围内)。 我试图使用文档提供的示例,但它给了我一个好处avi(我猜它是因为SharpAvi期望DIB位图)。 所以我改了一下,从这里借了一点( 如何从字节数组创建位图? )top得到一个有效的解决方案。
这是我的功能:
using SharpAvi;
using SharpAvi.Output;
这可能不是最好的方法,但它有效。 希望有人会觉得它很有用。
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.