简体   繁体   English

在单个命令中使用 ffmpeg 从图像生成和连接视频

[英]Generate and concatenate videos from images with ffmpeg in single command

My goal is to generate a video from images.我的目标是从图像中生成视频。 Let's say I have 2 images 1.png and 2.png .假设我有 2 张图片1.png2.png

I can do我可以

ffmpeg -loop 1 1.png -t 3 1.mp4
ffmpeg -loop 1 2.png -t 5 2.mp4

to create a 3 second video from the first image and 5 second video from the second image.从第一张图像创建 3 秒视频,从第二张图像创建 5 秒视频。

Then, I merge the two videos using然后,我使用合并两个视频

ffmpeg -i 1.mp4 -I 2.mp4 -filter_complex "concat" final.mp4 

to create my final 8 second video.创建我最后的 8 秒视频。

This process seems extremely inefficient, and I feel I do not have to use all this processing power+disk reading/writing to create 2 intermediary video files when I only want the one final video.这个过程似乎效率极低,当我只想要一个最终视频时,我觉得我不必使用所有这些处理能力+磁盘读/写来创建2个中间视频文件。

Is there a way to execute this entire process in one ffmpeg command (efficiently)?有没有办法在一个 ffmpeg 命令中(有效地)执行整个过程?

The most versatile way is to use the concat filter .最通用的方法是使用concat过滤器

Perhaps an easy way out assuming that all the durations are nice numbers (eg, integer seconds like you have in your example), then you can acctually use concat muxer and repeat files to occupy the desired time span.假设所有持续时间都是不错的数字(例如,integer 秒,就像您在示例中那样),也许一个简单的方法,然后您可以实际使用concat muxer重复文件来占用所需的时间跨度。

Using your example,使用您的示例,

concat.txt (in the same directory as the images) concat.txt (与图像在同一目录中)

ffconcat version 1.0
# repeated 3 times for 3 seconds
file capture_1.jpg
file capture_1.jpg
file capture_1.jpg
# repeated 5 times for 5 seconds
file capture_2.jpg
file capture_2.jpg
file capture_2.jpg
file capture_2.jpg
file capture_2.jpg

FFmpeg command with input rate of 1 frame/second:输入速率为 1 帧/秒的 FFmpeg 命令:

ffmpeg -f concat -safe 0 -r 1 -i concat.txt 

Obviously, this will get pretty ugly quickly but it does work.显然,这会很快变得非常难看,但它确实有效。

If you are to script generating the concat file, you might as well go the filter route imo.如果您要编写生成 concat 文件的脚本,您还可以 go 过滤器路由 imo。

If your images are smoothly and continuously numbered ie (1.png, 2.png, 3.png) not (1.png 02.png 4.png) then run如果您的图像平滑且连续编号,即 (1.png, 2.png, 3.png) 而不是 (1.png 02.png 4.png) 然后运行

ffmpeg -i %d.png -vf "setpts='eq(N\,0)*0+eq(N\,1)*3+eq(N\,2)*8',tpad=stop_mode=clone:stop_duration=2" out.mp4

In setpts, each image is assigned a timestamp that sets when it starts showing, so the image timestamp is equal to the cumulative duration of all earlier images.在 setpts 中,每个图像都分配了一个时间戳,该时间戳在它开始显示时设置,因此图像时间戳等于所有早期图像的累积持续时间。 The tpad sets the duration of the final image. tpad 设置最终图像的持续时间。 Add -r X just before out.mp4 to set a fps of X.在 out.mp4 之前添加-r X以设置 X 的 fps。

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

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