Skip to content

Commit ca7b26c

Browse files
committed
ffprobe parse keyframes faster without -skip_frame nokey.
1 parent 6f49701 commit ca7b26c

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

hlsvod/probe.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ func ProbeVideo(ctx context.Context, ffprobeBinary string, inputFilePath string)
129129
"-v", "error", // Hide debug information
130130

131131
// video
132-
"-skip_frame", "nokey",
133-
"-show_entries", "frame=pkt_pts_time", // List all I frames
132+
// we could use -skip_frame nokey, but it's extremely slow because it decodes the whole video
133+
"-show_entries", "packet=pts_time,flags",
134134
"-show_entries", "format=duration",
135135
"-show_entries", "stream=duration,width,height",
136136
"-select_streams", "v", // Video stream only, we're not interested in audio
@@ -154,9 +154,10 @@ func ProbeVideo(ctx context.Context, ffprobeBinary string, inputFilePath string)
154154
}
155155

156156
out := struct {
157-
Frames []struct {
158-
PktPtsTime string `json:"pkt_pts_time"`
159-
} `json:"frames"`
157+
Packets []struct {
158+
PtsTime string `json:"pts_time"`
159+
Flags string `json:"flags"`
160+
} `json:"packets"`
160161
Streams []struct {
161162
Width int `json:"width"`
162163
Height int `json:"height"`
@@ -191,17 +192,23 @@ func ProbeVideo(ctx context.Context, ffprobeBinary string, inputFilePath string)
191192
Duration: duration,
192193
}
193194

194-
for _, frame := range out.Frames {
195-
if frame.PktPtsTime == "" {
195+
for _, packet := range out.Packets {
196+
// Skip packets without PtsTime.
197+
if packet.PtsTime == "" || packet.PtsTime == "N/A" {
196198
continue
197199
}
198200

199-
pktPtsTime, err := strconv.ParseFloat(frame.PktPtsTime, 64)
201+
// We're only interested in key frames.
202+
if len(packet.Flags) > 0 && packet.Flags[0] != 'K' {
203+
continue
204+
}
205+
206+
ptsTime, err := strconv.ParseFloat(packet.PtsTime, 64)
200207
if err != nil {
201208
return nil, err
202209
}
203210

204-
data.PktPtsTime = append(data.PktPtsTime, pktPtsTime)
211+
data.PktPtsTime = append(data.PktPtsTime, ptsTime)
205212
}
206213

207214
return &data, nil

0 commit comments

Comments
 (0)