2

I want to encode after seeking to a certain position, and i want to make the first frame a key-frame, here the command i used:

ffmpeg -ss 300  -i howimet.mp4 -acodec libfaac -ar 48000 -ab 128k -ac 2  -vcodec libx264 -vf "scale=480:270" -f mpegts -force_key_frames 300 -t 120 howimet2.ts

the -force_key_frames is set to seek position to make a key frame there. I use the following script (from here) to check if the first frame is key-frame

ffprobe -show_frames -v quiet howimet2.ts | awk -F= '   /pict_type=/ { if (index($2, "I")) { i=1; } else { i=0; } } 
  /pkt_pts_time/ { if (i && ($2 >= 0)) print $2; }  
' | head -n 1

The result show the first key-frame is not locate at second 0.

I guess my command is not correct. what am I missing?

jackode
  • 457
  • 2
  • 6
  • 11

3 Answers3

5

When encoding video, the first frame has to be a keyframe. It will be the first one fully encoded, and subsequent frames may use it for inter-frame prediction. Also, at the beginning of the coded video sequence, you will have an H.264 access unit that tells the decoder to refresh.

So, regardless of what you're doing: Unless you just copy the bitstream, you're re-encoding the video and your first frame has to be a keyframe.

Now, for whatever reason your stream has an offset in its start time. This means that all the presentation timestamps are also shifted according to this offset. If you inspect the head of the ffprobe -show_frames output, you'll see that frame 0 will indeed be a keyframe, but with a different PTS.

To compensate for this, you can subtract the start time from all PTS.

slhck
  • 235,242
1

Like mentioned, the first frame of your video has to be an I-Frame. Your issue comes from not the encoding (or copy if that's your flavor) but from the decoding. Having -ss before the input is inexact so ffmpeg is doing the best it can to get you to about where you wanted. Ffmpeg passed that location and thus tells you a non-zero first timestamp.

I would recommend trying a second -ss after your -i. Along the lines -ss 299 -i input -ss 1.

This says go to about the location you wanted, then decode for 1 second before processing. I am not sure if this will fix your pts issue but hopefully it will move you in the right direction.

To point out, I don't think the force_key_frames command is doing what you think it is. If its doing anything, it's likely just telling it to add a key frame in the output every 300 seconds but that is just a guess. Meaning only the first frame of your output would be a key frame (t=120).

https://ffmpeg.org/ffmpeg.html

dstob
  • 313
0

Set the GOP size with "-g"

Just to demonstrate accuracy, here's 1 second segments

ffmpeg -i  in.mkv -g 30  -hls_time 1 -hls_list_size 0 index.m3u8

Here's what the m3u8 looks like :

    #EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:2
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:1.001000,
index0.ts
#EXTINF:1.001000,
index1.ts
#EXTINF:1.001000,
index2.ts
#EXTINF:1.001000,
index3.ts
#EXTINF:1.001000,
index4.ts
#EXTINF:1.001000,
index5.ts
#EXTINF:1.001000,
index6.ts
#EXTINF:1.001000,
index7.ts
#EXTINF:1.001000,
index8.ts
#EXTINF:1.001000,
index9.ts
#EXTINF:1.001000,
index10.ts
#EXTINF:1.001000,
index11.ts
#EXTINF:1.001000,
index12.ts
#EXTINF:1.001000,
index13.ts
#EXTINF:1.001000,
....