From 24b61675bc6db89cfc9921fa53b799e7df478eb9 Mon Sep 17 00:00:00 2001 From: Juris Bune Date: Thu, 24 Oct 2024 18:24:21 +0300 Subject: [PATCH] Switch to nb_read_frames as source for FrameCount 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. --- internal/tools/ffmpeg.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/internal/tools/ffmpeg.go b/internal/tools/ffmpeg.go index ccfefb2..ce34f84 100644 --- a/internal/tools/ffmpeg.go +++ b/internal/tools/ffmpeg.go @@ -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", @@ -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 { @@ -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 }