Skip to content

Tips: To extract cut by time

AntonioSun edited this page Dec 11, 2021 · 9 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

Sample:

 FFCVT_FTC='Shoulder Pain Relief Stretches in 5 Minute.mkv' 
 cut_from=1:22
 cut_to=7:51

$ date; nice ffcvt -nc -f "$FFCVT_FTC" -t wx -- -ss $cut_from -to $cut_to
Sat Dec 11 15:32:10 EST 2021

== Transcoding: Shoulder Pain Relief Stretches in 5 Minute.mkv
] ffmpeg -i Shoulder Pain Relief Stretches in 5 Minute.mkv -speed 2 -c:v libx264 -x264-params crf=33.8 -c:a aac -b:a 48k -q:a 3 -ss 1:22 -to 7:51 Shoulder Pain Relief Stretches in 5 Minute_.m4v

Done.
Org Size: 12537 KB
New Size: 8060 KB
Saved:    35% with 4477 KB
Time: 11.129677978s at 2021-12-11 15:32:21


Transcoding completed in 11.129997814s
Org Size: 12 MB
New Size: 7 MB
Saved:    35%

Trim using start / stop times and keep subtitle in sync

To carve from the video and keep subtitle in sync using ffcvt:

export FFCVT_CRF=33.8 FFCVT_O='-speed 2'

FFCVT_FTC=HowToFreelineSkates_Step4UpHILL.webm
cut_from=00:1:30
cut_to=0:8:37

date; nice ffcvt -nc -f "$FFCVT_FTC" -w $FFCVT_DST/files -t wx -- -hide_banner -vf subtitles=HowToFreelineSkates_Step4UpHILL.vtt -ss $cut_from -to $cut_to
Mon Sep 13 13:09:45 EDT 2021

== Transcoding: HowToFreelineSkates_Step4UpHILL.webm
] ffmpeg -i HowToFreelineSkates_Step4UpHILL.webm -speed 2 -af volume=3.2 -c:v libx264 -x264-params crf=33 -c:a aac -b:a 48k -q:a 3 -hide_banner -vf subtitles=HowToFreelineSkates_Step4UpHILL.vtt -ss 00:1:30 -to 0:8:37 /d/Prj/cvt/files/HowToFreelineSkates_Step4UpHILL.m4v

Done.
Org Size: 24420 KB
New Size: 15802 KB
Saved:    35% with 8618 KB
Time: 32.2243189s at 2021-09-13 13:10:17


Transcoding completed in 32.2251574s
Org Size: 23 MB
New Size: 15 MB
Saved:    35%

Check out the result at

HowToFreelineSkates_Step4UpHILL.mp4

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'

$ ffcvt -C 00:03:10        
pair - Cut range 0 format error for '00:03:10'

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