如何从带有时间戳的图像列表中渲染视频?

如何从带有时间戳的图像列表中渲染视频?

问题描述:

我有一个充满图像的目录,该图像遵循模式<timestamp>.png,其中<timestamp>代表自第一个图像以来经过的毫秒数. input.txt包含有趣图像的列表:

I have a directory full of images following the pattern <timestamp>.png, where <timestamp> represents milliseconds elapsed since the first image. input.txt contains a list of the interesting images:

file '0.png'
file '97.png'
file '178.png'
file '242.png'
file '296.png'
file '363.png'
...

我正在使用ffmpeg将这些图像连接成视频:

I am using ffmpeg to concatenate these images into a video:

ffmpeg -r 15 -f concat -i input.txt output.webm

我如何告诉ffmpeg将每一帧及时放置在其实际位置上,而不是使用恒定的帧速率?

How do I tell ffmpeg to place each frame at its actual position in time instead of using a constant framerate?

遵循LordNeckbeard的建议,为ffmpeg的时间持续时间语法input.txt看起来像这样:

Following LordNeckbeard's suggestion to supply the duration directive to ffmpeg's concat demuxer using the time duration syntax, input.txt looks like this:

file '0.png'
duration 0.097
file '97.png'
duration 0.081
file '178.png'
duration 0.064
file '242.png'
duration 0.054
file '296.png'
duration 0.067
file '363.png'

现在ffmpeg处理可变的帧率.

Now ffmpeg handles the variable framerate.

ffmpeg -f concat -i input.txt output.webm

这是构造input.txt的C#代码段:

Here is the C# snippet that constructs input.txt:

Frame previousFrame = null;

foreach (Frame frame in frames)
{
    if (previousFrame != null)
    {
        TimeSpan diff = frame.ElapsedPosition - previousFrame.ElapsedPosition;
        writer.WriteLine("duration {0}", diff.TotalSeconds);
    }

    writer.WriteLine("file '{0}'", frame.FullName);
    previousFrame = frame;
}