Use FFmpeg to split a video into equal-length segments

11,029

The original code (that didn't work for me) was:

ffmpeg -i input_video.MTS -c copy -map 0 -segment_time 8 -f segment output_video%03d.MTS

The code that did end up working for me:

ffmpeg -i input_video.MTS -c copy -map 0 -segment_time 8 -f segment output_video%%03d.MTS

The "%" needed to be "%%". Also, it worked in a .bat file (not .sh).

Share:
11,029
amateur3057
Author by

amateur3057

Updated on June 04, 2022

Comments

  • amateur3057
    amateur3057 almost 2 years

    I need to split many videos of unknown length into 2-minute chunks. I can use the accepted answer from https://unix.stackexchange.com/questions/1670/how-can-i-use-ffmpeg-to-split-mpeg-video-into-10-minute-chunks to do this, but this would require copying, pasting, and modifying many lines and manually calculating how many 2-minute parts are in each video (not a big deal for a few videos, but after a while it gets really tedious).

    I have used code in the past in which all I have to do is specify the input video, the start time, segment length, and the output name and by running it in a .sh file in the same folder as the input video it generates all the necessary separate videos which are labeled "output_video01," "output_video02," etc. However, somehow it doesn't want to work on my new computer. Other answers which claim to be able to do this don't work for me, either (when run as either a .bat or .sh file). I believe the code I previously used was:

    ffmpeg -i "input_video.MTS" -ss 164 -f segment -segment_time 120 -vcodec copy -reset_timestamps 1 -map 0:0 -an output_video%d.MTS
    

    Another suggestion that doesn't work for me but apparently works for others (see https://superuser.com/a/820773/313829):

    ffmpeg -i input_video.MTS -c copy -map 0 -segment_time 8 -f segment output_video%03d.MTS
    

    I currently have Windows 10 with the anniversary update build in which I have enabled Bash, but for some reason it doesn't want to work. I can't even see the error it gives me because the window closes so abruptly.

  • Rafael Lima
    Rafael Lima about 6 years
    % is a reserved character in windows, so whenever you need to use it in shell command you need to scape it %% in unix it is not necessary