48 #include <visp/vpDebug.h>
49 #include <visp/vpVideoWriter.h>
51 #if VISP_HAVE_OPENCV_VERSION >= 0x020200
52 # include <opencv2/imgproc/imgproc.hpp>
60 #ifdef VISP_HAVE_FFMPEG
62 # if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54,51,110)
63 codec(CODEC_ID_MPEG1VIDEO),
65 codec(AV_CODEC_ID_MPEG1VIDEO),
69 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
70 writer(), fourcc(0), framerate(0.),
72 formatType(FORMAT_UNKNOWN), initFileName(false), isOpen(false), frameCount(0),
73 firstFrame(0), width(0), height(0)
80 #ifdef VISP_HAVE_FFMPEG
82 # if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54,51,110) // libavcodec 54.51.100
83 codec = CODEC_ID_MPEG1VIDEO;
85 codec = AV_CODEC_ID_MPEG1VIDEO;
89 #elif VISP_HAVE_OPENCV_VERSION >= 0x030000
91 fourcc = cv::VideoWriter::fourcc(
'P',
'I',
'M',
'1');
92 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
94 fourcc = CV_FOURCC(
'P',
'I',
'M',
'1');
104 #ifdef VISP_HAVE_FFMPEG
120 if (!filename || *filename ==
'\0')
126 if (strlen( filename ) >= FILENAME_MAX) {
128 "Not enough memory to intialize the file name"));
131 strcpy(this->fileName,filename);
133 formatType = getFormat(fileName);
135 if (formatType == FORMAT_UNKNOWN) {
167 if (formatType == FORMAT_PGM ||
168 formatType == FORMAT_PPM ||
169 formatType == FORMAT_JPEG ||
170 formatType == FORMAT_PNG)
175 else if (formatType == FORMAT_AVI ||
176 formatType == FORMAT_MPEG ||
177 formatType == FORMAT_MPEG4 ||
178 formatType == FORMAT_MOV)
180 #ifdef VISP_HAVE_FFMPEG
187 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
188 writer = cv::VideoWriter(fileName, fourcc, framerate, cv::Size((
int)I.
getWidth(), (int)I.
getHeight()));
190 if(!writer.isOpened())
201 frameCount = firstFrame;
220 if (formatType == FORMAT_PGM ||
221 formatType == FORMAT_PPM ||
222 formatType == FORMAT_JPEG ||
223 formatType == FORMAT_PNG)
228 else if (formatType == FORMAT_AVI ||
229 formatType == FORMAT_MPEG ||
230 formatType == FORMAT_MPEG4 ||
231 formatType == FORMAT_MOV)
233 #ifdef VISP_HAVE_FFMPEG
240 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
241 writer = cv::VideoWriter(fileName, fourcc, framerate, cv::Size((
int)I.
getWidth(), (int)I.
getHeight()));
243 if(!writer.isOpened())
254 frameCount = firstFrame;
271 vpERROR_TRACE(
"The video has to be open first with the open method");
276 if (formatType == FORMAT_PGM ||
277 formatType == FORMAT_PPM ||
278 formatType == FORMAT_JPEG ||
279 formatType == FORMAT_PNG)
281 char name[FILENAME_MAX];
283 sprintf(name,fileName,frameCount);
289 #ifdef VISP_HAVE_FFMPEG
291 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
313 vpERROR_TRACE(
"The video has to be open first with the open method");
317 if (formatType == FORMAT_PGM ||
318 formatType == FORMAT_PPM ||
319 formatType == FORMAT_JPEG ||
320 formatType == FORMAT_PNG)
322 char name[FILENAME_MAX];
324 sprintf(name,fileName,frameCount);
330 #ifdef VISP_HAVE_FFMPEG
332 #elif VISP_HAVE_OPENCV_VERSION >= 0x030000
333 cv::Mat matFrame, rgbMatFrame;
335 cv::cvtColor(matFrame, rgbMatFrame, cv::COLOR_GRAY2BGR);
336 writer << rgbMatFrame;
337 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
338 cv::Mat matFrame, rgbMatFrame;
340 cv::cvtColor(matFrame, rgbMatFrame, CV_GRAY2BGR);
341 writer << rgbMatFrame;
356 vpERROR_TRACE(
"The video has to be open first with the open method");
359 #ifdef VISP_HAVE_FFMPEG
373 vpVideoWriter::vpVideoFormatType
374 vpVideoWriter::getFormat(
const char *filename)
376 std::string sfilename(filename);
378 std::string ext = vpVideoWriter::getExtension(sfilename);
380 if (ext.compare(
".PGM") == 0)
382 else if (ext.compare(
".pgm") == 0)
384 else if (ext.compare(
".PPM") == 0)
386 else if (ext.compare(
".ppm") == 0)
388 else if (ext.compare(
".JPG") == 0)
390 else if (ext.compare(
".jpg") == 0)
392 else if (ext.compare(
".JPEG") == 0)
394 else if (ext.compare(
".jpeg") == 0)
396 else if (ext.compare(
".PNG") == 0)
398 else if (ext.compare(
".png") == 0)
400 else if (ext.compare(
".AVI") == 0)
402 else if (ext.compare(
".avi") == 0)
404 else if (ext.compare(
".MPEG") == 0)
406 else if (ext.compare(
".mpeg") == 0)
408 else if (ext.compare(
".MPG") == 0)
410 else if (ext.compare(
".mpg") == 0)
412 else if (ext.compare(
".MPEG4") == 0)
414 else if (ext.compare(
".mpeg4") == 0)
416 else if (ext.compare(
".MP4") == 0)
418 else if (ext.compare(
".mp4") == 0)
420 else if (ext.compare(
".MOV") == 0)
422 else if (ext.compare(
".mov") == 0)
425 return FORMAT_UNKNOWN;
429 std::string vpVideoWriter::getExtension(
const std::string &filename)
432 size_t dot = filename.find_last_of(
".");
433 std::string ext = filename.substr(dot, filename.size()-1);
static void write(const vpImage< unsigned char > &I, const char *filename)
void setBitRate(const unsigned int rate)
unsigned int getWidth() const
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
bool saveFrame(vpImage< vpRGBa > &I)
error that can be emited by ViSP classes.
Error that can be emited by the vpImage class and its derivates.
This class interfaces the FFmpeg library to enable video stream reading or writing.
void saveFrame(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
void setFileName(const char *filename)
unsigned int getHeight() const
bool openEncoder(const char *filename, unsigned int width, unsigned int height, AVCodecID codec=AV_CODEC_ID_MPEG1VIDEO)
void setFramerate(const int framerate)