Cut multiple parts of a video with ffmpeg

2024/11/18 5:44:25

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:

===================

I make two cuts

======||=====||||==

and generate a new smaller video file:

=============

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.

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

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!!

Answer

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 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..

https://en.xdnf.cn/q/26532.html

Related Q&A

Can I fake/mock the type of my mock objects in python unittests

In my python code I check the type of one of the parameters to make sure it is of the type I expect. For instance: def myfunction(dbConnection):if (type(dbConnection)<>bpgsql.Connection):r[error…

Changing the background color of the axes planes of a 3D plot

On the basis of the scatterplot example of matplotlib, how can I change the gray background color of the 3 axes grid planes? I would like to set it to white, keeping the grid lines with the default gr…

Select checkbox using Selenium with Python

How can I select the checkbox using Selenium with Python? from selenium import webdriver from selenium.webdriver.common.keys import Keysbrowser = webdriver.Firefox() url = Any URL browser.get(url)brow…

linear programming in python?

I need to make a linear programming model. Here are the inequalities Im using (for example):6x + 4y <= 24 x + 2y <= 6 -x + y <= 1 y <= 2I need to find the area described by these inequaliti…

Beginner Python Practice? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic…

Pylint: Disable specific warnings for specific folder

We have a Python project laid out like this:project/ ├── .pylintrc ├── module1.py ├── module2.py └── tests/├── test_module1.py└── test_module2.pyOur unit and function tests reside in …

PyCharm: Configuring multi-hop remote Interpreters via SSH

To connect to the computer at my office I need to run ssh twice. First to connect to the host-1 and then from host-1 to host-2 and each one has different credentials. However the configuration menu in …

Return results from multiple models with Django REST Framework

I have three models — articles, authors and tweets. Im ultimately needing to use Django REST Framework to construct a feed that aggregates all the objects using the Article and Tweet models into one r…

Comparing XML in a unit test in Python

I have an object that can build itself from an XML string, and write itself out to an XML string. Id like to write a unit test to test round tripping through XML, but Im having trouble comparing the tw…

Passing a tuple as command line argument

My requirement is to pass a tuple as command line argument like --data (1,2,3,4)I tried to use the argparse module, but if I pass like this it is receiving as the string (1,2,3,4). I tried by giving ty…