FFmpeg will do this for you.
Your command might look something like this:
Code:
ffmpeg -i input.mpg -ss 00:00:10 -t 00:00:30 out1.mpg
-ss is the start point in hh:mm:ss from the beginning of your video file
-t is the length of time in hh:mm:ss of your new segment.
So, in the above example, you're starting 10 seconds in from the beginning of the original file and ending 30 seconds later.
If you want to create multiple parts in one pass then the following should work:
Code:
ffmpeg -i input.mpg -ss 00:00:10 -t 00:00:30 out1.mpg -ss 00:00:35 -t 00:00:30 out2.mpg
In this example, the first segment is the same as the first example, but you're also creating a second file starting at 35 seconds in and being 30 seconds long.
.
timecode_frame_start
does not work like this.
Seeking based on frame numbers is not possible. The only way to start at specific frames is to convert a number of frames to ss.ms
syntax, or hh:mm:ss.ms
. So, if your video is at 25 fps, and you want to start at 133 frames, you would need to first calculate the timestamp:
133 / 25 = 5.32
Then run:
ffmpeg -ss 5.32 -i input.mp4 -c:v libx264 -c:a aac out.mp4
Note that cutting on exact frames with bitstream copy (-c:v copy
) is not possible, since not all frames are intra-coded ("keyframes"). A video must begin with a keyframe to be decoded properly. You will therefore have to re-encode the video, e.g. to H.264 using -c:v libx264
as shown above. You can also choose a lossless codec like -c:v ffv1
which preserves the quality of the input video.
To summarize, -ss
will always be frame-accurate when performing re-encoding.
If you further want to encode a specific number of frames, use -frames:v
, for example:
ffmpeg -ss 5.32 -i input.mp4 -c:v libx264 -c:a aac -frames:v 60 out.mp4
Note that you you also have the choice to use the select
/aselect
filters to select frames/audio samples.
ffmpeg -i input.mp4 -vf 'select=gte(n\,100)' -c:v libx264 -c:a aac out.mp4
This, however, is slower than the -ss
option shown above, since the entire video will be decoded.
Best Answer
Cutting video with
ffmpeg
You can accurately cut videos with FFmpeg. Since version 2.5 it's very easy.
This would for example cut 10 seconds, starting from 0 minutes, 3 seconds and 123 milliseconds.
The position and the time may be either in seconds or in
hh:mm:ss[.xxx]
form.Note that in these examples, video will be re-encoded using the x264 encoder; audio is copied over.
You can also use
-to
instead of-t
to specify the end point instead of the duration. In this case, however,-to
is equivalent to-t
, since by putting the-ss
in front of-i
, ffmpeg will first seek to that point and then start outputting.If the output does not appear to be cut correctly, adding
-fflags +genpts
to the command may help.See also the Seeking wiki entry.