ViSP  2.6.2
vpVideoWriter.cpp
1 /****************************************************************************
2  *
3  * $Id: vpImagePoint.h 2359 2009-11-24 15:09:25Z nmelchio $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Write image sequences.
36  *
37  * Authors:
38  * Nicolas Melchior
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
48 #include <visp/vpDebug.h>
49 #include <visp/vpVideoWriter.h>
50 
55 {
56  initFileName = false;
57  firstFrame = 0;
58  frameCount = 0;
59 
60  #ifdef VISP_HAVE_FFMPEG
61  ffmpeg = NULL;
62  codec = CODEC_ID_MPEG1VIDEO;
63  bit_rate = 500000;
64  #endif
65 }
66 
67 
72 {
73  #ifdef VISP_HAVE_FFMPEG
74  if (ffmpeg != NULL)
75  delete ffmpeg;
76  #endif
77 }
78 
79 
87 void vpVideoWriter::setFileName(const char *filename)
88 {
89  if (filename == '\0')
90  {
91  vpERROR_TRACE("filename empty ") ;
92  throw (vpImageException(vpImageException::noFileNameError,"filename empty ")) ;
93  }
94 
95  strcpy(this->fileName,filename);
96 
97  formatType = getFormat(fileName);
98 
99  initFileName = true;
100 }
101 
102 
109 {
110  if (!initFileName)
111  {
112  vpERROR_TRACE("The generic filename has to be set");
113  throw (vpImageException(vpImageException::noFileNameError,"filename empty"));
114  }
115 
116  if (formatType == FORMAT_PGM ||
117  formatType == FORMAT_PPM ||
118  formatType == FORMAT_JPEG ||
119  formatType == FORMAT_PNG)
120  {
121  width = I.getWidth();
122  height = I.getHeight();
123  }
124  #ifdef VISP_HAVE_FFMPEG
125  else if (formatType == FORMAT_AVI ||
126  formatType == FORMAT_MPEG ||
127  formatType == FORMAT_MOV)
128  {
129  ffmpeg = new vpFFMPEG;
130  ffmpeg->setBitRate(bit_rate);
131  if(!ffmpeg->openEncoder(fileName, I.getWidth(), I.getHeight(), codec))
132  throw (vpException(vpException::ioError ,"Could not open the video"));
133  }
134 
135  #else
136  else if (formatType == FORMAT_AVI ||
137  formatType == FORMAT_MPEG ||
138  formatType == FORMAT_MOV)
139  {
140  vpERROR_TRACE("To write video files the FFmpeg library has to be installed");
141  throw (vpException(vpException::fatalError ,"the FFmpeg library is required"));
142  }
143  #endif
144 
145  frameCount = firstFrame;
146 
147  isOpen = true;
148 }
149 
150 
157 {
158  if (!initFileName)
159  {
160  vpERROR_TRACE("The generic filename has to be set");
161  throw (vpImageException(vpImageException::noFileNameError,"filename empty"));
162  }
163 
164  if (formatType == FORMAT_PGM ||
165  formatType == FORMAT_PPM ||
166  formatType == FORMAT_JPEG ||
167  formatType == FORMAT_PNG)
168  {
169  width = I.getWidth();
170  height = I.getHeight();
171  }
172  #ifdef VISP_HAVE_FFMPEG
173  else if (formatType == FORMAT_AVI ||
174  formatType == FORMAT_MPEG ||
175  formatType == FORMAT_MOV)
176  {
177  ffmpeg = new vpFFMPEG;
178  ffmpeg->setBitRate(bit_rate);
179  if(!ffmpeg->openEncoder(fileName, I.getWidth(), I.getHeight(), codec))
180  throw (vpException(vpException::ioError ,"Could not open the video"));
181  }
182 
183  #else
184  else if (formatType == FORMAT_AVI ||
185  formatType == FORMAT_MPEG ||
186  formatType == FORMAT_MOV)
187  {
188  vpERROR_TRACE("To write video files the FFmpeg library has to be installed");
189  throw (vpException(vpException::fatalError ,"the FFmpeg library is required"));
190  }
191  #endif
192 
193  frameCount = firstFrame;
194 
195  isOpen = true;
196 }
197 
198 
207 {
208  if (!isOpen)
209  {
210  vpERROR_TRACE("The video has to be open first with the open method");
211  throw (vpException(vpException::notInitialized,"file not yet opened"));
212  }
213 
214 
215  if (formatType == FORMAT_PGM ||
216  formatType == FORMAT_PPM ||
217  formatType == FORMAT_JPEG ||
218  formatType == FORMAT_PNG)
219  {
220  char name[FILENAME_MAX];
221 
222  sprintf(name,fileName,frameCount);
223 
224  vpImageIo::write(I, name);
225  }
226 
227  #ifdef VISP_HAVE_FFMPEG
228  else
229  {
230  ffmpeg->saveFrame(I);
231  }
232  #endif
233 
234  frameCount++;
235 }
236 
237 
246 {
247  if (!isOpen)
248  {
249  vpERROR_TRACE("The video has to be open first with the open method");
250  throw (vpException(vpException::notInitialized,"file not yet opened"));
251  }
252 
253  if (formatType == FORMAT_PGM ||
254  formatType == FORMAT_PPM ||
255  formatType == FORMAT_JPEG ||
256  formatType == FORMAT_PNG)
257  {
258  char name[FILENAME_MAX];
259 
260  sprintf(name,fileName,frameCount);
261 
262  vpImageIo::write(I, name);
263  }
264 
265  #ifdef VISP_HAVE_FFMPEG
266  else
267  {
268  ffmpeg->saveFrame(I);
269  }
270  #endif
271 
272  frameCount++;
273 }
274 
275 
280 {
281  if (!isOpen)
282  {
283  vpERROR_TRACE("The video has to be open first with the open method");
284  throw (vpException(vpException::notInitialized,"file not yet opened"));
285  }
286  #ifdef VISP_HAVE_FFMPEG
287  if (ffmpeg != NULL)
288  {
289  ffmpeg->endWrite();
290  }
291  #endif
292 }
293 
294 
300 vpVideoWriter::vpVideoFormatType
301 vpVideoWriter::getFormat(const char *filename)
302 {
303  std::string sfilename(filename);
304 
305  std::string ext = vpVideoWriter::getExtension(sfilename);
306 
307  if (ext.compare(".PGM") == 0)
308  return FORMAT_PGM;
309  else if (ext.compare(".pgm") == 0)
310  return FORMAT_PGM;
311  else if (ext.compare(".PPM") == 0)
312  return FORMAT_PPM;
313  else if (ext.compare(".ppm") == 0)
314  return FORMAT_PPM;
315  else if (ext.compare(".JPG") == 0)
316  return FORMAT_JPEG;
317  else if (ext.compare(".jpg") == 0)
318  return FORMAT_JPEG;
319  else if (ext.compare(".JPEG") == 0)
320  return FORMAT_JPEG;
321  else if (ext.compare(".jpeg") == 0)
322  return FORMAT_JPEG;
323  else if (ext.compare(".PNG") == 0)
324  return FORMAT_PNG;
325  else if (ext.compare(".png") == 0)
326  return FORMAT_PNG;
327  else if (ext.compare(".AVI") == 0)
328  return FORMAT_AVI;
329  else if (ext.compare(".avi") == 0)
330  return FORMAT_AVI;
331  else if (ext.compare(".MPEG") == 0)
332  return FORMAT_MPEG;
333  else if (ext.compare(".mpeg") == 0)
334  return FORMAT_MPEG;
335  else if (ext.compare(".MPG") == 0)
336  return FORMAT_MPEG;
337  else if (ext.compare(".mpg") == 0)
338  return FORMAT_MPEG;
339  else if (ext.compare(".MOV") == 0)
340  return FORMAT_MOV;
341  else if (ext.compare(".mov") == 0)
342  return FORMAT_MOV;
343  else
344  return FORMAT_UNKNOWN;
345 }
346 
347 // return the extension of the file including the dot
348 std::string vpVideoWriter::getExtension(const std::string &filename)
349 {
350  // extract the extension
351  size_t dot = filename.find_last_of(".");
352  std::string ext = filename.substr(dot, filename.size()-1);
353  return ext;
354 }
static void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:355
unsigned int getWidth() const
Definition: vpImage.h:154
#define vpERROR_TRACE
Definition: vpDebug.h:379
void setBitRate(const unsigned int bit_rate)
Definition: vpFFMPEG.h:231
bool saveFrame(vpImage< vpRGBa > &I)
Definition: vpFFMPEG.cpp:718
Error that can be emited by the vpImage class and its derivates.
bool openEncoder(const char *filename, unsigned int width, unsigned int height, CodecID codec=CODEC_ID_MPEG1VIDEO)
Definition: vpFFMPEG.cpp:642
This class interfaces the FFmpeg library to enable the reading of video files.
Definition: vpFFMPEG.h:154
void saveFrame(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
void setFileName(const char *filename)
unsigned int getHeight() const
Definition: vpImage.h:145
bool endWrite()
Definition: vpFFMPEG.cpp:763