Skip to content

[backends] Remove genpts option from fflags in PyAV backend #590 - #592

Open
Breakthrough wants to merge 1 commit into
mainfrom
issue-590-pyav-genpts-fix
Open

Breakthrough wants to merge 1 commit into
mainfrom
issue-590-pyav-genpts-fix

Conversation

@Breakthrough

@Breakthrough Breakthrough commented Oct 25, 2026 •

Copy link
Copy Markdown
Owner

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

@Breakthrough Breakthrough self-assigned this Oct 25, 2026
@Breakthrough Breakthrough added this to the 0.7.3 milestone Oct 25, 2026
@Breakthrough Breakthrough linked an issue Oct 25, 2026 that may be closed by this pull request
@Breakthrough
Breakthrough force-pushed the issue-590-pyav-genpts-fix branch 2 times, most recently from c143ffd to fdb87ce Compare October 26, 2026 02:31
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
@Breakthrough
Breakthrough force-pushed the issue-590-pyav-genpts-fix branch from fdb87ce to 8a0991e Compare October 27, 2026 00:01
import pytest

from scenedetect.backends.pyav import MAX_CONSECUTIVE_DECODE_FAILURES, VideoStreamAv, _to_fraction
import scenedetect.backends.pyav as pyav_backend

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add workaround for PyAV genpts flag

2 participants