44 #include <visp3/core/vpDebug.h>
45 #include <visp3/io/vpVideoWriter.h>
47 #if VISP_HAVE_OPENCV_VERSION >= 0x020200
48 # include <opencv2/imgproc/imgproc.hpp>
56 #ifdef VISP_HAVE_FFMPEG
58 # if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54,51,110)
59 codec(CODEC_ID_MPEG1VIDEO),
61 codec(AV_CODEC_ID_MPEG1VIDEO),
65 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
66 writer(), fourcc(0), framerate(0.),
68 formatType(FORMAT_UNKNOWN), initFileName(false), isOpen(false), frameCount(0),
69 firstFrame(0), width(0), height(0)
76 #ifdef VISP_HAVE_FFMPEG
78 # if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54,51,110) // libavcodec 54.51.100
79 codec = CODEC_ID_MPEG1VIDEO;
81 codec = AV_CODEC_ID_MPEG1VIDEO;
85 #elif VISP_HAVE_OPENCV_VERSION >= 0x030000
87 fourcc = cv::VideoWriter::fourcc(
'P',
'I',
'M',
'1');
88 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
90 fourcc = CV_FOURCC(
'P',
'I',
'M',
'1');
100 #ifdef VISP_HAVE_FFMPEG
116 if (!filename || *filename ==
'\0')
122 if (strlen( filename ) >= FILENAME_MAX) {
124 "Not enough memory to intialize the file name"));
127 strcpy(this->fileName,filename);
129 formatType = getFormat(fileName);
131 if (formatType == FORMAT_UNKNOWN) {
163 if (formatType == FORMAT_PGM ||
164 formatType == FORMAT_PPM ||
165 formatType == FORMAT_JPEG ||
166 formatType == FORMAT_PNG)
171 else if (formatType == FORMAT_AVI ||
172 formatType == FORMAT_MPEG ||
173 formatType == FORMAT_MPEG4 ||
174 formatType == FORMAT_MOV)
176 #ifdef VISP_HAVE_FFMPEG
183 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
184 writer = cv::VideoWriter(fileName, fourcc, framerate, cv::Size((
int)I.
getWidth(), (int)I.
getHeight()));
186 if(!writer.isOpened())
197 frameCount = firstFrame;
216 if (formatType == FORMAT_PGM ||
217 formatType == FORMAT_PPM ||
218 formatType == FORMAT_JPEG ||
219 formatType == FORMAT_PNG)
224 else if (formatType == FORMAT_AVI ||
225 formatType == FORMAT_MPEG ||
226 formatType == FORMAT_MPEG4 ||
227 formatType == FORMAT_MOV)
229 #ifdef VISP_HAVE_FFMPEG
236 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
237 writer = cv::VideoWriter(fileName, fourcc, framerate, cv::Size((
int)I.
getWidth(), (int)I.
getHeight()));
239 if(!writer.isOpened())
250 frameCount = firstFrame;
267 vpERROR_TRACE(
"The video has to be open first with the open method");
272 if (formatType == FORMAT_PGM ||
273 formatType == FORMAT_PPM ||
274 formatType == FORMAT_JPEG ||
275 formatType == FORMAT_PNG)
277 char name[FILENAME_MAX];
279 sprintf(name,fileName,frameCount);
285 #ifdef VISP_HAVE_FFMPEG
287 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
309 vpERROR_TRACE(
"The video has to be open first with the open method");
313 if (formatType == FORMAT_PGM ||
314 formatType == FORMAT_PPM ||
315 formatType == FORMAT_JPEG ||
316 formatType == FORMAT_PNG)
318 char name[FILENAME_MAX];
320 sprintf(name,fileName,frameCount);
326 #ifdef VISP_HAVE_FFMPEG
328 #elif VISP_HAVE_OPENCV_VERSION >= 0x030000
329 cv::Mat matFrame, rgbMatFrame;
331 cv::cvtColor(matFrame, rgbMatFrame, cv::COLOR_GRAY2BGR);
332 writer << rgbMatFrame;
333 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
334 cv::Mat matFrame, rgbMatFrame;
336 cv::cvtColor(matFrame, rgbMatFrame, CV_GRAY2BGR);
337 writer << rgbMatFrame;
352 vpERROR_TRACE(
"The video has to be open first with the open method");
355 #ifdef VISP_HAVE_FFMPEG
369 vpVideoWriter::vpVideoFormatType
370 vpVideoWriter::getFormat(
const char *filename)
372 std::string sfilename(filename);
374 std::string ext = vpVideoWriter::getExtension(sfilename);
376 if (ext.compare(
".PGM") == 0)
378 else if (ext.compare(
".pgm") == 0)
380 else if (ext.compare(
".PPM") == 0)
382 else if (ext.compare(
".ppm") == 0)
384 else if (ext.compare(
".JPG") == 0)
386 else if (ext.compare(
".jpg") == 0)
388 else if (ext.compare(
".JPEG") == 0)
390 else if (ext.compare(
".jpeg") == 0)
392 else if (ext.compare(
".PNG") == 0)
394 else if (ext.compare(
".png") == 0)
396 else if (ext.compare(
".AVI") == 0)
398 else if (ext.compare(
".avi") == 0)
400 else if (ext.compare(
".MPEG") == 0)
402 else if (ext.compare(
".mpeg") == 0)
404 else if (ext.compare(
".MPG") == 0)
406 else if (ext.compare(
".mpg") == 0)
408 else if (ext.compare(
".MPEG4") == 0)
410 else if (ext.compare(
".mpeg4") == 0)
412 else if (ext.compare(
".MP4") == 0)
414 else if (ext.compare(
".mp4") == 0)
416 else if (ext.compare(
".MOV") == 0)
418 else if (ext.compare(
".mov") == 0)
421 return FORMAT_UNKNOWN;
425 std::string vpVideoWriter::getExtension(
const std::string &filename)
428 size_t dot = filename.find_last_of(
".");
429 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)