[Bugfix] Video loading: sample over presentable frames, not header sample count (MP4 edit-list trims) (#48608)

Signed-off-by: AmitMY <amitmoryossef@gmail.com>
Signed-off-by: Isotr0py <Isotr0py@outlook.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: Isotr0py <Isotr0py@outlook.com>
This commit is contained in:
Amit Moryossef
2026-08-20 04:23:09 +00:00
committed by GitHub
co-authored by Claude Fable 5 Isotr0py
parent 11baa0ebd8
commit 9b2aef5124
4 changed files with 117 additions and 1 deletions
+21
View File
@@ -78,6 +78,27 @@ class OpenCVVideoBackendMixin:
def get_video_metadata(cap: "cv2.VideoCapture") -> VideoSourceMetadata:
total_frames_num = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
original_fps = cap.get(cv2.CAP_PROP_FPS)
# CAP_PROP_FRAME_COUNT counts every physical sample in the container,
# overstating the presentable frames when an mp4 edit list hides the
# decode lead-in (e.g. a lossless `ffmpeg -ss ... -c copy` trim).
# Cross-check with an end-of-stream seek: the FFMPEG backend reports
# the resulting position against the edit-list-aware timeline, which
# exposes the true frame count without decoding anything. The seek
# may land mid-stream on corrupt files, so only trust it when the
# stream really ends there. A one-frame tolerance keeps mere header
# miscounts from being "corrected".
if total_frames_num > 0 and cap.set(cv2.CAP_PROP_POS_AVI_RATIO, 1.0):
visible_frames = int(cap.get(cv2.CAP_PROP_POS_FRAMES))
at_stream_end = not cap.grab()
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
if at_stream_end and 0 < visible_frames < total_frames_num - 1:
logger.warning(
"Video header claims %d frames but only %d are "
"presentable; sampling over the true frame count.",
total_frames_num,
visible_frames,
)
total_frames_num = visible_frames
duration = total_frames_num / original_fps if original_fps > 0 else 0
return VideoSourceMetadata(
total_frames_num=total_frames_num,
+8
View File
@@ -62,6 +62,14 @@ class PyAVVideoBackendMixin:
duration = float(stream.duration * stream.time_base) if stream.duration else 0.0
if total_frames == 0 and duration > 0 and fps > 0:
total_frames = int(duration * fps)
elif duration > 0 and fps > 0 and total_frames > round(duration * fps) + 1:
# The header sample count can exceed the frames the presentation
# timeline actually holds — e.g. an mp4 edit list hides the decode
# lead-in of a stream-copied cut, but ``stream.frames`` still
# counts the hidden samples. Sampling indices past the visible
# range would silently collapse onto the last visible frame, so
# trust the (edit-list-aware) duration instead.
total_frames = round(duration * fps)
return VideoSourceMetadata(total_frames, fps, duration)
@staticmethod