From f1c16f42b45f93498ed374d5a919c32b0c467fe3 Mon Sep 17 00:00:00 2001 From: Vladislav Sovrasov Date: Wed, 14 Jun 2017 17:09:32 +0300 Subject: [PATCH] video: add one more constructor for VideoWriter --- modules/videoio/include/opencv2/videoio.hpp | 21 +++- .../include/opencv2/videoio/videoio_c.h | 3 +- modules/videoio/src/cap.cpp | 98 ++++++++++++------- modules/videoio/src/precomp.hpp | 4 + 4 files changed, 87 insertions(+), 39 deletions(-) diff --git a/modules/videoio/include/opencv2/videoio.hpp b/modules/videoio/include/opencv2/videoio.hpp index bb6ad2a508..d480624b98 100644 --- a/modules/videoio/include/opencv2/videoio.hpp +++ b/modules/videoio/include/opencv2/videoio.hpp @@ -114,7 +114,8 @@ enum VideoCaptureAPIs { CAP_GSTREAMER = 1800, //!< GStreamer CAP_FFMPEG = 1900, //!< Open and record video file or stream using the FFMPEG library CAP_IMAGES = 2000, //!< OpenCV Image Sequence (e.g. img_%02d.jpg) - CAP_ARAVIS = 2100 //!< Aravis SDK + CAP_ARAVIS = 2100, //!< Aravis SDK + CAP_OCV_MJPEG = 2200 //!< Built-in MotionJPEG codec }; /** @brief %VideoCapture generic properties identifier. @@ -849,6 +850,13 @@ public: CV_WRAP VideoWriter(const String& filename, int fourcc, double fps, Size frameSize, bool isColor = true); + /** @overload + The `apiPreference` parameter allows to specify API backends to use. Can be used to enforce a specific reader implementation + if multiple are available: e.g. cv::CAP_FFMPEG or cv::CAP_GSTREAMER. + */ + CV_WRAP VideoWriter(int apiPreference, const String& filename, int fourcc, double fps, + Size frameSize, bool isColor = true); + /** @brief Default destructor The method first calls VideoWriter::release to close the already opened file. @@ -866,6 +874,17 @@ public: CV_WRAP virtual bool open(const String& filename, int fourcc, double fps, Size frameSize, bool isColor = true); + /** @brief Initializes or reinitializes video writer. + + The method opens video writer. Parameters are the same as in the constructor + VideoWriter::VideoWriter. + @return `true` if video writer has been successfully initialized + + The method first calls VideoWriter::release to close the already opened file. + */ + CV_WRAP bool open(int apiPreference, const String& filename, int fourcc, double fps, + Size frameSize, bool isColor = true); + /** @brief Returns true if video writer has been successfully initialized. */ CV_WRAP virtual bool isOpened() const; diff --git a/modules/videoio/include/opencv2/videoio/videoio_c.h b/modules/videoio/include/opencv2/videoio/videoio_c.h index 024633c8b9..48581ddec4 100644 --- a/modules/videoio/include/opencv2/videoio/videoio_c.h +++ b/modules/videoio/include/opencv2/videoio/videoio_c.h @@ -124,7 +124,8 @@ enum CV_CAP_FFMPEG = 1900, // FFMPEG CV_CAP_IMAGES = 2000, // OpenCV Image Sequence (e.g. img_%02d.jpg) - CV_CAP_ARAVIS = 2100 // Aravis GigE SDK + CV_CAP_ARAVIS = 2100, // Aravis GigE SDK + CV_CAP_OCV_MJPEG = 2200 // Built-in MotionJPEG codec }; /** @brief start capturing frames from camera: index = camera_index + domain_offset (CV_CAP_*) diff --git a/modules/videoio/src/cap.cpp b/modules/videoio/src/cap.cpp index f1218fdaa0..95a23e730d 100644 --- a/modules/videoio/src/cap.cpp +++ b/modules/videoio/src/cap.cpp @@ -371,47 +371,59 @@ CV_IMPL CvCapture * cvCreateFileCapture (const char * filename) CV_IMPL CvVideoWriter* cvCreateVideoWriter( const char* filename, int fourcc, double fps, CvSize frameSize, int is_color ) { - // If none of the writers is used - // these statements suppress 'unused parameter' warnings. + return cvCreateVideoWriterWithPreference(CV_CAP_ANY, filename, fourcc, fps, frameSize, is_color); +} + +CvVideoWriter* cvCreateVideoWriterWithPreference(int apiPreference, const char* filename, int fourcc, + double fps, CvSize frameSize, int is_color ) +{ CV_UNUSED(frameSize); CV_UNUSED(is_color); - //CV_FUNCNAME( "cvCreateVideoWriter" ); - CvVideoWriter *result = 0; if(!fourcc || !fps) TRY_OPEN(result, cvCreateVideoWriter_Images(filename)) -#ifdef HAVE_FFMPEG - TRY_OPEN(result, cvCreateVideoWriter_FFMPEG_proxy (filename, fourcc, fps, frameSize, is_color)) -#endif - -#ifdef HAVE_VFW - TRY_OPEN(result, cvCreateVideoWriter_VFW(filename, fourcc, fps, frameSize, is_color)) -#endif - -#ifdef HAVE_MSMF - TRY_OPEN(result, cvCreateVideoWriter_MSMF(filename, fourcc, fps, frameSize, is_color)) -#endif - -/* #ifdef HAVE_XINE - TRY_OPEN(result, cvCreateVideoWriter_XINE(filename, fourcc, fps, frameSize, is_color)) - #endif -*/ -#ifdef HAVE_AVFOUNDATION - TRY_OPEN(result, cvCreateVideoWriter_AVFoundation(filename, fourcc, fps, frameSize, is_color)) -#endif - -#if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT) - TRY_OPEN(result, cvCreateVideoWriter_QT(filename, fourcc, fps, frameSize, is_color)) -#endif - -#ifdef HAVE_GSTREAMER - TRY_OPEN(result, cvCreateVideoWriter_GStreamer(filename, fourcc, fps, frameSize, is_color)) -#endif - - TRY_OPEN(result, cvCreateVideoWriter_Images(filename)) + switch(apiPreference) + { + default: + //exit if the specified API is unavaliable + if (apiPreference != CV_CAP_ANY) break; + #ifdef HAVE_FFMPEG + case CV_CAP_FFMPEG: + TRY_OPEN(result, cvCreateVideoWriter_FFMPEG_proxy (filename, fourcc, fps, frameSize, is_color)) + if (apiPreference != CV_CAP_ANY) break; + #endif + #ifdef HAVE_VFW + case CV_CAP_VFW: + TRY_OPEN(result, cvCreateVideoWriter_VFW(filename, fourcc, fps, frameSize, is_color)) + if (apiPreference != CV_CAP_ANY) break; + #endif + #ifdef HAVE_MSMF + case CV_CAP_MSMF: + TRY_OPEN(result, cvCreateVideoWriter_MSMF(filename, fourcc, fps, frameSize, is_color)) + if (apiPreference != CV_CAP_ANY) break; + #endif + #ifdef HAVE_AVFOUNDATION + case CV_CAP_AVFOUNDATION: + TRY_OPEN(result, cvCreateVideoWriter_AVFoundation(filename, fourcc, fps, frameSize, is_color)) + if (apiPreference != CV_CAP_ANY) break; + #endif + #if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT) + case(CV_CAP_QT): + TRY_OPEN(result, cvCreateVideoWriter_QT(filename, fourcc, fps, frameSize, is_color)) + if (apiPreference != CV_CAP_ANY) break; + #endif + #ifdef HAVE_GSTREAMER + case CV_CAP_GSTREAMER: + TRY_OPEN(result, cvCreateVideoWriter_GStreamer (filename, fourcc, fps, frameSize, is_color)) + if (apiPreference != CV_CAP_ANY) break; + #endif + case CV_CAP_IMAGES: + TRY_OPEN(result, cvCreateVideoWriter_Images(filename)) + if (apiPreference != CV_CAP_ANY) break; + } return result; } @@ -543,10 +555,11 @@ static Ptr IVideoCapture_create(const String& filename) return Ptr(); } -static Ptr IVideoWriter_create(const String& filename, int _fourcc, double fps, Size frameSize, bool isColor) +static Ptr IVideoWriter_create(int apiPreference, const String& filename, int _fourcc, double fps, Size frameSize, bool isColor) { Ptr iwriter; - if( _fourcc == CV_FOURCC('M', 'J', 'P', 'G') ) + if( (apiPreference == CAP_OCV_MJPEG || apiPreference == CAP_ANY) + && _fourcc == CV_FOURCC('M', 'J', 'P', 'G') ) iwriter = createMotionJpegWriter(filename, fps, frameSize, isColor); return iwriter; } @@ -725,6 +738,12 @@ VideoWriter::VideoWriter(const String& filename, int _fourcc, double fps, Size f open(filename, _fourcc, fps, frameSize, isColor); } + +VideoWriter::VideoWriter(int apiPreference, const String& filename, int _fourcc, double fps, Size frameSize, bool isColor) +{ + open(apiPreference, filename, _fourcc, fps, frameSize, isColor); +} + void VideoWriter::release() { iwriter.release(); @@ -737,14 +756,19 @@ VideoWriter::~VideoWriter() } bool VideoWriter::open(const String& filename, int _fourcc, double fps, Size frameSize, bool isColor) +{ + return open(CAP_ANY, filename, _fourcc, fps, frameSize, isColor); +} + +bool VideoWriter::open(int apiPreference, const String& filename, int _fourcc, double fps, Size frameSize, bool isColor) { CV_INSTRUMENT_REGION() if (isOpened()) release(); - iwriter = IVideoWriter_create(filename, _fourcc, fps, frameSize, isColor); + iwriter = IVideoWriter_create(apiPreference, filename, _fourcc, fps, frameSize, isColor); if (!iwriter.empty()) return true; - writer.reset(cvCreateVideoWriter(filename.c_str(), _fourcc, fps, frameSize, isColor)); + writer.reset(cvCreateVideoWriterWithPreference(apiPreference, filename.c_str(), _fourcc, fps, frameSize, isColor)); return isOpened(); } diff --git a/modules/videoio/src/precomp.hpp b/modules/videoio/src/precomp.hpp index 8a10d20498..a63649b76a 100644 --- a/modules/videoio/src/precomp.hpp +++ b/modules/videoio/src/precomp.hpp @@ -163,6 +163,10 @@ CvCapture * cvCreateCameraCapture_Unicap (const int index); CvCapture * cvCreateCameraCapture_PvAPI (const int index); CvVideoWriter* cvCreateVideoWriter_GStreamer( const char* filename, int fourcc, double fps, CvSize frameSize, int is_color ); +CvVideoWriter* cvCreateVideoWriterWithPreference(int api, const char* filename, int fourcc, + double fps, CvSize frame_size, + int is_color CV_DEFAULT(1)); + namespace cv {