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 -f segment -segment_time 10 splits a video into equal pieces

Ffmpeg
~/tips/tip-ffmpeg-segment-muxer-split-file

ffmpeg -i in.mkv -f segment -segment_time 10 -segment_format_options movflags=+faststart out%03d.mp4

Cuts one video into numbered files of a fixed length in a single command.

Cutting a long video into pieces by hand means typing a lot of -ss values. The segment muxer does it in one run and numbers the files for you.

The example from the ffmpeg formats documentation reads:

ffmpeg -i in.mkv -f segment -segment_time 10 -segment_format_options movflags=+faststart out%03d.mp4

-f segment selects the muxer, and -segment_time sets the length of each piece. The docs say the value is a duration and the default is 2 seconds, so you always want to set it yourself. The %03d in the name becomes 000, 001, 002 and so on. -segment_format_options hands options to the container of each piece, here the fast start flag that moves the index to the front of every MP4.

Two related options from the same page are worth knowing. reset_timestamps resets the timestamps at the beginning of each segment, so that every piece starts near zero, and segment_format overrides the container, which is otherwise guessed from the extension of the name:

ffmpeg -i in.mkv -f segment -segment_time 10 -reset_timestamps 1 part%03d.mkv

I’d add reset_timestamps when the pieces will be played or edited on their own, because a piece that starts at minute 7 on its clock confuses some players.

The caveat: the docs example re-encodes, because nothing in it asks for stream copy. If you add -c copy to make it quick, the stream can only be cut where the format allows, so the real pieces may not be exactly 10 seconds. Look at their lengths with ffprobe afterwards and decide if that is fine for you.

Also make sure the output folder holds nothing with the same names. The manual says -y overwrites output files without asking, and -n exits immediately if an output file already exists, so pick the one that fits.

Further reading: ffmpeg formats documentation: segment muxer

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