ffmpeg -i input.mp4 -map 0 -c copy -f segment -segment_time 120 -reset_timestamps 1 out%02d.mp4
...will create files along the lines of 'out01.mp4 'out02.mp4', each one being 120 seconds in duration (except the last one, which may be shorter). Note that it has to split up the file on a keyframe, so you won't get 100% accurate splitting. See the segment documentation.
If you're willing to re-encode, you can get accurate splitting using -force_key_frames
. To force a key frame every 120 seconds and split using segment:
ffmpeg -i input.mp4 -map 0 -c:v libx264 -preset veryfast -crf 22 -c:a libfdk_aac -vbr 3 \
-force_key_frames expr:gte(t,n_forced*120) -f segment -segment_time 120 -reset_timestamps 1 out%02d.mp4
Alternatively, you can use -g
to set the GOP size - this requires you to know the frame rate of your video. If your video has a frame rate of 25 fps, then a GOP of 120*25=3000 would get one keyframe every two minutes; obviously, this is too few! Reducing it to 300 will be more reasonable, and will give you keyframes on the places you need:
ffmpeg -i input.mp4 -map 0 -c:v libx264 -preset veryfast -crf 22 -c:a libfdk_aac -vbr 3 \
-g 300 -f segment -segment_time 120 -reset_timestamps 1 out%02d.mp4
The specific codecs your version of ffmpeg supports may be different to mine (you won't have libfdk_aac unless you compiled from source).
Thanks. Any explanatio on this:
${infile/.mpg/.$((i/120)).mpg}
? – DocWiki – 2011-07-08T23:16:46.640Assuming that the infile's extension is
.mpg
, that should produce output files namedinfile.0.mpg infile.1.mpg infile.2.mpg ...
– blahdiblah – 2011-07-08T23:36:04.320Some checking with
ffmpeg
reveals that the$?
condition isn't going to hit anyways becauseffmpeg
doesn't care if-ss
is out of range. Changing the exit condition based on finding the length of the video or that output files aren't empty is left as an exercise for the reader. – blahdiblah – 2011-07-09T00:00:55.997Nice exercise! MIN=
ffmpeg -i ${1} 2>&1 | grep "Duration" | cut -d ' ' -f 4 | sed s/,// | cut -d ":" -f 2
– DocWiki – 2011-07-09T00:36:13.340Here is my code: http://stackoverflow.com/questions/6631971/bash-really-newbie-question-and-really-strange Some errors cant be corrected.
– DocWiki – 2011-07-09T00:53:01.267