ViSP  2.9.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 - 2014 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 #ifdef VISP_HAVE_FFMPEG
57  ffmpeg(NULL),
58 # if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54,51,110) // libavcodec 54.51.100
59  codec(CODEC_ID_MPEG1VIDEO),
60 # else
61  codec(AV_CODEC_ID_MPEG1VIDEO),
62 # endif
63  bit_rate(500000),
64  framerate(25),
65 #endif
66  formatType(FORMAT_UNKNOWN), initFileName(false), isOpen(false), frameCount(0),
67  firstFrame(0), width(0), height(0)
68 {
69  initFileName = false;
70  firstFrame = 0;
71  frameCount = 0;
72  isOpen = false;
73  width = height = 0;
74 #ifdef VISP_HAVE_FFMPEG
75  ffmpeg = NULL;
76 # if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54,51,110) // libavcodec 54.51.100
77  codec = CODEC_ID_MPEG1VIDEO;
78 # else
79  codec = AV_CODEC_ID_MPEG1VIDEO;
80 # endif
81  bit_rate = 500000;
82  framerate = 25;
83 #endif
84 }
85 
86 
91 {
92  #ifdef VISP_HAVE_FFMPEG
93  if (ffmpeg != NULL)
94  delete ffmpeg;
95  #endif
96 }
97 
98 
106 void vpVideoWriter::setFileName(const char *filename)
107 {
108  if (filename == '\0')
109  {
110  vpERROR_TRACE("filename empty ") ;
111  throw (vpImageException(vpImageException::noFileNameError,"filename empty ")) ;
112  }
113 
114  if (strlen( filename ) >= FILENAME_MAX) {
116  "Not enough memory to intialize the file name"));
117  }
118 
119  strcpy(this->fileName,filename);
120 
121  formatType = getFormat(fileName);
122 
123  initFileName = true;
124 }
125 
133 void vpVideoWriter::setFileName(const std::string &filename)
134 {
135  setFileName(filename.c_str());
136 }
137 
144 {
145  if (!initFileName)
146  {
147  vpERROR_TRACE("The generic filename has to be set");
148  throw (vpImageException(vpImageException::noFileNameError,"filename empty"));
149  }
150 
151  if (formatType == FORMAT_PGM ||
152  formatType == FORMAT_PPM ||
153  formatType == FORMAT_JPEG ||
154  formatType == FORMAT_PNG)
155  {
156  width = I.getWidth();
157  height = I.getHeight();
158  }
159  #ifdef VISP_HAVE_FFMPEG
160  else if (formatType == FORMAT_AVI ||
161  formatType == FORMAT_MPEG ||
162  formatType == FORMAT_MOV)
163  {
164  ffmpeg = new vpFFMPEG;
165  ffmpeg->setFramerate(framerate);
166  ffmpeg->setBitRate(bit_rate);
167  if(!ffmpeg->openEncoder(fileName, I.getWidth(), I.getHeight(), codec))
168  throw (vpException(vpException::ioError ,"Could not open the video"));
169  }
170 
171  #else
172  else if (formatType == FORMAT_AVI ||
173  formatType == FORMAT_MPEG ||
174  formatType == FORMAT_MOV)
175  {
176  vpERROR_TRACE("To write video files the FFmpeg library has to be installed");
177  throw (vpException(vpException::fatalError ,"the FFmpeg library is required"));
178  }
179  #endif
180 
181  frameCount = firstFrame;
182 
183  isOpen = true;
184 }
185 
186 
193 {
194  if (!initFileName)
195  {
196  vpERROR_TRACE("The generic filename has to be set");
197  throw (vpImageException(vpImageException::noFileNameError,"filename empty"));
198  }
199 
200  if (formatType == FORMAT_PGM ||
201  formatType == FORMAT_PPM ||
202  formatType == FORMAT_JPEG ||
203  formatType == FORMAT_PNG)
204  {
205  width = I.getWidth();
206  height = I.getHeight();
207  }
208  #ifdef VISP_HAVE_FFMPEG
209  else if (formatType == FORMAT_AVI ||
210  formatType == FORMAT_MPEG ||
211  formatType == FORMAT_MOV)
212  {
213  ffmpeg = new vpFFMPEG;
214  ffmpeg->setFramerate(framerate);
215  ffmpeg->setBitRate(bit_rate);
216  if(!ffmpeg->openEncoder(fileName, I.getWidth(), I.getHeight(), codec))
217  throw (vpException(vpException::ioError ,"Could not open the video"));
218  }
219 
220  #else
221  else if (formatType == FORMAT_AVI ||
222  formatType == FORMAT_MPEG ||
223  formatType == FORMAT_MOV)
224  {
225  vpERROR_TRACE("To write video files the FFmpeg library has to be installed");
226  throw (vpException(vpException::fatalError ,"the FFmpeg library is required"));
227  }
228  #endif
229 
230  frameCount = firstFrame;
231 
232  isOpen = true;
233 }
234 
235 
244 {
245  if (!isOpen)
246  {
247  vpERROR_TRACE("The video has to be open first with the open method");
248  throw (vpException(vpException::notInitialized,"file not yet opened"));
249  }
250 
251 
252  if (formatType == FORMAT_PGM ||
253  formatType == FORMAT_PPM ||
254  formatType == FORMAT_JPEG ||
255  formatType == FORMAT_PNG)
256  {
257  char name[FILENAME_MAX];
258 
259  sprintf(name,fileName,frameCount);
260 
261  vpImageIo::write(I, name);
262  }
263 
264  #ifdef VISP_HAVE_FFMPEG
265  else
266  {
267  ffmpeg->saveFrame(I);
268  }
269  #endif
270 
271  frameCount++;
272 }
273 
274 
283 {
284  if (!isOpen)
285  {
286  vpERROR_TRACE("The video has to be open first with the open method");
287  throw (vpException(vpException::notInitialized,"file not yet opened"));
288  }
289 
290  if (formatType == FORMAT_PGM ||
291  formatType == FORMAT_PPM ||
292  formatType == FORMAT_JPEG ||
293  formatType == FORMAT_PNG)
294  {
295  char name[FILENAME_MAX];
296 
297  sprintf(name,fileName,frameCount);
298 
299  vpImageIo::write(I, name);
300  }
301 
302  #ifdef VISP_HAVE_FFMPEG
303  else
304  {
305  ffmpeg->saveFrame(I);
306  }
307  #endif
308 
309  frameCount++;
310 }
311 
312 
317 {
318  if (!isOpen)
319  {
320  vpERROR_TRACE("The video has to be open first with the open method");
321  throw (vpException(vpException::notInitialized,"file not yet opened"));
322  }
323  #ifdef VISP_HAVE_FFMPEG
324  if (ffmpeg != NULL)
325  {
326  ffmpeg->endWrite();
327  }
328  #endif
329 }
330 
331 
337 vpVideoWriter::vpVideoFormatType
338 vpVideoWriter::getFormat(const char *filename)
339 {
340  std::string sfilename(filename);
341 
342  std::string ext = vpVideoWriter::getExtension(sfilename);
343 
344  if (ext.compare(".PGM") == 0)
345  return FORMAT_PGM;
346  else if (ext.compare(".pgm") == 0)
347  return FORMAT_PGM;
348  else if (ext.compare(".PPM") == 0)
349  return FORMAT_PPM;
350  else if (ext.compare(".ppm") == 0)
351  return FORMAT_PPM;
352  else if (ext.compare(".JPG") == 0)
353  return FORMAT_JPEG;
354  else if (ext.compare(".jpg") == 0)
355  return FORMAT_JPEG;
356  else if (ext.compare(".JPEG") == 0)
357  return FORMAT_JPEG;
358  else if (ext.compare(".jpeg") == 0)
359  return FORMAT_JPEG;
360  else if (ext.compare(".PNG") == 0)
361  return FORMAT_PNG;
362  else if (ext.compare(".png") == 0)
363  return FORMAT_PNG;
364  else if (ext.compare(".AVI") == 0)
365  return FORMAT_AVI;
366  else if (ext.compare(".avi") == 0)
367  return FORMAT_AVI;
368  else if (ext.compare(".MPEG") == 0)
369  return FORMAT_MPEG;
370  else if (ext.compare(".mpeg") == 0)
371  return FORMAT_MPEG;
372  else if (ext.compare(".MPG") == 0)
373  return FORMAT_MPEG;
374  else if (ext.compare(".mpg") == 0)
375  return FORMAT_MPEG;
376  else if (ext.compare(".MOV") == 0)
377  return FORMAT_MOV;
378  else if (ext.compare(".mov") == 0)
379  return FORMAT_MOV;
380  else
381  return FORMAT_UNKNOWN;
382 }
383 
384 // return the extension of the file including the dot
385 std::string vpVideoWriter::getExtension(const std::string &filename)
386 {
387  // extract the extension
388  size_t dot = filename.find_last_of(".");
389  std::string ext = filename.substr(dot, filename.size()-1);
390  return ext;
391 }
static void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:452
void setBitRate(const unsigned int rate)
Definition: vpFFMPEG.h:257
unsigned int getWidth() const
Definition: vpImage.h:159
#define vpERROR_TRACE
Definition: vpDebug.h:395
bool saveFrame(vpImage< vpRGBa > &I)
Definition: vpFFMPEG.cpp:729
error that can be emited by ViSP classes.
Definition: vpException.h:76
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.
Definition: vpFFMPEG.h:149
void saveFrame(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
void setFileName(const char *filename)
unsigned int getHeight() const
Definition: vpImage.h:150
bool endWrite()
Definition: vpFFMPEG.cpp:811
bool openEncoder(const char *filename, unsigned int width, unsigned int height, AVCodecID codec=AV_CODEC_ID_MPEG1VIDEO)
Definition: vpFFMPEG.cpp:652
void setFramerate(const int framerate)
Definition: vpFFMPEG.h:263