我有 400 张序列图像。 我想从它们创建视频文件(剪辑..mpeg)
我下载了“AForge.NET”,并尝试研究它是否可能 - 但我不知道有什么方法可以做到这一点。
我该怎么做?
最佳答案
Look up the documentation ,找到VideoFileWriter Class ,看看它是members ,参见WriteVideoFrame method(s) ,阅读line :“将新的视频帧写入当前打开的视频文件中。”。答对了。已经成功一半了。
如果您在阅读方法签名 ( WriteVideoFrame(Bitmap)
) 时无法将各个点联系起来,或者不明白如何使用 Open()
overloads或Close()
method (为什么不呢,最后两个对于文件 I/O 来说很常见)你总是可以 Google "VideoFileWriter WriteVideoFrame example" , find code ,从那里开始。该函数的核心内容是:
VideoFileWriter writer = new VideoFileWriter();
writer.Open("myfile.avi", width, height, 25, VideoCodec.MPEG4, 1000000);
// ... here you'll need to load your bitmaps
writer.WriteVideoFrame(image);
}
writer.Close();
所以像这样的东西可能应该有效:
using (VideoFileWriter writer = new VideoFileWriter())
{
writer.Open(@"d:\myfile.avi", 640, 480, 25, VideoCodec.MPEG4);
foreach (var file in Directory.GetFiles(@"d:\foo\bar", "*.jpg"))
{
writer.WriteVideoFrame(Bitmap.FromFile(file) as Bitmap);
}
writer.Close();
}
经过几分钟的摆弄,我得到了这样的结果:
var size = new Size(1600, 1200); // The desired size of the video
var fps = 25; // The desired frames-per-second
var codec = VideoCodec.MPEG4; // Which codec to use
var destinationfile = @"d:\myfile.avi"; // Output file
var srcdirectory = @"d:\foo\bar"; // Directory to scan for images
var pattern = "*.jpg"; // Files to look for in srcdirectory
var searchoption = SearchOption.TopDirectoryOnly; // Search Top Directory Only or all subdirectories (recursively)?
using (var writer = new VideoFileWriter()) // Initialize a VideoFileWriter
{
writer.Open(destinationfile, size.Width, size.Height, fps, codec); // Start output of video
foreach (var file in Directory.GetFiles(srcdirectory, pattern, searchoption)) // Iterate files
{
using (var original = (Bitmap)Image.FromFile(file)) // Read bitmap
using (var resized = new Bitmap(original, size)) // Resize if necessary
writer.WriteVideoFrame(resized); // Write frame
}
writer.Close(); // Close VideoFileWriter
} // Dispose VideoFileWriter
这会调整图像大小;序列中的所有图像不应相同。如果它们是,您只需更改即可跳过该步骤
using (var original = (Bitmap)Image.FromFile(file)) // Read bitmap
using (var resized = new Bitmap(original, size)) // Resize if necessary
writer.WriteVideoFrame(resized); // Write frame
至:
using (var mybitmap = (Bitmap)Image.FromFile(file)) // Read bitmap
writer.WriteVideoFrame(mybitmap); // Write frame
还要确保添加正确的 using
语句;对于上述示例,您至少需要以下内容:
using AForge.Video.FFMPEG;
using System.Drawing;
using System.IO;
此外,您还需要引用 DLL,如 here 中所述。 :
... you should have created a project, added a reference to the AForge.Video.FFMPEG library, set the target platform to x86 and the target framework version to 3.5 or lower now. If so, it can go on.
... we need a few more dlls from the AForge archive. You can find these in the folder “Externals\ffmpeg” inside the AForge archive. All files in this folder have to be copied to the output folder of your Visual Studio project. (After we changed the target architecture this now should be “YourProjectFolder\bin\x86\Debug”.)
如果它仍然不起作用,请告诉我们发生了什么,确切错误消息等。
关于c# - 如何从图像序列文件创建视频文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36744334/