Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cut from multiple input videos and concat to single video using ffmpeg?

Tags:

ffmpeg

I did something like this

ffmpeg -i video1.mp4 -ss 00:01:00 -to 00:03:00 -i video2.mp4 -ss 00:05:00 -to 00:02:00 -c copy cut.mp4

Apparently muxing didnt happen.

How can I make it work?

like image 788
Hadley V Sunny Avatar asked Feb 20 '18 10:02

Hadley V Sunny


1 Answers

You have to tell ffmpeg that you want to concatenate each video and audio stream:

ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex
"[0:v]trim=start=60:end=180,setpts=PTS-STARTPTS[v0];
 [0:a]atrim=start=60:end=180,asetpts=PTS-STARTPTS[a0];
 [1:v]trim=start=300:end=420,setpts=PTS-STARTPTS[v1];
 [1:a]atrim=start=300:end=420,asetpts=PTS-STARTPTS[a1];
 [v0][a0][v1][a1]concat=n=2:v=1:a=1[v][a]"
-map "[v]" -map "[a]" output.mp4
  • (a)trim creates each segment, (a)setpts resets the segment timestamps (recommended for concat), and concat concatenates each segment. See FFmpeg Filters for documentation on each filter.

  • I broke up the command into separate lines for readability. Reconstitute into a single line or add whatever line-break characters are suitable for your environment.

  • This will re-encode the video but cutting will be accurate. If you want to avoid re-encoding see the concat demuxer but cutting may not be accurate and results may not be satisfactory.

  • The -to value cannot be smaller than the -ss value, so I'm going to assume you meant -ss 00:05:00 -to 00:07:00 instead of -ss 00:05:00 -to 00:02:00.

like image 73
llogan Avatar answered Sep 29 '22 08:09

llogan