libeblearntools
/home/rex/ebltrunk/tools/libeblearntools/include/camera_video.hpp
00001 /***************************************************************************
00002  *   Copyright (C) 2010 by Pierre Sermanet   *
00003  *   pierre.sermanet@gmail.com   *
00004  *   All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Redistribution under a license not approved by the Open Source 
00014  *       Initiative (http://www.opensource.org) must display the 
00015  *       following acknowledgement in all advertising material:
00016  *        This product includes software developed at the Courant
00017  *        Institute of Mathematical Sciences (http://cims.nyu.edu).
00018  *     * The names of the authors may not be used to endorse or promote products
00019  *       derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00024  * DISCLAIMED. IN NO EVENT SHALL ThE AUTHORS BE LIABLE FOR ANY
00025  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00026  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00028  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  ***************************************************************************/
00032 
00033 #ifndef CAMERA_VIDEO_HPP_
00034 #define CAMERA_VIDEO_HPP_
00035 
00036 #include <ostream>
00037 #include <stdlib.h>
00038 #include "configuration.h"
00039 
00040 namespace ebl {
00041 
00043   // constructors & initializations
00044 
00045   template <typename Tdata>
00046   camera_video<Tdata>::camera_video(const char *filename,
00047                                     int height_, int width_,
00048                                     uint sstep, uint endpos)
00049     : camera_directory<Tdata>(height_, width_), fps_video(1) {
00050     cout << "Initializing camera from video file: " << filename << endl;
00051     string dir = filename;
00052     dir += "_images";
00053     // create temporary directory for images
00054     mkdir_full(dir);
00055     // get video fps
00056     cout << "Getting video information..." << endl;
00057     ostringstream cmd;
00058     ostringstream videoconf;
00059     int ret;
00060     videoconf << filename << ".conf";
00061     cmd << "mplayer -identify " << filename;
00062     cmd << " -ao null -vo null -frames 0 2>/dev/null | grep ID_ ";
00063     cmd << " > " << videoconf.str();
00064     ret = std::system(cmd.str().c_str());
00065     configuration conf(videoconf.str());
00066     if (conf.exists("ID_VIDEO_FPS"))
00067       fps_video = conf.get_float("ID_VIDEO_FPS");
00068     cout << "Video FPS is: " << fps_video << endl;
00069     // extract all images from video
00070     cmd.str("");
00071     cmd << "mplayer " << filename << " -ao null -vo pnm:outdir=" << dir;
00072     if (endpos > 0)
00073       cmd << " -endpos " << endpos;
00074     if (sstep > 0)
00075       cmd << " -sstep " << sstep;
00076     cout << "Extracting video frames with command:" << endl 
00077          << cmd.str() << endl;
00078     ret = std::system(cmd.str().c_str());
00079     if (ret < 0)
00080       eblerror("video images extraction failed");
00081     // extract audio
00082     this->audio_filename = dir;
00083     this->audio_filename += "/audio.wav";
00084     cout << "Extracting audio into " << this->audio_filename << endl;
00085     cmd.str("");
00086     cmd << "mplayer -vo null -hardframedrop -ao pcm:file=";
00087     cmd << this->audio_filename << " " << filename;
00088     if (endpos > 0)
00089       cmd << " -endpos " << endpos;
00090     if (sstep > 0)
00091       cmd << " -sstep " << sstep;
00092     ret = std::system(cmd.str().c_str());
00093     // find all images
00094     this->read_directory(dir.c_str());
00095   }
00096   
00097   template <typename Tdata>
00098   camera_video<Tdata>::~camera_video() {
00099   }
00100   
00102   // info
00103   
00104   template <typename Tdata>
00105   float camera_video<Tdata>::fps() {
00106     return fps_video;
00107   }    
00108 
00109 } // end namespace ebl
00110 
00111 #endif /* CAMERA_VIDEO_HPP_ */