How to create video file from Images sequence file? [closed]

一曲冷凌霜 提交于 2019-11-30 10:58:57

Look up the documentation, find the VideoFileWriter Class, look at it's members, see the WriteVideoFrame method(s), read the line: "Write new video frame into currently opened video file.". Bingo. Half way there.

If you can't connect the dots when reading the methods' signature (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, go from there. The meat of the function there is:

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();

So something like this should probably work:

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();
}

Which, with a few minutes of fiddling around, gave me something like this:

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

This resizes images; should not all images in the sequence be the same. If they are you can skip that step simply by changing

using (var original = (Bitmap)Image.FromFile(file))     // Read bitmap
using (var resized = new Bitmap(original, size))        // Resize if necessary
    writer.WriteVideoFrame(resized);                    // Write frame

to:

using (var mybitmap = (Bitmap)Image.FromFile(file))     // Read bitmap
    writer.WriteVideoFrame(mybitmap);                   // Write frame

Also make sure you add the correct using statements; you will, at a minimum, need the following for above examples:

using AForge.Video.FFMPEG;
using System.Drawing;
using System.IO;

Also you'll need to reference the DLL's as described 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”.)

If it still doesn't work then tell us what happens, exact error messages etc.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!