Skip to content

Commit

Permalink
Switch to nb_read_frames as source for FrameCount
Browse files Browse the repository at this point in the history
With this FrameCount should be more reliable, since nb_frames may not be
accurate in some cases. nb_read_frames in comparison is more accurate,
but it is more computationally intensive. To mitigate this, ffprobe
should use -threads 0 to make use of multithreading available on host
system.
  • Loading branch information
jurisevo committed Oct 29, 2024
1 parent 73cd54f commit 24b6167
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions internal/tools/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ func FfprobeExtractMetadata(videoFile string) (video.Metadata, error) {

ffprobeArgs := []string{
"-v", "quiet",
"-threads", "0",
"-select_streams", "v",
"-count_frames",
"-of", "json",
"-show_format",
"-show_streams",
Expand All @@ -85,7 +87,7 @@ func FfprobeExtractMetadata(videoFile string) (video.Metadata, error) {
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
BitRate int `json:"bit_rate,omitempty,string"`
FrameCount int `json:"nb_frames,omitempty,string"`
FrameCount int `json:"nb_read_frames,omitempty,string"`
}
// Unmarshal metadata from both "streams" and "format" JSON objects.
meta := &struct {
Expand All @@ -95,11 +97,11 @@ func FfprobeExtractMetadata(videoFile string) (video.Metadata, error) {
if err := json.Unmarshal(out, &meta); err != nil {
return vmeta, fmt.Errorf("FfprobeExtractMetadata() json.Unmarshal: %w", err)
}
logging.Debugf("%s %+v", videoFile, meta)

vmeta = video.Metadata(meta.Streams[0])
// For mkv container Streams does not contain duration, so we have to look into Format.
vmeta.Duration = math.Max(vmeta.Duration, meta.Format.Duration)
logging.Debugf("%s %+v", videoFile, vmeta)

return vmeta, nil
}
Expand Down

0 comments on commit 24b6167

Please sign in to comment.