[FFmpeg-user] Command to extract and concatenate video sections in one step?

Bo Berglund bo.berglund at gmail.com
Sat Aug 20 08:25:38 EEST 2022


This is about ffmpg running on Ubuntu Server 20.04 LTS

I have a program which uses ffmpeg to extract sections of a video file and then
paste them together into one output video file.
The program uses this type of command to extract each section:

ffmpeg -hide_banner -ss <TSTRT> -i input.mp4 -to <CLIPT> -c copy clipN.mp4

Here
 <TSTRT> is the start time in seconds inside the video
 <CLIPT> is the length in seconds of he extracted video section
 clipN.mp4 is the name of the output files where N is the sequence number

Once these files have been created a list file join.txt looking like this is
created:

file clip1.mp4
file clip2.mp4
file clip3.mp4
file clip4.mp4
file clip5.mp4

And finally the concatenation is done using this command:

ffmpeg -hide_banner -f concat -safe 0 -i join.txt -c copy joinedfile.mp4

Then the program must do cleanup operations to delete all of the created
clipN.mp4 files as well as the join.txt file.

This works but has drawbacks like:
- If multiple calls are made to the program there is a chance that they operate
at the same time and thus there is confusion regarding file names.

- Intermediate files are crated on the file system

In order to avoid this and get a single ffmpeg command I have been advised to
use this type of command instead:

ffmpeg -hide_banner \
-ss <TSTRT1> -t <CLIPT1> -i input.mp4 \
-ss <TSTRT2> -t <CLIPT2> -i input.mp4 \
-ss <TSTRT3> -t <CLIPT3> -i input.mp4 \
-ss <TSTRT4> -t <CLIPT4> -i input.mp4 \
-ss <TSTRT5> -t <CLIPT5> -i input.mp4 \
-lavfi "concat=n=5:v=1:a=1" -an output.mp4

With this all intermedate files are gone and therefore also the problem of
running the command in several instances at the same time.

But now instead of using the copy function it re-encodes the output video, which
takes a lot of time and should not be needed since the source is a single video
file for all clips so enconding should be consistent.

QUESTION:
---------
How can I change the "combined" command such that it will use the copy function
rather than re-encoding?
I assume that this part should be different, but how?

-lavfi "concat=n=5:v=1:a=1" -an output.mp4

I have tried to read the section in the documentation about concatenation but I
don't really understand it...

TIA!


-- 
Bo Berglund
Developer in Sweden



More information about the ffmpeg-user mailing list