The tools I've tried so far are only gives me approximations to the start and end times that I specify. They are usually off by a few seconds.
I need a tool to extract video and audio at exact start and stop times, ideally to the millisecond. It would be preferable to do so without any easily noticeable loss in video quality from the original. I would also prefer to keep the audio in the video.
Using this test video, I cut a 4 and a half second chunk of video and audio out and into a new.mp4
with this:
ffmpeg -i sample-30s.mp4 \
-filter:v "trim=8.25:12.75,setpts=PTS-STARTPTS" \
-filter:a "atrim=8.25:12.75,asetpts=PTS-STARTPTS" \
-vcodec libx264 \
new.mp4
The various options mean:
-filter:v
is to filter video and -filter:a
is the same for audio.trim=
and atrim=
specify the start and stop time for the cut to make.setpts=
and asetpts=
apply the PTS-STARTPTS filter to change the timestamps to 0:00 in the output file.-vcodec libx264
uses the x264 library.The timestamps can take many forms such as '23.189' for 23.189 seconds, ‘12:03:45’ for 12 hours, 03 minutes and 45 seconds, and 200000 microseconds for 0.2s.
You need the setpts/asetpts option to reset timestamps, as explained here:
Be aware that frames are taken from each input video in timestamp order, hence, if their initial timestamps differ, it is a good idea to pass the two inputs through a setpts=PTS-STARTPTS filter to have them begin in the same zero timestamp, as the example for the movie filter does.