1

I am trying to use to the concat protocol but I have troubles with keyframes

I use this ffprobe command to visualize the keyframes pts of my videos:

ffprobe -loglevel error -select_streams v:0 -show_entries packet=pts_time,flags -of csv=print_section=0 inout.mp4 | awk -F',' '/K/ {print $1}'

Here is my process: I have the original video, with these keyframes:

0.000000 5.000000 9.760000 14.240000 18.440000 ...

I trim the first 5 seconds, so the output has these keyframes:

0.000000 4.760000 9.240000 13.440000 ...

I create a 5 seconds video by looping an image, with the same parameters/codec... that were used to produce the original video. This video just has one keyframe at 0.000000

Then I concat this video with the trimmed video like this:

ffmpeg -f concat -safe 0 -i list.txt -c copy -shortest output.mp4 -y

But when I look for the keyframes of the output video, I get this:

0.021016 5.035000 9.795000 14.275000 ...

Is this behavior normal? Should I add a parameter to handle keyframes during concatenation? Thanks

Rems
  • 106
  • 7
  • 1
    Why is PTS so important here? I mean the video plays correctly no matter what those numbers say, right? Or maybe not?.... If this was my problem I would try to concat at the **codec** level and not at the **container** level. This means: **(1)** Trim the first 5 seconds of original video and output as `.h264`. **(2)** The image then also makes a 5-second `.h264` file **(3)** Concat both H264 files first and then mux that single combined H264 file (of 10 seconds) into a single MP4 (using `codec copy` option). When muxing, also set input/output frame rate). See if you get the result you want. – VC.One Jan 13 '23 at 17:34
  • 1
    PTS is important because if I need to trim this video again, it will trim at 0.02, My function trims at the nearest lower keyframe from 5 seconds. So I need to have a keyframe at 5.0s Actually, I found that the issue comes from the video created from looping the image, it seems that even though I specified to loop the image for 5 seconds, the ouput video is a little longer – Rems Jan 16 '23 at 08:41
  • 1
    Glad its solved by simply setting a shorter output. **PS:** Removing `-shortest` might work too (but is an untested idea). **PPS** The logic of working at codec level is that H264 doesn't normally have a frame rate (unless its VUI parameter for timing is added). The framerate is then set inside the container (eg: at MP4 level) so you would control how many H264 frames are output to make expected total of 5 seconds at your chosen FPS. Then pack into the MP4 with additional setting of the chosen FPS. – VC.One Jan 16 '23 at 09:57
  • Ty, but it doesn't seem to be a frame issue, but rather the issue seems to come from the silent audio stream I add. When I loop the image, I need to create an empty audio stream, because a concatenation between a video that has an audio stream and a video that doesn't results in an ouput video which doesn't possess audio So I use : -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=48000 to create an empty audio stream, but somehow it makes the video longer (5.14s, instead of 5.0s when I don't add the audio stream). – Rems Jan 16 '23 at 12:27

0 Answers0