11

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.

user2132188
  • 595
  • 5
  • 9
  • 21
  • You can’t cute a video halfway into a block without re-encoding. Because the frames in-between keyframes are not full frames. Their pixels refer to the last keyframe and other frames before it. And to cut there, you need to decode that keyframe, apply the other references, and create a new keyframe out of it. Since you told ffmpeg to not process the video data, it can only cut at a frame without causing severe errors in the file. And since it makes no sense to cut the audio somewhere else (and you didn’t tell it what to put in the gap), it cuts the audio there too. – Evi1M4chine May 24 '22 at 14:59
  • Alternatives would be, to either allow re-processing. Or if you want to save as much processing as possible, you could extract just the bit between the first two keyframes, and only re-encode and precisely cut THAT, and then re-attach that to the more imprecisely cut but totally unprocessed rest of the video. (Though some video formats may not support cutting without re-encoding, even at keyframes.) Needless to say, this has not been implemented in ffmpeg, due to its speciality. Could be done manually with ffmpeg though, of course. – Evi1M4chine May 24 '22 at 15:04

1 Answers1

5

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.

Behroozfar
  • 868
  • 5
  • 12
  • 3
    That's not a solution as it would force a reencode and mess up subsequent joining of the splits, as per op's requests. The issue with ss is that it's buggy. Even if ss is at a keyframe, it should work, but it doesn't. Speaking out of experience. – JasonXA Mar 22 '20 at 16:29
  • It seems that placing ss / t / to before the input will fix the issue even with copy in new versions of ffmpeg. Which is non-suggestive. when using timeframes of I-frames, it should not matter if ss is before of after input. Not even editlist will fix it. – JasonXA Mar 22 '20 at 16:41
  • @JasonXA this is an old question. There was no better option at that time and reencoding was the only option. I didn't check newer versions. Write your better solution as an answer – Behroozfar Mar 22 '20 at 16:56
  • Try to edit your answer... there's no point in answering something that was already answered before. – JasonXA Mar 25 '20 at 20:51
  • @JasonXA there is no accepted answer and i don't have any better option – Behroozfar Mar 25 '20 at 20:55
  • With the latest `ffmpeg n4.2.2` on Arch Linux, only re-encoding worked. [This comment](https://superuser.com/questions/1129396/ffmpeg-ss-t-seeking-output-is-not-accurate-any-workarounds#comment2328458_1131129) didn't work. – ynn Apr 12 '20 at 18:48
  • @Behroozfar, OK, I can't remove the vote unless you edit your answer, my vote is locked. Edit and an update, saying in recent versions copy is supported as well. – JasonXA Apr 12 '20 at 18:49