Skip to content

Tips: To extract cut by time

suntong edited this page Sep 11, 2021 · 10 revisions

Extract/Cut simply by time

This can be easily done using the "-t" option. Take a look at the example.

$ ffmpeg -i input.fmt1 -t 30 output.fmt2

The time specified is in seconds. But the format hh.mm.ss is also supported. This command will convert the first 30 seconds of the file input.fmt2 into output.fmt2.

Trim a media file using start time and length/duration

To trim down a video to smaller clip using start and stop times, we can use the following command.

$ ffmpeg -i input.mp4 -ss 00:00:50 -codec copy -t 50 output.mp4

Here,

  • –ss – Indicates the starting time of the video clip. In our example, starting time is the 50th second.
  • -t – Indicates the total time duration.

This is very helpful when you want to cut a part from an audio or video file using starting and ending time.

Similarly, we can trim down the audio file like below.

$ ffmpeg -i audio.mp3 -ss 00:01:54 -t 00:06:53 -c copy output.mp3

Trim a media file by start time and duration using ffcvt

$ ffcvt -f tears_of_steel_720p.mov -force -o ' -t 30'

(Note the space before -t is necessary)
or,

$ ffcvt -f tears_of_steel_720p.mov -force -- -ss 00:01:50 -t 30

Trim a media file using start and stop times

Sometimes, using start and stop times instead of start time and duration is more convenient, especially when you get those times when playing the video. In such case replace the -t duration with the -to stop_time in the above step.

Note, from ffmpeg -help:

-t duration         record or transcode "duration" seconds of audio/video
-to time_stop       record or transcode stop time

Extract/Cut out multiple segments

If you want to extract / cut out multiple segments by time, then however, using ffmpeg alone, just like golopot put it, "is very unhuman". However, ffcvt is here to help:

export FFCVT_CRF=33.8 FFCVT_O='-speed 2'

FFCVT_FTC=Nice_scenic_videos_from_dashcam_With_Evening_sunset_view.webm

$ nice ffcvt -nc -f "$FFCVT_FTC" -t wx -C 00:00:40-00:00:50 -C 00:00:53-00:00:56 -C ' 00:03:10 - 00:03:12 ' -C 00:03:36-

== Transcoding: Nice_scenic_videos_from_dashcam_With_Evening_sunset_view.webm
] ffmpeg -i Nice_scenic_videos_from_dashcam_With_Evening_sunset_view.webm -speed 2 -c:v libx264 -x264-params crf=33.8 -c:a aac -b:a 48k -q:a 3 -filter_complex [0:v]trim=start=40:end=50,setpts=PTS-STARTPTS[v0];[0:a]atrim=start=40:end=50,asetpts=PTS-STARTPTS[a0];[0:v]trim=start=53:end=56,setpts=PTS-STARTPTS[v1];[0:a]atrim=start=53:end=56,asetpts=PTS-STARTPTS[a1];[0:v]trim=start=190:end=192,setpts=PTS-STARTPTS[v2];[0:a]atrim=start=190:end=192,asetpts=PTS-STARTPTS[a2];[0:v]trim=start=216,setpts=PTS-STARTPTS[v3];[0:a]atrim=start=216,asetpts=PTS-STARTPTS[a3];[v0][a0][v1][a1][v2][a2][v3][a3]concat=n=4:v=1:a=1[vo][ao] -map [vo] -map [ao] Nice_scenic_videos_from_dashcam_With_Evening_sunset_view_.m4v

Done.
Org Size: 9765 KB
New Size: 628 KB
Saved:    93% with 9137 KB
Time: 2.148489s at 2021-09-11 16:42:16

image

Notes,

  • The source file (credit here) and the cut-short file can be found in https://github.com/suntong/ffcvt/issues/7
  • What the above command does is that, it extracted / cut out segments of,
    • 40-50sec as the first segment,
    • 53-56sec as the second segment,
    • 00:03:10 - 00:03:12 as the third segment, and
    • 00:03:36 till the end as the last segment
  • And as you can see that, the time format for the segments has to be strictly in the hh:mm:ss format.
    Even tiny slightly variation will fail:
$ ffcvt -C '0:3:10-0:3:12'
start - Cut range 0 format error for '0:3:10-0:3:12'

$ ffcvt -C '00:03:10-0:3:12'
end - Cut range 0 format error for '00:03:10-0:3:12'

Here is the online help part:

  -C    Cut segment(s) out to keep. Specify in the form of start-[end],
        strictly in the format of hh:mm:ss, and may repeat

Clone this wiki locally