Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cut multiple videos and merge with ffmpeg

Tags:

video

ffmpeg

How can I cut a video into multiple parts and then join them together to make a new video with ffmpeg?

like image 561
rinofcan Avatar asked Mar 12 '17 13:03

rinofcan


People also ask

What is video concatenation?

When using FFmpeg to merge a series of video clips, the process is called concatenation, or concat for short. There are several ways that videos can be concatenated, depending on whether the individual videos share the same codec or not.

How do I combine two videos in android programmatically FFmpeg?

run(new String[] { "ffmpeg", "-i", "'concat:" + ts1 + "|" + ts2 + "'", "-vcodec", "copy", "-acodec", "copy", "-absf", "aac_adtstoasc", output });


2 Answers

You can use the concat demuxer.

#1 Create a text file

file video.mp4
inpoint 34.5
outpoint 55.1
file video.mp4
inpoint 111.0
outpoint 155.3
file video.mp4
inpoint 278
outpoint 316.4

The inpoint/outpoint directives specify the trim in and out points in seconds for the file listed above them.

#2a Create the joint file

ffmpeg -f concat -i list.txt combined.mp4

#2b Do the overlay together

ffmpeg -f concat -i list.txt -i background.mp4 -filter_complex "[0:v]scale=400:400[v1];[1:v][v1]overlay=0:0:shortest=1" -shortest -preset superfast "output.mp4"

External Audio stream with concat

ffmpeg -i 12m.mp4 -f concat -i list.txt -vf setpts=(PTS-STARTPTS)/1.1 -af atempo=1.1 -map 1:v -map 0:a -shortest new.mp4
like image 188
Gyan Avatar answered Oct 05 '22 00:10

Gyan


You question is quiet general...
The following example may help you, but it might not solve your specific issues.

The example applies three stages:

  • Create synthetic video (with no audio):

    ffmpeg -f lavfi -i testsrc=duration=3:size=160x120:rate=10 -c:v rawvideo -pix_fmt rgb24 testsrc.avi
    

    (The created video is uncompressed).
    Reference: https://trac.ffmpeg.org/wiki/FilteringGuide

  • Cut video into 3 parts (creating 3 video files):

    ffmpeg -i testsrc.avi -ss 00:00:00 -c copy -t 00:00:01 sec0.avi
    ffmpeg -i testsrc.avi -ss 00:00:01 -c copy -t 00:00:01 sec1.avi
    ffmpeg -i testsrc.avi -ss 00:00:02 -c copy -t 00:00:01 sec2.avi
    

    Reference: https://superuser.com/questions/138331/using-ffmpeg-to-cut-up-video

  • Concatenate (merge) 3 parts in reverse order:

    ffmpeg -i "concat:sec2.avi|sec1.avi|sec0.avi" -codec copy output.avi
    

    Note: for Linux use single quotes '
    Reference: Concatenate two mp4 files using ffmpeg

Synthetic video looks as the following image:
enter image description here

like image 45
Rotem Avatar answered Oct 05 '22 01:10

Rotem