From 4c024c35fbc7f0610501e087a9ef20c336a75e2b Mon Sep 17 00:00:00 2001 From: Sachin Shah <39803835+inventshah@users.noreply.github.com> Date: Mon, 14 Jul 2025 04:59:44 -0400 Subject: [PATCH] Merge pull request #27523 from inventshah:fix-set-then-get-pos FIX: CvCapture_FFMPEG::setProperty(CAP_PROP_POS_*) followed by getProperty #27523 Partially fixes #23088 and #23472. This PR fixes `get(CAP_PROP_POS_MSEC)` calls after a `set(CAP_PROP_POS_*)` without calling `read` first. Since `seek` calls `grabFrame` which already sets `picture_pts`, manually setting `picture_pts` anywhere else in the call stack should not be necessary (except for the special case of seeking to frame 0). Minimal example from #23088 ```cpp for(int i = 0; i < 3; i++) cap.read(img); printf("at: %f frames, %f msec\n", cap.get(CAP_PROP_POS_FRAMES), cap.get(CAP_PROP_POS_MSEC)); cap.set(CAP_PROP_POS_FRAMES, 3); printf("at: %f frames, %f msec\n", cap.get(CAP_PROP_POS_FRAMES), cap.get(CAP_PROP_POS_MSEC)); ``` Current ```txt at: 3.000000 frames, 80.000000 msec at: 3.000000 frames, 0.234375 msec ``` PR ```txt at: 3.000000 frames, 80.000000 msec at: 3.000000 frames, 80.000000 msec ``` It similarly helps with `CAP_PROP_POS_MSEC`: Current ```txt at: 3.000000 frames, 80.000000 msec at: 2.000000 frames, 6.250000 msec ``` PR ```txt at: 3.000000 frames, 80.000000 msec at: 2.000000 frames, 40.000000 msec ``` Note the seek operation is still inconsistent between the `CAP_PROP_POS_*` options as mentioned by #23088, and VFR video seeking has issues discussed in #9053. For fixed-frame rate video, we could change 0.5 to 1 in `void CvCapture_FFMPEG::seek(double sec);` to align `CAP_PROP_POS_MSEC` with `CAP_PROP_POS_FRAMES`, but `CAP_POS_AVI_RATIO` and VFR video would still be broken. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake --- modules/videoio/src/cap_ffmpeg_impl.hpp | 26 +++++++----------------- modules/videoio/test/test_ffmpeg.cpp | 27 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/modules/videoio/src/cap_ffmpeg_impl.hpp b/modules/videoio/src/cap_ffmpeg_impl.hpp index 9cd20fbf77..489dbe565d 100644 --- a/modules/videoio/src/cap_ffmpeg_impl.hpp +++ b/modules/videoio/src/cap_ffmpeg_impl.hpp @@ -2080,6 +2080,7 @@ void CvCapture_FFMPEG::seek(int64_t _frame_number) else { frame_number = 0; + picture_pts = AV_NOPTS_VALUE_; break; } } @@ -2097,27 +2098,14 @@ bool CvCapture_FFMPEG::setProperty( int property_id, double value ) switch( property_id ) { case CAP_PROP_POS_MSEC: + seek(value/1000.0); + return true; case CAP_PROP_POS_FRAMES: + seek((int64_t)value); + return true; case CAP_PROP_POS_AVI_RATIO: - { - switch( property_id ) - { - case CAP_PROP_POS_FRAMES: - seek((int64_t)value); - break; - - case CAP_PROP_POS_MSEC: - seek(value/1000.0); - break; - - case CAP_PROP_POS_AVI_RATIO: - seek((int64_t)(value*ic->duration)); - break; - } - - picture_pts=(int64_t)value; - } - break; + seek((int64_t)(value*ic->duration)); + return true; case CAP_PROP_FORMAT: if (value == -1) return setRaw(); diff --git a/modules/videoio/test/test_ffmpeg.cpp b/modules/videoio/test/test_ffmpeg.cpp index eeb0835078..3021bdfa4e 100644 --- a/modules/videoio/test/test_ffmpeg.cpp +++ b/modules/videoio/test/test_ffmpeg.cpp @@ -1030,4 +1030,31 @@ inline static std::string videoio_ffmpeg_mismatch_name_printer(const testing::Te INSTANTIATE_TEST_CASE_P(/**/, videoio_ffmpeg_channel_mismatch, testing::ValuesIn(mismatch_cases), videoio_ffmpeg_mismatch_name_printer); +// PR: https://github.com/opencv/opencv/pull/27523 +// TODO: Enable the tests back on Windows after FFmpeg plugin rebuild +#ifndef _WIN32 + +// related issue: https://github.com/opencv/opencv/issues/23088 +TEST(ffmpeg_cap_properties, set_pos_get_msec) +{ + if (!videoio_registry::hasBackend(CAP_FFMPEG)) + throw SkipTestException("FFmpeg backend was not found"); + + string video_file = findDataFile("video/big_buck_bunny.mp4"); + VideoCapture cap; + EXPECT_NO_THROW(cap.open(video_file, CAP_FFMPEG)); + ASSERT_TRUE(cap.isOpened()) << "Can't open the video"; + + cap.set(CAP_PROP_POS_FRAMES, 25); + EXPECT_EQ(cap.get(CAP_PROP_POS_MSEC), 1000.0); + + cap.set(CAP_PROP_POS_MSEC, 525); + EXPECT_EQ(cap.get(CAP_PROP_POS_MSEC), 500.0); + + cap.set(CAP_PROP_POS_AVI_RATIO, 0); + EXPECT_EQ(cap.get(CAP_PROP_POS_MSEC), 0.0); +} + +#endif // WIN32 + }} // namespace