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

Extracting images from a video:

ffmpeg -i input.mp4 -r {frames/second} -s {WxH} -f image2 out-%06d.jpeg

1분당 1장, HD(1280x720) 크기로 이미지 추출:

ffmpeg -i input.mp4 -r 1/60 -s 1280x720 -f image2 out-%06d.jpeg

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

Cutting Videos Based on Time Range using FFmpeg  (0) 2022.11.23
FFmpeg CLI  (0) 2021.08.15

H.264 + MP4 container, no audio

ffmpeg -i in.avi -vcodec h264 out.mp4
ffmpeg -i in.avi -ss 00:12:34 -t 01:23:45 out.mp4

-ss : the starting timecode, hh:mm:ss.xxx -t : the duration to transcode after the starting timecode, If omitted, will encode to the end.

-vcodec copy -acodec copy -vn : ignore video -an : ignore audio


To encode an image sequence to a video file:

ffmpeg -framerate 25 -i “image_%03d.png” -vcodec h264 -b:v 10485760 out.mp4
ffmpeg -framerate 24 -start_number 12 -i “image_%03d.png” -vcodec h264 -b:v 10485760 out.mp4

-b:v : the video bitrate of the output file


Transcode a video to an image sequence:

ffmpeg -i in.mp4 -vocdec png -an “image_%03d.png”

 


Rotate video

0 = 90CounterClockwise and Vertical Flip (default)

1 = 90Clockwise

2 = 90CounterClockwise

3 = 90Clockwise and Vertical Flip

To rotate 180 degress, instead use “transpose=2,transpose=2”

ffmpeg -i in.mp4 -vf “transpose=1” -codec:a copy out.mp4

 

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

Cutting Videos Based on Time Range using FFmpeg  (0) 2022.11.23
FFmpeg CLI Tips  (0) 2021.09.01

+ Recent posts