:: AForge.NET Framework :: Articles :: Forums ::

Create video file from images

Forum to discuss AForge.NET Framework, its features, API, how-tos, etc.

Create video file from images

Postby Explomat » Thu Sep 15, 2016 2:20 pm

Hi everyone!Sorry, but My English is not very good ))). I'm trying to create video from images. I have xml file that represent the structure of video. For example:

Code: Select all
<root timestamp="1473329484" height="500" width="700">
     <frame type="image" name="20168811592965.png" x="337.0" y="661.0"  time="2137" />
     <frame type="image" name="20168811592989.png" x="5.0" y="38.0"  time="2152"/>
     <frame type="image" name="201688115937193.png" x="5.0" y="352.0" time="10264"/>

And I have a list of pictures, that match the attribute "name" of <frame> in xml file.
Every frame has a time in milliseconds.
Here some code, where i convert images to video.
Code: Select all
private void CreateMovie()
            int width = this.model.Width;
            int height = this.model.Height;
            int frameRate = this.frameRate;

            // create instance of video writer
            using (var vFWriter = new VideoFileWriter())
                // create new video file
                vFWriter.Open("nameOfMyVideoFile.avi", width, height, frameRate, VideoCodec.Raw);
                var imagePaths = this.GetFiles(this.filePath);
                List<Frame> frames = this.model.Frames.Where(f => f.Type == "image").ToList<Frame>();
                Bitmap reduced = new Bitmap(width, height);

                foreach (Frame fr in frames)
                    string imagePath = imagePaths.Where(img => Path.GetFileName(img) == fr.Name).FirstOrDefault<string>();
                    if (imagePath != null)
                        string imageName = Path.GetFileName(imagePath);
                        Bitmap originalBitmap = new Bitmap(imagePath);
                        Bitmap bmpReduced = this.ReduceBitmap(originalBitmap, reduced, (int)fr.X, (int)fr.Height);
                        vFWriter.WriteVideoFrame(bmpReduced, TimeSpan.FromMilliseconds(fr.Time));


The problem is, the length of the video should be more then 1 hour, I'm trying in this code "vFWriter.WriteVideoFrame(bmpReduced, TimeSpan.FromMilliseconds(fr.Time));" and rather this "TimeSpan.FromMilliseconds(fr.Time))" to point from where milliseconds play frame in video, but its not working. Working only FrameRate, if i install FrameRate big value(60), so video length is little(about 10 seconds), if I install little value (for example "1") so video length is bigger(about 150 seconds (153 frames)). And the second parametr "timestamp" of "WriteVideoFrame" not working or I don't know how to use it. I need to use "time" parametr from frame in xml file.Simply put, the video length is adjustable only FrameRate, nothing more. Please help me.
Posts: 1
Joined: Thu Sep 15, 2016 1:52 pm

Return to AForge.NET Framework