FFmpeg -ss -t (seeking) output is not accurate, any workarounds? [duplicate]

My mission is to make small video segments from a larger one, then join them together into one file. After a lot of search and reading I decided to use the next command

ffmpeg -y -ss 03:00 -i myvideo.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts -t 00:05 segment01.ts

My problems; hoping that you have any workarounds to solve them:

1- As you can see the wanted output's duration is 5 seconds which I didn't get at all. I got different duration for different segments; 4, 6, 8 ,9 seconds.

**After searching I found out that happening due to keyframes issues.

But I don't get at all what that has to do with the segment's duration.

I am totally understand that the seeking position could differ due to keyframes, but why the duration?

2-After more reading, I decided to use -fs command to limit the segment's size. It worked somehow except one thing.

The segments have like a little hang (drop frames! I have no idea) at the end, making the final result when joining together is terrible. What can I do to get rid off this "hang"

Any Ideas?

**Note: I have tried both of (input/output) seeking but nothing changed.


Solution 1:

It often happens when using the -ss and -t together with -c copy or -codec copy.

Don't use copy, and use another codecs or simply don't specify -c , -codec options. and this won't happen.

for example: ffmpeg -y -ss 03:00 -i myvideo.mp4 -c:v libx264 -f mpegts -t 00:05 segment01.ts or something like that.