FFMPEG – Generate video from images

1-

First, we need to download FFMPEG executables, (I have downloaded for Windows)

2-

After download, extract zip to a folder; (I have extracted under C:\FFMPEG and took these folder and files only)

And I have these images,

Let’s write the code!

First import this Nuget package like below,

Then we will define our paths!

static void Main(string[] args)
{
     var inputPath = @"C:\Images";
     var outputPath = @"C:\Output\" + DateTime.Now.Ticks.ToString() + ".mp4";
     var ffmpegPath = @"C:\FFMPEG\bin";
}

InputPathis where images are stored

OutputPath is where mp4 file will be saved.

FfmpegPath is where we extracted the executable files.

Now let’s use them,

static void Main(string[] args)
{
     var inputPath = @"C:\Images";
     var outputPath = @"C:\Output\" + DateTime.Now.Ticks.ToString() + ".mp4";
     var ffmpegPath = @"C:\FFMPEG\bin";

     FFmpeg.SetExecutablesPath(ffmpegPath);
     string[] files = Directory.EnumerateFiles(inputPath).ToArray();
}

We have set ffmpeg executable path to library by usingSetExecutablesPath()

Then we have took all the files under the inputPath

Let’s convert it,

static void Main(string[] args)
{
     var inputPath = @"C:\Images";
     var outputPath = @"C:\Output\" + DateTime.Now.Ticks.ToString() + ".mp4";
     var ffmpegPath = @"C:\FFMPEG\bin";

     FFmpeg.SetExecutablesPath(ffmpegPath);
     string[] files = Directory.EnumerateFiles(inputs).ToArray();
     var conv = new Conversion();
     Task<IConversionResult> result = conv.SetInputFrameRate(0.5)
                                          .BuildVideoFromImages(files)
                                          .SetFrameRate(30)
                                          .SetOutput(outputPath).Start();
     result.Wait(); // wait until operation done without App Exits.
}

Result, (Opened with Chrome Browser)

Tags:, ,