Published on

How to create a video from multiple images using C#

Authors
  • avatar
    Name
    Siddharth Singh
    Twitter

Introduction

I am working on a fun project for video generation with AI Agents. For that, there was a requirement to create a tool to convert group of image files into a video file. Just sharing the code and the process so that it helps someone in future.

Setup

There are few libraries for this in .NET framework. The popular apporach is to use the FFMPEG libraries and excute it in a process. There are many wrapper available though.

I am using the FFMediaToolKit package.

FFMediaToolkit is a cross-platform video decoder/encoder library for .NET that uses FFmpeg native libraries. It supports video frames extraction, reading stream metadata and creating videos from bitmaps in any format supported by FFmpeg.

Here is how I am using it.

  1. Download the FFMpeg libraries from github. I used this build. Note that using the latest builds did not work for me(4.5.1 ) so I had to look for an older build.

  2. Set the FFMPEG path in the code to the bin folder.

FFmpegLoader.FFmpegPath = @"C:\ffmpeg\bin";
  1. Add the FFMediaToolit package from Nuget to your project.
PM> Install-Package FFMediaToolkit
  1. Setup the test images and output path. Update the paths in the code.
using FFMediaToolkit;
using FFMediaToolkit.Encoding;
using FFMediaToolkit.Graphics;
using System.Drawing;
using System.Drawing.Imaging;

namespace ImageToVideo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            FFmpegLoader.FFmpegPath = @"C:\ffmpeg\bin";

            var settings = new VideoEncoderSettings(width: 960, height: 544, framerate: 30, codec: VideoCodec.H264);
            settings.EncoderPreset = EncoderPreset.Fast;
            settings.CRF = 17;
            var file = MediaBuilder.CreateContainer(@"C:\out-video\out.mp4")
                .WithVideo(settings)
                .Create();
            var files = Directory.GetFiles(@"C:\Input\");
            foreach (var inputFile in files)
            {
                var binInputFile = File.ReadAllBytes(inputFile);
                var memInput = new MemoryStream(binInputFile);
                var bitmap = Bitmap.FromStream(memInput) as Bitmap;
                var rect = new System.Drawing.Rectangle(System.Drawing.Point.Empty, bitmap.Size);
                var bitLock = bitmap.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
                var bitmapData = ImageData.FromPointer(bitLock.Scan0, ImagePixelFormat.Bgr24, bitmap.Size);
                for (int i = 0; i < 25; i++) { 
                    file.Video.AddFrame(bitmapData); // Encode the frame
                }
                bitmap.UnlockBits(bitLock);
            }

            file.Dispose();
        }
    }
}

Errors

If the FFMPEG dll version is not compatible with the package, or the path is incorrect. You will get below error.

System.DllNotFoundException: 'Cannot load FFmpeg libraries from C:\ffmpeg\ directory.
Required FFmpeg version: 6.x (shared build)
Make sure the "Build"Prefer 32-bit" option in the project settings is turned off.
For more information please see https://github.com/radek-k/FFMediaToolkit#setup'

Possible fixes would be to correct the FFMPEG path or check with another version of the dlls from the builds page.

Thanks for reading!