Skip to content

[bugfix] Raise VideoOpenFailure for input files without a video stream - #631

Open
flavio-cesar wants to merge 3 commits into
Breakthrough:mainfrom
flavio-cesar:fix/no-video-stream
Open

flavio-cesar wants to merge 3 commits into
Breakthrough:mainfrom
flavio-cesar:fix/no-video-stream

Conversation

@flavio-cesar

@flavio-cesar flavio-cesar commented Nov 28, 2026 •

Copy link
Copy Markdown

Fixes #630

This started with a problem in my video editing pipeline, which uses open_video, SceneManager and save_images. Testing the same calls with clips from my own videos, plus edge cases made from them, showed that files with no video stream (e.g. audio only) aren't rejected when they're opened:

Changes

  • VideoStreamAv raises VideoOpenFailure if there's no video stream. VideoOpenFailure is also re-raised as is, like OSError, so it isn't wrapped in a second one.
  • VideoStreamCv2 raises VideoOpenFailure if the input was opened with MSMF and its width or height is 0. Other backends aren't checked, because they can report 0x0 for valid videos (e.g. an MPEG-TS file where the video starts late).
  • The MSMF check also covers URLs and names with %, but not camera device IDs.
  • On Windows, the CLI now shows a usage error (exit code 2) for audio-only files instead of a traceback.

As requested, the changelog entry only covers PyAV.

Why keep the OpenCV check for MSMF

Without it, when PyAV rejects an audio-only file, open_video falls back to OpenCV, which hangs on Windows.

If the check only covered regular files (input_is_video_file), open_video("100% audio.m4a", backend="pyav") would go from an IndexError on main to a hang. That's why it also covers URLs and names with %.

I compared four versions using the same 43 cases, with valid, audio-only and damaged files, local URLs and a server that never responds:

Version Hangs on Windows
main 9
PyAV fix only, without the OpenCV check 12
MSMF check for regular files only 6
This PR 3

The counts were the same on windows-latest with Python 3.12 and 3.14 (OpenCV 5.1.0, PyAV 19.0.2). Each case ran once per version, in its own process. I counted it as a hang if it didn't finish within 20s for files, 30s for URLs or 100s for the server that never responds. Nothing hung on Ubuntu (Python 3.10, PyAV 17.1.0) or macOS (Python 3.14, PyAV 19.0.2). Whenever main read frames, this PR got the same number of decoded images.

OpenCV 5.1.0 opens my MPEG-TS file where the video starts at 12 seconds, but doesn't return any images from it in any of the four versions. PyAV reads all 100.

Some runs also crashed, but not because of this PR. With PyAV 17.1.0 on Ubuntu, a video stream with no decoder crashed all four versions. On Windows, two runs crashed on exit after reading that MPEG-TS file: one with main and one with the version that only checks regular files.

This comparison used main at 6144f21. The only commit since then, 7136a77, doesn't change any code.

What can still hang

These cases also hang on main:

  • A damaged MP4, whether it's opened with OpenCV or with PyAV, which falls back to OpenCV (2 of the 3 hangs)
  • A URL on a server that accepts the connection but never responds (the other one)

VideoCaptureAdapter isn't covered by this check either, so it can still hang with an MSMF capture.

I also tried skipping MSMF whenever FFmpeg was available. Nothing hung in the same 43 cases, but it rejected two inputs that main can read on Windows: an MP4 with a PNG cover before its tracks, and a video on a server that blocks FFmpeg's user agent. I didn't want to fix one problem by creating another, so that approach isn't in this PR.

Tests

  • test_no_video_stream opens a short WAV file made with the wave module. VideoStreamCv2 is skipped on Windows, with a reference to videoio(MSMF): VideoCapture opens files without a video stream, grab() spins burning a CPU core (denial of service) opencv/opencv#31215. MoviePy is skipped because it already raises OSError.
  • test_zero_frame_size_is_only_rejected_with_msmf uses a mocked capture to check that 0x0 is rejected with MSMF but not with FFmpeg. It covers a regular file, a name with % and a URL. Real MSMF is only covered by the comparison above and the checks on my PC below.
  • On main, 4 of the new test cases fail: the 3 MSMF ones and the PyAV one.
  • On 7136a77, the full suite had 775 passed, 4 skipped and no failures on each of Ubuntu (Python 3.10), macOS (Python 3.14) and Windows (Python 3.14). After those runs, I only changed a line break in a test docstring.
  • On my PC (Windows 11, Python 3.10, OpenCV 5.1.0, PyAV 17.1.0, MoviePy 2.2.1), the final version had the same result with pytest -vv (775 passed, 4 skipped). ruff check, ruff format --check and pyright also passed.
  • The final version was also checked on my PC with generated test files. Audio-only files, including one with % in its name, raised VideoOpenFailure with both OpenCV and PyAV. The CLI exited with code 2 with both backends. OpenCV, PyAV and MoviePy each read all 100 frames from a valid video.
  • fb2fcd3 was also compared with main (7136a77) using two 20s clips from my own videos, plus a rotated version and one with a variable frame rate. With OpenCV and PyAV, the CRC of all decoded frames, the detected scenes and the saved images were the same. Two audio-only files made from the clips raised VideoOpenFailure with both backends.

AI use

I used Claude Code as a tool to speed up the investigation, coding, tests, and also to help write and translate my messages here
All the technical decisions and what went into this PR are mine
After your review pointed out the first fix could reject valid videos, I dug deeper and rejected the option of skipping MSMF since that caused another issue
I also made sure we ran real checks on Linux and macOS instead of guessing
Everything was reviewed by me before pushing this update, including the code, test results, and these notes


import os.path
import typing as ty
import wave

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL there's a wave module in the standard library!

Breakthrough

This comment was marked as outdated.

@Breakthrough Breakthrough added this to the 0.7.4 milestone Nov 28, 2026
Comment thread scenedetect/backends/opencv.py Outdated
# Some capture backends (e.g. MSMF on Windows) open files without a video stream, such as
# audio-only files, with a frame size of 0x0. Reading from them can block, so reject them.
if input_is_video_file and (
cap.get(cv2.CAP_PROP_FRAME_WIDTH) <= 0 or cap.get(cv2.CAP_PROP_FRAME_HEIGHT) <= 0

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this check guaranteed to work? I recall having some issues previously where the reported width/height from the container metadata was not consistent with the actual frames being returned.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to make sure we clarify this before merging - I'm also curious how this might work for other inputs like image sequences/etc or if this can possibly break things.

As an alternative, we could just emit a warning here, and then throw an exception when we try to read the first frame there.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed this will cause issues, things like MPEG TS streams can report zero for frame width/height here, and it's easy to construct such inputs. We can't leverage this check alone. I also found an OpenCV bug here that's quite relevant to your report: opencv/opencv#31215

Let's think some more about how to address this. One solution would be to limit the check when cap.getBackendName() == "MSMF" holds. For now I would suggest landing only the PyAV fix in this PR, and submit another one for the OpenCV issue and reference the bug I filed in it as well.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You were right, 0x0 isn't enough on its own. Now the check only runs when getBackendName() returns MSMF, so valid videos that other backends open as 0x0 aren't rejected by it anymore

I kept both fixes in this PR, so the PyAV fix doesn't send audio-only files to the OpenCV hang. The full test results are in #630

_ = vs_type("this_path_should_not_exist.mp4")


def test_no_video_stream(vs_type: ty.Callable[..., VideoStream], tmp_path):

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once the OpenCV check is removed, MSMF on Windows will probably open this file again. Can you:

Thanks for addressing this, other than this LGTM!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, VideoStreamCv2 is now skipped here on Windows, with a link to opencv/opencv#31215, and the changelog only covers PyAV

I kept the OpenCV check only for MSMF, like you said in #630. Without it, audio-only files opened with PyAV fall back to OpenCV and hang on Windows

Since this test is skipped there, I also added one with a mocked capture. It checks that 0x0 is rejected with MSMF but not with FFmpeg

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Files without a video stream aren't rejected on open (IndexError with PyAV, AssertionError with OpenCV on Windows)

2 participants