简体   繁体   中英

How to create an MP4 video from a series of stills in C#

I am trying to write a quick program to create time lapse videos from images but I cannot find any libraries or other ways to do this. I will input a series of images and the program will then merge them into a video with a frame rate and size (width/height not file size) I specify. The images are quite large most of the time (3456x2304 from a Canon DSLR) and I want it to be capable of outputting a video the same size as the images in specifically the MP4 format and nothing else.

Basically it is exactly like what QuickTime Pro does where you give it a set of stills and it creates a video from them at a frame rate you specify...and no I don't want to have to buy QuickTime though. I have the user interface and I just need a way to create a video from the images, what can I do/use (only things that are free)? Thank you.

You can do this with FFmpeg :

https://trac.ffmpeg.org/wiki/Create%20a%20video%20slideshow%20from%20images

Frame rates

Create a video (using the encoder libx264) from series of numerically sequential images such as img001.png, img002.png, img003.png, etc.

Important: All images in a series need to be the same size and format.

You can specify two frame rates:

  • The rate according to which the images are read, by setting -r before -i. The default for reading input is -r 25 which will be set if no -r is specified.
  • The output frame rate for the video stream by setting -r after -i, or by using the fps filter. If you want the input and output frame rates to be the same, then just declare an input -r and the output will inherit the same value.

By using a separate -r (frames per second) for the input and output you can control the duration at which each input is displayed and tell ffmpeg the frame rate you want for the output file. If the input -r is lower than the output -r then ffmpeg will duplicate frames to reach your desired output frame rate. If the input -r is higher than the output -r then ffmpeg will drop frames to reach your desired output frame rate.

In this example each image will have a duration of 5 seconds (the inverse of 1/5 frames per second). The video stream will have a frame rate of 30 fps by duplicating the frames accordingly:

ffmpeg -r 1/5 -i img%03d.png -c:v libx264 -r 30 -pix_fmt yuv420p out.mp4

Knowing the command line you want to use, you can kick it off in c# like this:

if (File.Exists(outputFile))
    File.Delete(outputFile);

var info = new ProcessStartInfo(exePath, yourParams);
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardOutput = true;

using (var proc = Process.Start(info))
    proc.WaitForExit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM