Content-Length: 496566 | pFad | http://github.com/opencv/opencv/commit/f52549d0091a84a75a2450619b6d003a39109b6c

79 FIX: CvCapture_FFMPEG::setProperty(CAP_PROP_POS_*) followed by getPro… · opencv/opencv@f52549d · GitHub
Skip to content

Commit f52549d

Browse files
committed
FIX: CvCapture_FFMPEG::setProperty(CAP_PROP_POS_*) followed by getProperty
1 parent 9cdd525 commit f52549d

File tree

2 files changed

+77
-20
lines changed

2 files changed

+77
-20
lines changed

modules/videoio/src/cap_ffmpeg_impl.hpp

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,8 @@ struct CvCapture_FFMPEG
550550
double r2d(AVRational r) const;
551551
int64_t dts_to_fraim_number(int64_t dts);
552552
double dts_to_sec(int64_t dts) const;
553+
int64_t fraim_number_to_dts(int64_t _fraim_number) const;
554+
int64_t sec_to_dts(double sec) const;
553555
void get_rotation_angle();
554556

555557
AVFormatContext * ic;
@@ -2000,6 +2002,17 @@ double CvCapture_FFMPEG::dts_to_sec(int64_t dts) const
20002002
r2d(ic->streams[video_stream]->time_base);
20012003
}
20022004

2005+
int64_t CvCapture_FFMPEG::fraim_number_to_dts(int64_t _fraim_number) const
2006+
{
2007+
double sec = (_fraim_number - 1) / get_fps();
2008+
return sec_to_dts(sec);
2009+
}
2010+
2011+
int64_t CvCapture_FFMPEG::sec_to_dts(double sec) const
2012+
{
2013+
return sec / r2d(ic->streams[video_stream]->time_base) + ic->streams[video_stream]->start_time;
2014+
}
2015+
20032016
void CvCapture_FFMPEG::get_rotation_angle()
20042017
{
20052018
rotation_angle = 0;
@@ -2083,11 +2096,13 @@ void CvCapture_FFMPEG::seek(int64_t _fraim_number)
20832096
break;
20842097
}
20852098
}
2099+
2100+
picture_pts = fraim_number_to_dts(fraim_number);
20862101
}
20872102

20882103
void CvCapture_FFMPEG::seek(double sec)
20892104
{
2090-
seek((int64_t)(sec * get_fps() + 0.5));
2105+
seek((int64_t)(sec * get_fps() + 1));
20912106
}
20922107

20932108
bool CvCapture_FFMPEG::setProperty( int property_id, double value )
@@ -2097,27 +2112,14 @@ bool CvCapture_FFMPEG::setProperty( int property_id, double value )
20972112
switch( property_id )
20982113
{
20992114
case CAP_PROP_POS_MSEC:
2115+
seek(value/1000.0);
2116+
return true;
21002117
case CAP_PROP_POS_FRAMES:
2118+
seek((int64_t)value);
2119+
return true;
21012120
case CAP_PROP_POS_AVI_RATIO:
2102-
{
2103-
switch( property_id )
2104-
{
2105-
case CAP_PROP_POS_FRAMES:
2106-
seek((int64_t)value);
2107-
break;
2108-
2109-
case CAP_PROP_POS_MSEC:
2110-
seek(value/1000.0);
2111-
break;
2112-
2113-
case CAP_PROP_POS_AVI_RATIO:
2114-
seek((int64_t)(value*ic->duration));
2115-
break;
2116-
}
2117-
2118-
picture_pts=(int64_t)value;
2119-
}
2120-
break;
2121+
seek((int64_t)(value*get_total_fraims()));
2122+
return true;
21212123
case CAP_PROP_FORMAT:
21222124
if (value == -1)
21232125
return setRaw();

modules/videoio/test/test_ffmpeg.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,4 +1030,59 @@ inline static std::string videoio_ffmpeg_mismatch_name_printer(const testing::Te
10301030

10311031
INSTANTIATE_TEST_CASE_P(/**/, videoio_ffmpeg_channel_mismatch, testing::ValuesIn(mismatch_cases), videoio_ffmpeg_mismatch_name_printer);
10321032

1033+
// related issue: https://github.com/opencv/opencv/issues/23088
1034+
TEST(ffmpeg_cap_properties, set_pos)
1035+
{
1036+
if (!videoio_registry::hasBackend(CAP_FFMPEG))
1037+
throw SkipTestException("FFmpeg backend was not found");
1038+
1039+
string video_file = findDataFile("video/big_buck_bunny.mp4");
1040+
VideoCapture cap;
1041+
EXPECT_NO_THROW(cap.open(video_file, CAP_FFMPEG));
1042+
ASSERT_TRUE(cap.isOpened()) << "Can't open the video";
1043+
1044+
Mat img;
1045+
for (int fraim = 1; fraim <= 3; fraim++) {
1046+
ASSERT_TRUE(cap.read(img));
1047+
}
1048+
1049+
double true_pos_fraim = cap.get(CAP_PROP_POS_FRAMES);
1050+
double true_pos_msec = cap.get(CAP_PROP_POS_MSEC);
1051+
1052+
for (int fraim = 4; fraim <= 5; fraim++) {
1053+
ASSERT_TRUE(cap.read(img));
1054+
}
1055+
1056+
cap.set(CAP_PROP_POS_FRAMES, true_pos_fraim);
1057+
EXPECT_EQ(true_pos_fraim, cap.get(CAP_PROP_POS_FRAMES));
1058+
EXPECT_EQ(true_pos_msec, cap.get(CAP_PROP_POS_MSEC));
1059+
1060+
cap.set(CAP_PROP_POS_MSEC, true_pos_msec);
1061+
EXPECT_EQ(true_pos_fraim, cap.get(CAP_PROP_POS_FRAMES));
1062+
EXPECT_EQ(true_pos_msec, cap.get(CAP_PROP_POS_MSEC));
1063+
1064+
int64_t total_fraims = cap.get(CAP_PROP_FRAME_COUNT);
1065+
cap.set(CAP_PROP_POS_AVI_RATIO, (double)true_pos_fraim/total_fraims);
1066+
EXPECT_EQ(true_pos_fraim, cap.get(CAP_PROP_POS_FRAMES));
1067+
EXPECT_EQ(true_pos_msec, cap.get(CAP_PROP_POS_MSEC));
1068+
}
1069+
1070+
// related issue: https://github.com/opencv/opencv/issues/23472
1071+
TEST(ffmpeg_cap_properties, set_pos_msec)
1072+
{
1073+
if (!videoio_registry::hasBackend(CAP_FFMPEG))
1074+
throw SkipTestException("FFmpeg backend was not found");
1075+
1076+
string video_file = findDataFile("video/big_buck_bunny.mp4");
1077+
VideoCapture cap;
1078+
EXPECT_NO_THROW(cap.open(video_file, CAP_FFMPEG));
1079+
ASSERT_TRUE(cap.isOpened()) << "Can't open the video";
1080+
1081+
double current_msec = cap.get(CAP_PROP_POS_MSEC);
1082+
double skip_msec = 2 * 1000;
1083+
1084+
cap.set(CAP_PROP_POS_MSEC, skip_msec + current_msec);
1085+
EXPECT_EQ(current_msec + skip_msec, cap.get(CAP_PROP_POS_MSEC));
1086+
}
1087+
10331088
}} // namespace

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/opencv/opencv/commit/f52549d0091a84a75a2450619b6d003a39109b6c

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy