ViSP  2.7.0
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 - 2013 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 
109 void vpVideoWriter::setFileName(const std::string &filename)
110 {
111  setFileName(filename.c_str());
112 }
113 
120 {
121  if (!initFileName)
122  {
123  vpERROR_TRACE("The generic filename has to be set");
124  throw (vpImageException(vpImageException::noFileNameError,"filename empty"));
125  }
126 
127  if (formatType == FORMAT_PGM ||
128  formatType == FORMAT_PPM ||
129  formatType == FORMAT_JPEG ||
130  formatType == FORMAT_PNG)
131  {
132  width = I.getWidth();
133  height = I.getHeight();
134  }
135  #ifdef VISP_HAVE_FFMPEG
136  else if (formatType == FORMAT_AVI ||
137  formatType == FORMAT_MPEG ||
138  formatType == FORMAT_MOV)
139  {
140  ffmpeg = new vpFFMPEG;
141  ffmpeg->setBitRate(bit_rate);
142  if(!ffmpeg->openEncoder(fileName, I.getWidth(), I.getHeight(), codec))
143  throw (vpException(vpException::ioError ,"Could not open the video"));
144  }
145 
146  #else
147  else if (formatType == FORMAT_AVI ||
148  formatType == FORMAT_MPEG ||
149  formatType == FORMAT_MOV)
150  {
151  vpERROR_TRACE("To write video files the FFmpeg library has to be installed");
152  throw (vpException(vpException::fatalError ,"the FFmpeg library is required"));
153  }
154  #endif
155 
156  frameCount = firstFrame;
157 
158  isOpen = true;
159 }
160 
161 
168 {
169  if (!initFileName)
170  {
171  vpERROR_TRACE("The generic filename has to be set");
172  throw (vpImageException(vpImageException::noFileNameError,"filename empty"));
173  }
174 
175  if (formatType == FORMAT_PGM ||
176  formatType == FORMAT_PPM ||
177  formatType == FORMAT_JPEG ||
178  formatType == FORMAT_PNG)
179  {
180  width = I.getWidth();
181  height = I.getHeight();
182  }
183  #ifdef VISP_HAVE_FFMPEG
184  else if (formatType == FORMAT_AVI ||
185  formatType == FORMAT_MPEG ||
186  formatType == FORMAT_MOV)
187  {
188  ffmpeg = new vpFFMPEG;
189  ffmpeg->setBitRate(bit_rate);
190  if(!ffmpeg->openEncoder(fileName, I.getWidth(), I.getHeight(), codec))
191  throw (vpException(vpException::ioError ,"Could not open the video"));
192  }
193 
194  #else
195  else if (formatType == FORMAT_AVI ||
196  formatType == FORMAT_MPEG ||
197  formatType == FORMAT_MOV)
198  {
199  vpERROR_TRACE("To write video files the FFmpeg library has to be installed");
200  throw (vpException(vpException::fatalError ,"the FFmpeg library is required"));
201  }
202  #endif
203 
204  frameCount = firstFrame;
205 
206  isOpen = true;
207 }
208 
209 
218 {
219  if (!isOpen)
220  {
221  vpERROR_TRACE("The video has to be open first with the open method");
222  throw (vpException(vpException::notInitialized,"file not yet opened"));
223  }
224 
225 
226  if (formatType == FORMAT_PGM ||
227  formatType == FORMAT_PPM ||
228  formatType == FORMAT_JPEG ||
229  formatType == FORMAT_PNG)
230  {
231  char name[FILENAME_MAX];
232 
233  sprintf(name,fileName,frameCount);
234 
235  vpImageIo::write(I, name);
236  }
237 
238  #ifdef VISP_HAVE_FFMPEG
239  else
240  {
241  ffmpeg->saveFrame(I);
242  }
243  #endif
244 
245  frameCount++;
246 }
247 
248 
257 {
258  if (!isOpen)
259  {
260  vpERROR_TRACE("The video has to be open first with the open method");
261  throw (vpException(vpException::notInitialized,"file not yet opened"));
262  }
263 
264  if (formatType == FORMAT_PGM ||
265  formatType == FORMAT_PPM ||
266  formatType == FORMAT_JPEG ||
267  formatType == FORMAT_PNG)
268  {
269  char name[FILENAME_MAX];
270 
271  sprintf(name,fileName,frameCount);
272 
273  vpImageIo::write(I, name);
274  }
275 
276  #ifdef VISP_HAVE_FFMPEG
277  else
278  {
279  ffmpeg->saveFrame(I);
280  }
281  #endif
282 
283  frameCount++;
284 }
285 
286 
291 {
292  if (!isOpen)
293  {
294  vpERROR_TRACE("The video has to be open first with the open method");
295  throw (vpException(vpException::notInitialized,"file not yet opened"));
296  }
297  #ifdef VISP_HAVE_FFMPEG
298  if (ffmpeg != NULL)
299  {
300  ffmpeg->endWrite();
301  }
302  #endif
303 }
304 
305 
311 vpVideoWriter::vpVideoFormatType
312 vpVideoWriter::getFormat(const char *filename)
313 {
314  std::string sfilename(filename);
315 
316  std::string ext = vpVideoWriter::getExtension(sfilename);
317 
318  if (ext.compare(".PGM") == 0)
319  return FORMAT_PGM;
320  else if (ext.compare(".pgm") == 0)
321  return FORMAT_PGM;
322  else if (ext.compare(".PPM") == 0)
323  return FORMAT_PPM;
324  else if (ext.compare(".ppm") == 0)
325  return FORMAT_PPM;
326  else if (ext.compare(".JPG") == 0)
327  return FORMAT_JPEG;
328  else if (ext.compare(".jpg") == 0)
329  return FORMAT_JPEG;
330  else if (ext.compare(".JPEG") == 0)
331  return FORMAT_JPEG;
332  else if (ext.compare(".jpeg") == 0)
333  return FORMAT_JPEG;
334  else if (ext.compare(".PNG") == 0)
335  return FORMAT_PNG;
336  else if (ext.compare(".png") == 0)
337  return FORMAT_PNG;
338  else if (ext.compare(".AVI") == 0)
339  return FORMAT_AVI;
340  else if (ext.compare(".avi") == 0)
341  return FORMAT_AVI;
342  else if (ext.compare(".MPEG") == 0)
343  return FORMAT_MPEG;
344  else if (ext.compare(".mpeg") == 0)
345  return FORMAT_MPEG;
346  else if (ext.compare(".MPG") == 0)
347  return FORMAT_MPEG;
348  else if (ext.compare(".mpg") == 0)
349  return FORMAT_MPEG;
350  else if (ext.compare(".MOV") == 0)
351  return FORMAT_MOV;
352  else if (ext.compare(".mov") == 0)
353  return FORMAT_MOV;
354  else
355  return FORMAT_UNKNOWN;
356 }
357 
358 // return the extension of the file including the dot
359 std::string vpVideoWriter::getExtension(const std::string &filename)
360 {
361  // extract the extension
362  size_t dot = filename.find_last_of(".");
363  std::string ext = filename.substr(dot, filename.size()-1);
364  return ext;
365 }
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:720
error that can be emited by ViSP classes.
Definition: vpException.h:75
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:644
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:765