Repository navigation
[bugfix] Raise VideoOpenFailure for input files without a video stream - #631
flavio-cesar wants to merge 3 commits into
Conversation
|
|
||
| import os.path | ||
| import typing as ty | ||
| import wave |
There was a problem hiding this comment.
TIL there's a wave module in the standard library!
| # 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
Once the OpenCV check is removed, MSMF on Windows will probably open this file again. Can you:
- skip VideoStreamCv2 on Windows in this test
- Add a reference to videoio(MSMF): VideoCapture opens files without a video stream, grab() spins burning a CPU core (denial of service) opencv/opencv#31215 in a comment at the skip mark
- Change the changelog entry to cover PyAV only
Thanks for addressing this, other than this LGTM!
There was a problem hiding this comment.
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
Fixes #630
This started with a problem in my video editing pipeline, which uses
open_video,SceneManagerandsave_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:open_video(path, backend="pyav")raisesIndexError: tuple index out of range. That isn't aVideoOpenFailure, soopen_videodoesn't fall back to OpenCV.detect_scenescan stop on a bareAssertionError.Changes
VideoStreamAvraisesVideoOpenFailureif there's no video stream.VideoOpenFailureis also re-raised as is, likeOSError, so it isn't wrapped in a second one.VideoStreamCv2raisesVideoOpenFailureif 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).%, but not camera device IDs.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_videofalls 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 anIndexErroron 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:
The counts were the same on
windows-latestwith 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:
VideoCaptureAdapterisn'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_streamopens a short WAV file made with thewavemodule.VideoStreamCv2is 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 raisesOSError.test_zero_frame_size_is_only_rejected_with_msmfuses 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.pytest -vv(775 passed, 4 skipped).ruff check,ruff format --checkandpyrightalso passed.%in its name, raisedVideoOpenFailurewith 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.VideoOpenFailurewith 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