简体   繁体   English

如何用C#中的一系列静止图像创建MP4视频

[英]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. 图像大部分时间都是很大的(佳能单反相机为3456x2304),我希望它能够输出与图像大小相同的视频(特别是MP4格式),而别无其他。

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. 基本上,这与QuickTime Pro所做的工作完全相同,在其中您可以给它们提供一组静止图像,并以它们指定的帧速率从静止图像中创建视频...而且我不希望购买QuickTime。 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 : 您可以使用FFmpeg做到这一点:

https://trac.ffmpeg.org/wiki/Create%20a%20video%20slideshow%20from%20images 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. 根据一系列数字序列图像(例如img001.png,img002.png,img003.png等)创建视频(使用编码器libx264)。

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. 通过在-i之前设置-r来读取图像的速率。 The default for reading input is -r 25 which will be set if no -r is specified. 读取输入的默认值为-r 25,如果未指定-r,则将设置该值。
  • The output frame rate for the video stream by setting -r after -i, or by using the fps filter. 通过在-i之后设置-r或使用fps过滤器,可以输出视频流的帧速率。 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. 如果您希望输入和输出帧速率相同,则只需声明输入-r,输出将继承相同的值。

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. 通过对输入和输出使用单独的-r(每秒帧数),您可以控制显示每个输入的持续时间,并告诉ffmpeg输出文件所需的帧速率。 If the input -r is lower than the output -r then ffmpeg will duplicate frames to reach your desired output frame rate. 如果输入-r低于输出-r,则ffmpeg将复制帧以达到所需的输出帧速率。 If the input -r is higher than the output -r then ffmpeg will drop frames to reach your desired output frame rate. 如果输入-r高于输出-r,则ffmpeg将丢弃帧以达到所需的输出帧速率。

In this example each image will have a duration of 5 seconds (the inverse of 1/5 frames per second). 在此示例中,每个图像的持续时间为5秒(每秒1/5帧的倒数)。 The video stream will have a frame rate of 30 fps by duplicating the frames accordingly: 通过相应地复制帧,视频流将具有30 fps的帧速率:

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: 知道要使用的命令行后,您可以像这样在c#中启动它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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