Merge pull request #8920 from sovrasov:video_vriter_ext

This commit is contained in:
Maksim Shabunin 2017-06-19 10:52:34 +00:00
commit 1f4067112f
4 changed files with 87 additions and 39 deletions

View File

@ -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;

View File

@ -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_*)

View File

@ -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> IVideoCapture_create(const String& filename)
return Ptr<IVideoCapture>();
}
static Ptr<IVideoWriter> IVideoWriter_create(const String& filename, int _fourcc, double fps, Size frameSize, bool isColor)
static Ptr<IVideoWriter> IVideoWriter_create(int apiPreference, const String& filename, int _fourcc, double fps, Size frameSize, bool isColor)
{
Ptr<IVideoWriter> 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();
}

View File

@ -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
{