Using FFMPEG to trim your videos

 There are multiple methods to trim or clip videos with the help of FFMPEG, but sometimes looking at the documentation can confuse lots of us, and we cannot derive the output we want. And the examples on the documentation are simpler than what we want to do.


So I will show few methods to combine, trim or clip video files using FFMPEG. I won't be going through installation process and will assume that you already have it installed.


1. Using timestamps to clip the video ๐Ÿ•

 

ffmpeg -i video.mp4 -ss 00:00:00 -to 00:10:00 -c copy output.mp4

 

This command will copy the clip from 0 sec to 10 min without re-encoding, since we specify not to re-encode.

Break down of command:

  1.      -i  : input video file
  2.     -ss : starting timestamp you can define in hh:mm:ss or just secs
  3.     -to : ending timestamp, this is the timestamp of the video
  4.     -c copy : codec is copy, which doesn't require re-encoding

 

If you want to copy the video from some other time to some duration 

 

ffmpeg -ss 00:10:00 -i video.mp4 -t 600 -c copy output.mp4

 

Here we read the video from 10min and then get 600sec(10 min) from the video. For this to run make sure you have a video whose duration is greater than 20 min. 

Note: If you had used -ss after the input of video -i, then ffmpeg will read the video from 0 sec and then start writing from -ss time, so to speed this up we read right from the starting timestamp.

 

 

2.  Combining multiple video files into one ๐Ÿ“น

We will create a text file containing video files and we will input that text file to the command line. The files should be same codec or else artifacts would be generated.

 

ffmpeg -f concat -i list.txt -c copy output.mp4

 

Our list.txt looks like:

 

file 'video1.mp4'

file 'video2.mp4'

 

 Breakdown:

  1.     -f concat : file filter for concatenation
  2.     -i : input for the command
  3.     -c copy : copy codec



3. Using complex filters to create a clip

This one is helpful when you want to get short sub clips from a big video, for example, you have a movie and you want only some fight sequences from the main file.

For this you can create complex filter that will have trim, split and concat function to do the work. Since this method has a filter so the video will be trans-coded and  copy codec will not be used.


ffmpeg -i video.mkv -filter_complex "[0:v]split=2[vc1][vc2];
[vc1]trim=start=1:duration=10,setpts=PTS-STARTPTS[v1];
[vc2]trim=start=300:duration=10,setpts=PTS-STARTPTS[v2];
[0:a]asplit=2[ac1][ac2];
[ac1]atrim=start=1:duration=10,asetpts=PTS-STARTPTS[a1];
[ac2]atrim=start=300:duration=10,asetpts=PTS-STARTPTS[a2];
[v1][a1][v2][a2]concat=n=2:v=1:a=1[video][audio]" -map "[video]" -map "[audio]" output.mkv


this one is complex, this command trims two portion from the input video.

1. from 1 sec to 11 sec (10sec clip)

2. from 5 min to 5min 10 sec (10 sec clip)

there are four steps, 2 for video and 2 for audio


Breakdown :

    -i : input file (can be multiple -i s)

    -filter_complex : creating a complex filter for the file

    [0:v] : get the video stream of 0th file (our input file), if you have multiple file change the index (remember index starts from 0)

    split=2[vc1][vc2] : splitting the video file into 2 (any no can do) [vc1] these are labels to reference later you can name it anything you like

    - now we have splitted the input file into two separate copies now we can trim parts from them, if you want to trim more parts you have create multiple copies

 

    [vc1]trim=start=1:duration=10,setpts=PTS-STARTPTS[v1]

                here the trim function is used, it takes in start (timestamp / secs); duration(sec) also end is available. setpts is the point where video should end. And finally naming the output of trim as [v1]  

    same thing for the second trim only we are cutting from 5min to 5min 10 sec

    [0:a] : audio stream of video file, you can specify separate audio file too

    split=2[ac1][ac2] : again splitting into two naming each of them

     just like video trim we have atrim and asetpts. Many of the ffmpeg function are similar if it fun for video it would be afun for audio.

Finally we have all outputs of out trims [v1][v2][a1][a2]. Now we will concat them

 

[v1][a1][v2][a2]concat=n=2:v=1:a=1[video][audio]

here we concat the input labels(trims) into single video and audio and we name them [video] and [audio], the n is the no of streams (default = 2) 

 

And then, we map our labels to output file using -map "[label]"

 

If you know a better way, I am sure there is, then comment down below.

For any advice or suggestion write them in comments.

Popular posts from this blog

Audio de-noising using Python (Wavelets)

Converting HTML5 Game for Android | 2 Methods