Visual Servoing Platform  version 3.1.0
vpVideoWriter.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Write image sequences.
33  *
34  * Authors:
35  * Nicolas Melchior
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
45 #include <visp3/core/vpDebug.h>
46 #include <visp3/io/vpVideoWriter.h>
47 
48 #if VISP_HAVE_OPENCV_VERSION >= 0x020200
49 #include <opencv2/imgproc/imgproc.hpp>
50 #endif
51 
56  :
57 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
58  writer(), fourcc(0), framerate(0.),
59 #endif
60  formatType(FORMAT_UNKNOWN), initFileName(false), isOpen(false), frameCount(0), firstFrame(0), width(0), height(0)
61 {
62  initFileName = false;
63  firstFrame = 0;
64  frameCount = 0;
65  isOpen = false;
66  width = height = 0;
67 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
68  framerate = 25.0;
69  fourcc = cv::VideoWriter::fourcc('P', 'I', 'M', '1');
70 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
71  framerate = 25.0;
72  fourcc = CV_FOURCC('P', 'I', 'M', '1'); // default is a MPEG-1 codec
73 #endif
74 }
75 
80 
92 void vpVideoWriter::setFileName(const char *filename)
93 {
94  if (!filename || *filename == '\0') {
95  vpERROR_TRACE("filename empty ");
96  throw(vpImageException(vpImageException::noFileNameError, "filename empty "));
97  }
98 
99  if (strlen(filename) >= FILENAME_MAX) {
100  throw(vpException(vpException::memoryAllocationError, "Not enough memory to intialize the file name"));
101  }
102 
103  strcpy(this->fileName, filename);
104 
105  formatType = getFormat(fileName);
106 
107  if (formatType == FORMAT_UNKNOWN) {
108  throw(vpException(vpException::badValue, "Filename extension not supported"));
109  }
110 
111  initFileName = true;
112 }
113 
125 void vpVideoWriter::setFileName(const std::string &filename) { setFileName(filename.c_str()); }
126 
133 {
134  if (!initFileName) {
135  vpERROR_TRACE("The generic filename has to be set");
136  throw(vpImageException(vpImageException::noFileNameError, "filename empty"));
137  }
138 
139  if (formatType == FORMAT_PGM || formatType == FORMAT_PPM || formatType == FORMAT_JPEG || formatType == FORMAT_PNG) {
140  width = I.getWidth();
141  height = I.getHeight();
142  } else if (formatType == FORMAT_AVI || formatType == FORMAT_MPEG || formatType == FORMAT_MPEG4 ||
143  formatType == FORMAT_MOV) {
144 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
145  writer = cv::VideoWriter(fileName, fourcc, framerate, cv::Size((int)I.getWidth(), (int)I.getHeight()));
146 
147  if (!writer.isOpened()) {
148  // vpERROR_TRACE("Could not open encode the video with opencv");
149  throw(vpException(vpException::fatalError, "Could not open encode the video with opencv"));
150  }
151 #else
152  throw(vpException(vpException::fatalError, "To encode video files ViSP should be build with "
153  "opencv 3rd >= 2.1.0 party libraries."));
154 #endif
155  }
156 
157  frameCount = firstFrame;
158 
159  isOpen = true;
160 }
161 
168 {
169  if (!initFileName) {
170  vpERROR_TRACE("The generic filename has to be set");
171  throw(vpImageException(vpImageException::noFileNameError, "filename empty"));
172  }
173 
174  if (formatType == FORMAT_PGM || formatType == FORMAT_PPM || formatType == FORMAT_JPEG || formatType == FORMAT_PNG) {
175  width = I.getWidth();
176  height = I.getHeight();
177  } else if (formatType == FORMAT_AVI || formatType == FORMAT_MPEG || formatType == FORMAT_MPEG4 ||
178  formatType == FORMAT_MOV) {
179 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
180  writer = cv::VideoWriter(fileName, fourcc, framerate, cv::Size((int)I.getWidth(), (int)I.getHeight()));
181 
182  if (!writer.isOpened()) {
183  vpERROR_TRACE("Could not encode the video with opencv");
184  throw(vpException(vpException::ioError, "Could not encode the video with opencv"));
185  }
186 #else
187  throw(vpException(vpException::fatalError, "To encode video files ViSP should be build with "
188  "opencv 3rd >= 2.1.0 party libraries."));
189 #endif
190  }
191 
192  frameCount = firstFrame;
193 
194  isOpen = true;
195 }
196 
207 {
208  if (!isOpen) {
209  vpERROR_TRACE("The video has to be open first with the open method");
210  throw(vpException(vpException::notInitialized, "file not yet opened"));
211  }
212 
213  if (formatType == FORMAT_PGM || formatType == FORMAT_PPM || formatType == FORMAT_JPEG || formatType == FORMAT_PNG) {
214  char name[FILENAME_MAX];
215 
216  sprintf(name, fileName, frameCount);
217 
218  vpImageIo::write(I, name);
219  } else {
220 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
221  cv::Mat matFrame;
222  vpImageConvert::convert(I, matFrame);
223  writer << matFrame;
224 #endif
225  }
226 
227  frameCount++;
228 }
229 
240 {
241  if (!isOpen) {
242  vpERROR_TRACE("The video has to be open first with the open method");
243  throw(vpException(vpException::notInitialized, "file not yet opened"));
244  }
245 
246  if (formatType == FORMAT_PGM || formatType == FORMAT_PPM || formatType == FORMAT_JPEG || formatType == FORMAT_PNG) {
247  char name[FILENAME_MAX];
248 
249  sprintf(name, fileName, frameCount);
250 
251  vpImageIo::write(I, name);
252  } else {
253 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
254  cv::Mat matFrame, rgbMatFrame;
255  vpImageConvert::convert(I, matFrame);
256  cv::cvtColor(matFrame, rgbMatFrame, cv::COLOR_GRAY2BGR);
257  writer << rgbMatFrame;
258 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
259  cv::Mat matFrame, rgbMatFrame;
260  vpImageConvert::convert(I, matFrame);
261  cv::cvtColor(matFrame, rgbMatFrame, CV_GRAY2BGR);
262  writer << rgbMatFrame;
263 #endif
264  }
265 
266  frameCount++;
267 }
268 
273 {
274  if (!isOpen) {
275  vpERROR_TRACE("The video has to be open first with the open method");
276  throw(vpException(vpException::notInitialized, "file not yet opened"));
277  }
278 }
279 
285 vpVideoWriter::vpVideoFormatType vpVideoWriter::getFormat(const char *filename)
286 {
287  std::string sfilename(filename);
288 
289  std::string ext = vpVideoWriter::getExtension(sfilename);
290 
291  if (ext.compare(".PGM") == 0)
292  return FORMAT_PGM;
293  else if (ext.compare(".pgm") == 0)
294  return FORMAT_PGM;
295  else if (ext.compare(".PPM") == 0)
296  return FORMAT_PPM;
297  else if (ext.compare(".ppm") == 0)
298  return FORMAT_PPM;
299  else if (ext.compare(".JPG") == 0)
300  return FORMAT_JPEG;
301  else if (ext.compare(".jpg") == 0)
302  return FORMAT_JPEG;
303  else if (ext.compare(".JPEG") == 0)
304  return FORMAT_JPEG;
305  else if (ext.compare(".jpeg") == 0)
306  return FORMAT_JPEG;
307  else if (ext.compare(".PNG") == 0)
308  return FORMAT_PNG;
309  else if (ext.compare(".png") == 0)
310  return FORMAT_PNG;
311  else if (ext.compare(".AVI") == 0)
312  return FORMAT_AVI;
313  else if (ext.compare(".avi") == 0)
314  return FORMAT_AVI;
315  else if (ext.compare(".MPEG") == 0)
316  return FORMAT_MPEG;
317  else if (ext.compare(".mpeg") == 0)
318  return FORMAT_MPEG;
319  else if (ext.compare(".MPG") == 0)
320  return FORMAT_MPEG;
321  else if (ext.compare(".mpg") == 0)
322  return FORMAT_MPEG;
323  else if (ext.compare(".MPEG4") == 0)
324  return FORMAT_MPEG4;
325  else if (ext.compare(".mpeg4") == 0)
326  return FORMAT_MPEG4;
327  else if (ext.compare(".MP4") == 0)
328  return FORMAT_MPEG4;
329  else if (ext.compare(".mp4") == 0)
330  return FORMAT_MPEG4;
331  else if (ext.compare(".MOV") == 0)
332  return FORMAT_MOV;
333  else if (ext.compare(".mov") == 0)
334  return FORMAT_MOV;
335  else
336  return FORMAT_UNKNOWN;
337 }
338 
339 // return the extension of the file including the dot
340 std::string vpVideoWriter::getExtension(const std::string &filename)
341 {
342  // extract the extension
343  size_t dot = filename.find_last_of(".");
344  std::string ext = filename.substr(dot, filename.size() - 1);
345  return ext;
346 }
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
#define vpERROR_TRACE
Definition: vpDebug.h:393
error that can be emited by ViSP classes.
Definition: vpException.h:71
Error that can be emited by the vpImage class and its derivates.
static void write(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:375
void saveFrame(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
void setFileName(const char *filename)
unsigned int getHeight() const
Definition: vpImage.h:178
unsigned int getWidth() const
Definition: vpImage.h:229