Reference: https://www.baeldung.com/linux/ffmpeg-cutting-videos

1. Clipping with Re-Encoding

Video encoding is the process of compressing and preparing a video for output,

thus, making video sizes reasonably small and quick to process.

By default, the re-encoding will use the codec used in the orignal video.

$ ffmpeg -i input.mp4 -ss 00:00:15 -t 00:00:10 -async -1 output.mp4
  • -i input.mp4 : used for specifying input files
  • -ss 00:12:34 : seeks to the timestamp specified
  • -t 00:10:00 : used to specify the duration of the clip
  • -async -1 : spcifies whether to contract or stretch the audio to match the timestamp.
    The value 1 will correct the start of the stream without any later correction.

Alternatively, if we need a more time-accurate cut,

we can manually add the keyframes to the start and end of the clipped video:

$ ffmpeg -i input.mp4 -force_key_frames 00:00:15,00:00:25 output.mp4
  • -force_key_frames : video clipping occurs at keyframes
    However, if the first frame is not a keyframe,
    then the frames before the first keyframe in the clip will not be playable.
    Therefore, we forced FFmpeg to add keyframes at the first and last frames to ensure we encode a perfect clip.
    Moreover, to limit errors, we should avoid adding lots of keyframes.

 

2. Clipping Instantly via Stream Copy

FFmpeg allows for copying the codec from the orignal video to the trimmed video,
which takes only a few seconds.

$ ffmpeg -i input.mp4 -ss 00:00:15 -to 00:00:25 -c copy output.mp4
  • -to : specifies the end of the clip. (from 00:0015 to 00:00:25)
  • -c : to copy both audio and video codecs to the output.mp4 container

If we're using different containers,

we'll be presented with a container mismatch error.

If we have two different containers, we can specify the copying options separately.

$ ffmpeg -i input.mkv -ss 00:00:15 -to 00:00:25 -acodec copy -vcodec copy output.mp4

 

3. Clipping Using the trim Filter

It's useful when we have a short video, preferably less than a minute,

and we want to cut a small portion of it:

$ ffmpeg -i input -vf trim=10:25,setpts=PTS-STARTPTS output.mp4
  • -vf : specifies that we're using a video filter
  • We provided the trim filter with the value 10:25,
    which will slice the video from 00:00:10 to 00:00:25.
  • The setpts filter sets the presentation timestamp for each video frame in the clip.
    We set its value to be PTS-STARTPTS to make sure our clip doesn't delay or halt at the start,
    and the frames are synchronized relative to the setpts value, which is 0.

 

 

 

'C, C++ > FFmepg' 카테고리의 다른 글

FFmpeg CLI Tips  (0) 2021.09.01
FFmpeg CLI  (0) 2021.08.15

+ Recent posts