好吧,这个答案来得有点晚,但是由于我最近注意到我最初的问题有一些活动(以及没有提供有效解决方案的事实),我想告诉你最终对我有用的东西。
我将把我的答案分成三个部分:
背景
(此部分对解决方案不重要)
我最初的问题是我有很多图像(即大量),这些图像以字节数组的形式单独存储在数据库中。 我想用所有这些图像制作一个视频序列。
我的设备设置类似于这张总图:
这些图像描绘了在不同州种植的番茄植物。所有图像均在白天每 1 分钟拍摄一次。
/*pseudo code for taking and storing images*/
while (true)
{
if (daylight)
{
//get an image from the camera
//store the image as byte array to db
}
//wait 1 min
}
我有一个非常简单的 db 用于存储图像,其中只有一个表(表 ImageSet):
问题
我已经阅读了很多关于 ffmpeg 的文章(请参阅我的原始问题),但我找不到任何关于如何从图像集合到视频的文章。
解决方案
终于,我找到了一个可行的解决方案!
它的主要部分来自开源项目AForge.NET。简而言之,你可以说AForge.NET is a computer vision and artificial intelligence library in C#。
(如果您想要该框架的副本,只需从http://www.aforgenet.com/ 获取)
在 AForge.NET 中,有一个 VideoFileWriter 类(一个借助 ffmpeg 编写视频文件的类)。这几乎完成了所有工作。 (还有一个很好的例子here)
这是我用来从我的图像数据库中获取图像数据并将其转换为视频的最后一个类(简化):
public class MovieMaker
{
public void Start()
{
var startDate = DateTime.Parse("12 Mar 2012");
var endDate = DateTime.Parse("13 Aug 2012");
CreateMovie(startDate, endDate);
}
/*THIS CODE BLOCK IS COPIED*/
public Bitmap ToBitmap(byte[] byteArrayIn)
{
var ms = new System.IO.MemoryStream(byteArrayIn);
var returnImage = System.Drawing.Image.FromStream(ms);
var bitmap = new System.Drawing.Bitmap(returnImage);
return bitmap;
}
public Bitmap ReduceBitmap(Bitmap original, int reducedWidth, int reducedHeight)
{
var reduced = new Bitmap(reducedWidth, reducedHeight);
using (var dc = Graphics.FromImage(reduced))
{
// you might want to change properties like
dc.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
dc.DrawImage(original, new Rectangle(0, 0, reducedWidth, reducedHeight), new Rectangle(0, 0, original.Width, original.Height), GraphicsUnit.Pixel);
}
return reduced;
}
/*END OF COPIED CODE BLOCK*/
private void CreateMovie(DateTime startDate, DateTime endDate)
{
int width = 320;
int height = 240;
var framRate = 200;
using (var container = new ImageEntitiesContainer())
{
//a LINQ-query for getting the desired images
var query = from d in container.ImageSet
where d.Date >= startDate && d.Date <= endDate
select d;
// create instance of video writer
using (var vFWriter = new VideoFileWriter())
{
// create new video file
vFWriter.Open("nameOfMyVideoFile.avi", width, height, framRate, VideoCodec.Raw);
var imageEntities = query.ToList();
//loop throught all images in the collection
foreach (var imageEntity in imageEntities)
{
//what's the current image data?
var imageByteArray = imageEntity.Data;
var bmp = ToBitmap(imageByteArray);
var bmpReduced = ReduceBitmap(bmp, width, height);
vFWriter.WriteVideoFrame(bmpReduced);
}
vFWriter.Close();
}
}
}
}
2013 年 11 月 29 日更新(如何)(希望这是您对 @Kiquenet 的要求?)
- 从downloads page 下载 AForge.NET Framework(下载完整的 ZIP 存档,您会在
AForge.NET Framework-2.2.5\Samples folder...中找到许多有趣的 Visual Studio 解决方案以及视频等项目)
- 命名空间:
AForge.Video.FFMPEG(来自documentation)
- 程序集:
AForge.Video.FFMPEG(在AForge.Video.FFMPEG.dll)(来自documentation)(您可以在AForge.Video.FFMPEG.dll 文件夹中找到这个AForge.Video.FFMPEG.dll)
如果您想创建自己的解决方案,请确保在您的项目中引用了AForge.Video.FFMPEG.dll。然后应该很容易使用VideoFileWriter 类。如果您关注link 上课,您会发现一个非常好的(和简单的示例)。在代码中,他们在 for-loop 中为 VideoFileWriter 提供 Bitmap image