brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: ffmpeg -ss with -to cuts a clip, and -c copy makes it start at a keyframe

Ffmpeg
~/tips/tip-ffmpeg-to-cut-clip

ffmpeg -ss 00:01:00 -to 00:01:30 -i match.mp4 -c:v libx264 -crf 20 -c:a aac goal.mp4

Cuts a part out of a video between two times, with a fast or an exact method.

Cutting a part out of a longer video takes two times: where to start and where to stop. ffmpeg has -ss for the start and -to for the end.

ffmpeg -ss 00:01:00 -to 00:01:30 -i match.mp4 -c:v libx264 -crf 20 -c:a aac goal.mp4

Both options come before -i here, so they apply to the input and -to is a time in the original file. That gives exactly 30 seconds. If you’d rather give a length, use -t 30 instead of -to.

The tempting shortcut is -c copy, which skips encoding and is instant. It has a price: without re-encoding, ffmpeg can only start the clip on a keyframe, and those may be several seconds apart. On a test file with a keyframe every 10 seconds, a 3-second copy cut came out 5.12 seconds long. So the clip starts earlier than you asked, or begins with a frozen or grey picture in some players.

My rule: -c copy when a rough cut is fine, like trimming a long recording, and a re-encode when the start has to be on the exact frame. Encoding only a short clip doesn’t take long anyway.

Further reading: ffmpeg documentation

More Ffmpeg tips ffmpeg -stream_loop 2 -i in.mp4 -c copy out.mp4 ffmpeg -i in.mp4 \ ffmpeg -i video.mp4 -i video.en.srt -map 0 -map 1 -c copy -c:s mov_text -metadata:s:s:0 language=eng video-subs.mp4 ffmpeg -i film.mkv -map 0:v -map 0:a:0 -c:v copy -c:a aac -b:a 192k -ac 2 film-stereo.mkv ffmpeg -i talk.mp4 -vn -c:a copy talk.m4a All Ffmpeg tips (38) All topics

Link