如何从图像序列文件创建视频文件?
How to create video file from Images sequence file?
我有 400 张序列图像。
我想从他们创建视频文件 ( clip .. mpeg )
我下载了 'AForge.NET' 并尝试查看它是否可行 - 但我不知道有什么办法可以做到这一点。
我该怎么做?
Look up the documentation, find the VideoFileWriter Class, look at it's members, see the WriteVideoFrame method(s), read the line:“将新的视频帧写入当前打开的视频文件。”。答对了。到一半了。
如果您在阅读方法签名时无法连接点(WriteVideoFrame(Bitmap)
) or don't understand how to use the Open()
overloads or Close()
method (why wouldn't you, the last two are pretty common for File I/O) you can always 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”.)
如果它仍然不工作然后告诉我们发生了什么,确切错误消息等
我有 400 张序列图像。 我想从他们创建视频文件 ( clip .. mpeg )
我下载了 'AForge.NET' 并尝试查看它是否可行 - 但我不知道有什么办法可以做到这一点。
我该怎么做?
Look up the documentation, find the VideoFileWriter Class, look at it's members, see the WriteVideoFrame method(s), read the line:“将新的视频帧写入当前打开的视频文件。”。答对了。到一半了。
如果您在阅读方法签名时无法连接点(WriteVideoFrame(Bitmap)
) or don't understand how to use the Open()
overloads or Close()
method (why wouldn't you, the last two are pretty common for File I/O) you can always 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”.)
如果它仍然不工作然后告诉我们发生了什么,确切错误消息等