Skip to content

Add Frame.best_effort_timestamp - #2478

Merged
WyattBlue merged 1 commit into
masterfrom
best-effort-timestamp
Oct 25, 2026
Merged

WyattBlue merged 1 commit into
masterfrom
best-effort-timestamp

Conversation

@WyattBlue

Copy link
Copy Markdown
Member

For inputs that store only DTS, such as AVI, the PTS that genpts generates can go backwards when the video has B-frames. Expose FFmpeg's own estimate, which falls back to DTS when PTS is unreliable, and document that input containers set genpts and how to turn it off.

Closes #2477

For inputs that store only DTS, such as AVI, the PTS that `genpts`
generates can go backwards when the video has B-frames. Expose FFmpeg's
own estimate, which falls back to DTS when PTS is unreliable, and
document that input containers set `genpts` and how to turn it off.

Closes #2477
@WyattBlue
WyattBlue merged commit 54ee662 into master Oct 25, 2026
9 checks passed
@WyattBlue
WyattBlue deleted the best-effort-timestamp branch October 25, 2026 13:04
@Breakthrough

Copy link
Copy Markdown

Thanks for the prompt fix here!

Breakthrough added a commit to Breakthrough/PySceneDetect that referenced this pull request Oct 27, 2026
PyAV sets `AVFMT_FLAG_GENPTS` on every input (see PyAV-Org/PyAV#2477 / PyAV-Org/PyAV#2478).

On containers that store DTS only (AVI, MPEG-TS) and contain B-frames, or streams that have no
timestamps at all, the generated PTS can come out in the incorrect decode order. This was
causing the PyAV backend to report non-monotonic timestamps in these cases, resulting in
incorrect splits (#590).

This PR now ensures we set `fflags=-genpts` to override the `+genpts` flag from PyAV. We also
fallback from PTS to DTS, and count frames when there are no timestamps entirely. We also add
support for when PyAV exposes `Frame.best_effort_timestamp` in a future release (tested locally
with the development changes linked above). Lastly, a one-time warning is logged if timestamps
are detected going backwards. Users hitting this issue should update PyAV to 19.1+ to resolve it.

We cannot pin to PyAV 19.1+ because it requires Python 3.12, but `scenedetect` supports 3.10+
and 3.11 is supported until October 2027.

Follow-ups kept as TODOs:
 - AVI tick rate and frame counts in `_get_duration`
 - MPEG-TS seeks that land after the target position
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unconditionally adding AVFMT_FLAG_GENPTS causes non-monotonic PTS values on DTS-only inputs

2 participants