ViSP  2.8.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 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54,51,110) // libavcodec 54.51.100
63  codec = CODEC_ID_MPEG1VIDEO;
64 #else
65  codec = AV_CODEC_ID_MPEG1VIDEO;
66 #endif
67  bit_rate = 500000;
68  framerate = 25;
69  #endif
70 }
71 
72 
77 {
78  #ifdef VISP_HAVE_FFMPEG
79  if (ffmpeg != NULL)
80  delete ffmpeg;
81  #endif
82 }
83 
84 
92 void vpVideoWriter::setFileName(const char *filename)
93 {
94  if (filename == '\0')
95  {
96  vpERROR_TRACE("filename empty ") ;
97  throw (vpImageException(vpImageException::noFileNameError,"filename empty ")) ;
98  }
99 
100  strcpy(this->fileName,filename);
101 
102  formatType = getFormat(fileName);
103 
104  initFileName = true;
105 }
106 
114 void vpVideoWriter::setFileName(const std::string &filename)
115 {
116  setFileName(filename.c_str());
117 }
118 
125 {
126  if (!initFileName)
127  {
128  vpERROR_TRACE("The generic filename has to be set");
129  throw (vpImageException(vpImageException::noFileNameError,"filename empty"));
130  }
131 
132  if (formatType == FORMAT_PGM ||
133  formatType == FORMAT_PPM ||
134  formatType == FORMAT_JPEG ||
135  formatType == FORMAT_PNG)
136  {
137  width = I.getWidth();
138  height = I.getHeight();
139  }
140  #ifdef VISP_HAVE_FFMPEG
141  else if (formatType == FORMAT_AVI ||
142  formatType == FORMAT_MPEG ||
143  formatType == FORMAT_MOV)
144  {
145  ffmpeg = new vpFFMPEG;
146  ffmpeg->setFramerate(framerate);
147  ffmpeg->setBitRate(bit_rate);
148  if(!ffmpeg->openEncoder(fileName, I.getWidth(), I.getHeight(), codec))
149  throw (vpException(vpException::ioError ,"Could not open the video"));
150  }
151 
152  #else
153  else if (formatType == FORMAT_AVI ||
154  formatType == FORMAT_MPEG ||
155  formatType == FORMAT_MOV)
156  {
157  vpERROR_TRACE("To write video files the FFmpeg library has to be installed");
158  throw (vpException(vpException::fatalError ,"the FFmpeg library is required"));
159  }
160  #endif
161 
162  frameCount = firstFrame;
163 
164  isOpen = true;
165 }
166 
167 
174 {
175  if (!initFileName)
176  {
177  vpERROR_TRACE("The generic filename has to be set");
178  throw (vpImageException(vpImageException::noFileNameError,"filename empty"));
179  }
180 
181  if (formatType == FORMAT_PGM ||
182  formatType == FORMAT_PPM ||
183  formatType == FORMAT_JPEG ||
184  formatType == FORMAT_PNG)
185  {
186  width = I.getWidth();
187  height = I.getHeight();
188  }
189  #ifdef VISP_HAVE_FFMPEG
190  else if (formatType == FORMAT_AVI ||
191  formatType == FORMAT_MPEG ||
192  formatType == FORMAT_MOV)
193  {
194  ffmpeg = new vpFFMPEG;
195  ffmpeg->setFramerate(framerate);
196  ffmpeg->setBitRate(bit_rate);
197  if(!ffmpeg->openEncoder(fileName, I.getWidth(), I.getHeight(), codec))
198  throw (vpException(vpException::ioError ,"Could not open the video"));
199  }
200 
201  #else
202  else if (formatType == FORMAT_AVI ||
203  formatType == FORMAT_MPEG ||
204  formatType == FORMAT_MOV)
205  {
206  vpERROR_TRACE("To write video files the FFmpeg library has to be installed");
207  throw (vpException(vpException::fatalError ,"the FFmpeg library is required"));
208  }
209  #endif
210 
211  frameCount = firstFrame;
212 
213  isOpen = true;
214 }
215 
216 
225 {
226  if (!isOpen)
227  {
228  vpERROR_TRACE("The video has to be open first with the open method");
229  throw (vpException(vpException::notInitialized,"file not yet opened"));
230  }
231 
232 
233  if (formatType == FORMAT_PGM ||
234  formatType == FORMAT_PPM ||
235  formatType == FORMAT_JPEG ||
236  formatType == FORMAT_PNG)
237  {
238  char name[FILENAME_MAX];
239 
240  sprintf(name,fileName,frameCount);
241 
242  vpImageIo::write(I, name);
243  }
244 
245  #ifdef VISP_HAVE_FFMPEG
246  else
247  {
248  ffmpeg->saveFrame(I);
249  }
250  #endif
251 
252  frameCount++;
253 }
254 
255 
264 {
265  if (!isOpen)
266  {
267  vpERROR_TRACE("The video has to be open first with the open method");
268  throw (vpException(vpException::notInitialized,"file not yet opened"));
269  }
270 
271  if (formatType == FORMAT_PGM ||
272  formatType == FORMAT_PPM ||
273  formatType == FORMAT_JPEG ||
274  formatType == FORMAT_PNG)
275  {
276  char name[FILENAME_MAX];
277 
278  sprintf(name,fileName,frameCount);
279 
280  vpImageIo::write(I, name);
281  }
282 
283  #ifdef VISP_HAVE_FFMPEG
284  else
285  {
286  ffmpeg->saveFrame(I);
287  }
288  #endif
289 
290  frameCount++;
291 }
292 
293 
298 {
299  if (!isOpen)
300  {
301  vpERROR_TRACE("The video has to be open first with the open method");
302  throw (vpException(vpException::notInitialized,"file not yet opened"));
303  }
304  #ifdef VISP_HAVE_FFMPEG
305  if (ffmpeg != NULL)
306  {
307  ffmpeg->endWrite();
308  }
309  #endif
310 }
311 
312 
318 vpVideoWriter::vpVideoFormatType
319 vpVideoWriter::getFormat(const char *filename)
320 {
321  std::string sfilename(filename);
322 
323  std::string ext = vpVideoWriter::getExtension(sfilename);
324 
325  if (ext.compare(".PGM") == 0)
326  return FORMAT_PGM;
327  else if (ext.compare(".pgm") == 0)
328  return FORMAT_PGM;
329  else if (ext.compare(".PPM") == 0)
330  return FORMAT_PPM;
331  else if (ext.compare(".ppm") == 0)
332  return FORMAT_PPM;
333  else if (ext.compare(".JPG") == 0)
334  return FORMAT_JPEG;
335  else if (ext.compare(".jpg") == 0)
336  return FORMAT_JPEG;
337  else if (ext.compare(".JPEG") == 0)
338  return FORMAT_JPEG;
339  else if (ext.compare(".jpeg") == 0)
340  return FORMAT_JPEG;
341  else if (ext.compare(".PNG") == 0)
342  return FORMAT_PNG;
343  else if (ext.compare(".png") == 0)
344  return FORMAT_PNG;
345  else if (ext.compare(".AVI") == 0)
346  return FORMAT_AVI;
347  else if (ext.compare(".avi") == 0)
348  return FORMAT_AVI;
349  else if (ext.compare(".MPEG") == 0)
350  return FORMAT_MPEG;
351  else if (ext.compare(".mpeg") == 0)
352  return FORMAT_MPEG;
353  else if (ext.compare(".MPG") == 0)
354  return FORMAT_MPEG;
355  else if (ext.compare(".mpg") == 0)
356  return FORMAT_MPEG;
357  else if (ext.compare(".MOV") == 0)
358  return FORMAT_MOV;
359  else if (ext.compare(".mov") == 0)
360  return FORMAT_MOV;
361  else
362  return FORMAT_UNKNOWN;
363 }
364 
365 // return the extension of the file including the dot
366 std::string vpVideoWriter::getExtension(const std::string &filename)
367 {
368  // extract the extension
369  size_t dot = filename.find_last_of(".");
370  std::string ext = filename.substr(dot, filename.size()-1);
371  return ext;
372 }
static void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:442
unsigned int getWidth() const
Definition: vpImage.h:159
#define vpERROR_TRACE
Definition: vpDebug.h:379
void setBitRate(const unsigned int bit_rate)
Definition: vpFFMPEG.h:257
bool saveFrame(vpImage< vpRGBa > &I)
Definition: vpFFMPEG.cpp:738
error that can be emited by ViSP classes.
Definition: vpException.h:75
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:820
bool openEncoder(const char *filename, unsigned int width, unsigned int height, AVCodecID codec=AV_CODEC_ID_MPEG1VIDEO)
Definition: vpFFMPEG.cpp:661
void setFramerate(const int framerate)
Definition: vpFFMPEG.h:263