使用 ffmpeg 剪切视频的多个部分 [英] Cut multiple parts of a video with ffmpeg
问题描述
在我尝试做的事情中,我需要删除视频文件的某些部分,然后从中创建一个新的部分.
In what I'm trying to do, I need to remove some parts of a video file, and create a new one from that.
例如,来自这样的视频文件:
For example, from a video file like this:
===================
我剪了两刀
======||=====||||==
并生成一个新的较小的视频文件:
and generate a new smaller video file:
=============
当我说 2 时,我的意思是任意数量的单独剪辑,具体取决于视频文件.
And when I say 2, I mean an arbitrary number of separate cuts, depending on the video file.
如果我只想切割一部分,我会这样做:
If I wanted to cut just one part I would do:
ffmpeg -i video.mp4 -ss 00:00:03.500 -to 00:00:08.500 -async 1 cut.mp4 -y
哪个完美.我也许可以这样做很多次,然后将所有剪辑合并在一起……但这对于较大的视频文件来说效率很低.
Which works perfectly. I could perhaps do this many times and then join all the cuts together... But this is very inefficient for larger video files.
为了进行两次切割,我正在查看 filter_complex.我一直在努力让它正确几个小时,但我似乎无法让它正常工作:/
To make two cuts I was looking at filter_complex. I've been trying to get it right for hours but I can't seem to get this working :/
如果我做这样的事情,我会得到一个没有音频的视频:
If I do something like this I get a video with no audio:
command='ffmpeg -i video.mp4
-filter_complex "[0]trim=start_frame=10:end_frame=20[v0];
[0]trim=start_frame=30:end_frame=40[v1];
[v0][v1]concat=n=2[v5]"
-map [v5] -async 1 output.mp4'
如果我尝试这样做,事情就会变得一团糟:
If I try to do this, things get all messed up:
ffmpeg -y -i video.mp4
-filter_complex "[0:v]trim=start_frame=10:end_frame=20[v0];
[0:a]atrim=start=10:end=20[a0];
[0:v]trim=start_frame=30:end_frame=40[v1];
[0:a]atrim=start=30:end=40[a1];
[v0][a0][v1][a1]concat=2:v=1:a=1[v5][a]"
-map [v5] -map [a] -async 1 output.mp4
我什至尝试在 Python 中使用 ffmpeg-python https://github.com/kkroening/ffmpeg-python 但我也无法使用音频.
I even trying to to this in Python with ffmpeg-python https://github.com/kkroening/ffmpeg-python but I also can't get audio to work.
有人可以帮我解决这个问题吗?非常感谢!!
Can anyone give me some help on this? Thank you very much!!
推荐答案
选择过滤器更适合这个.
The select filter is better for this.
ffmpeg -i video
-vf "select='between(t,4,6.5)+between(t,17,26)+between(t,74,91)',
setpts=N/FRAME_RATE/TB"
-af "aselect='between(t,4,6.5)+between(t,17,26)+between(t,74,91)',
asetpts=N/SR/TB" out.mp4
select 及其对应的过滤器分别应用于视频和音频.选择的段是时间 4 到 6.5 秒、17 到 26 秒,最后是 74 到 91 秒.时间戳与 setpts 及其对应过滤器连续..
select and its counterpart filter is applied to the video and audio respectively. Segments selected are times 4 to 6.5 seconds, 17 to 26 seconds and finally 74 to 91 seconds. The timestamps are made continuous with the setpts and its counterpart filter..
这篇关于使用 ffmpeg 剪切视频的多个部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!