ViSP  2.10.0
vpFFMPEG Class Reference

#include <vpFFMPEG.h>

Public Types

enum  vpFFMPEGColorType { COLORED, GRAY_SCALED }
 

Public Member Functions

 vpFFMPEG ()
 
 ~vpFFMPEG ()
 
bool acquire (vpImage< vpRGBa > &I)
 
bool acquire (vpImage< unsigned char > &I)
 
void closeStream ()
 
bool endWrite ()
 
bool getFrame (vpImage< vpRGBa > &I, unsigned int frameNumber)
 
bool getFrame (vpImage< unsigned char > &I, unsigned int frameNumber)
 
unsigned long getFrameNumber () const
 
double getFramerate () const
 
int getHeight () const
 
int getWidth () const
 
bool initStream ()
 
bool openEncoder (const char *filename, unsigned int width, unsigned int height, AVCodecID codec=AV_CODEC_ID_MPEG1VIDEO)
 
bool openStream (const char *filename, vpFFMPEGColorType color_type)
 
bool saveFrame (vpImage< vpRGBa > &I)
 
bool saveFrame (vpImage< unsigned char > &I)
 
void setBitRate (const unsigned int rate)
 
void setFramerate (const int framerate)
 

Detailed Description

This class interfaces the FFmpeg library to enable video stream reading or writing.

Here an example which explains how to use the class to read a video stream.

#include <visp/vpFFMPEG.h>
int main ()
{
#ifdef VISP_HAVE_FFMPEG
vpImage<vpRGBa> I; // The image to stores the frames
vpFFMPEG ffmpeg;
// Initialization
ffmpeg.openStream("video.mpeg", vpFFMPEG::COLORED);
ffmpeg.initStream();
std::cout << "framerate: " << ffmpeg.getFramerate() << "Hz" << std::endl;
std::cout << "image size: " << ffmpeg.getWidth() << " " << ffmpeg.getHeight() << std::endl;
// Video reading
int frameIndex = 0;
ffmpeg.getFrame(I,frameIndex); // Here the first frame (index 0) is read.
#endif
}

If you want to open the video as a gray scaled video, you can use the following example.

#include <visp/vpFFMPEG.h>
int main ()
{
#ifdef VISP_HAVE_FFMPEG
vpImage<unsigned char> I; // The image to stores the frames
vpFFMPEG ffmpeg;
// Initialization
ffmpeg.openStream("video.mpeg", vpFFMPEG::GRAY_SCALED);
ffmpeg.initStream();
std::cout << "framerate: " << ffmpeg.getFramerate() << "Hz" << std::endl;
std::cout << "image size: " << ffmpeg.getWidth() << " " << ffmpeg.getHeight() << std::endl;
// Video reading
int frameIndex = 0;
ffmpeg.getFrame(I,frameIndex); // Here the first frame (index 0) is read.
#endif
}

Definition at line 149 of file vpFFMPEG.h.

Member Enumeration Documentation

Enumerator
COLORED 
GRAY_SCALED 

Definition at line 152 of file vpFFMPEG.h.

Constructor & Destructor Documentation

vpFFMPEG::vpFFMPEG ( )

Basic constructor.

Definition at line 67 of file vpFFMPEG.cpp.

vpFFMPEG::~vpFFMPEG ( )

Basic destructor.

Definition at line 82 of file vpFFMPEG.cpp.

References closeStream().

Member Function Documentation

bool vpFFMPEG::acquire ( vpImage< vpRGBa > &  I)

Gets the Next frame in the video.

Parameters
I: The vpImage used to stored the video's frame.
Returns
It returns true if the frame could be read. Else it returns false.

Definition at line 348 of file vpFFMPEG.cpp.

References COLORED, GRAY_SCALED, and vpTRACE.

Referenced by vpVideoReader::acquire().

bool vpFFMPEG::acquire ( vpImage< unsigned char > &  I)

Gets the Next frame in the video.

Parameters
I: The vpImage used to stored the video's frame.
Returns
It returns true if the frame could be read. Else it returns false.

Definition at line 447 of file vpFFMPEG.cpp.

References COLORED, GRAY_SCALED, and vpTRACE.

void vpFFMPEG::closeStream ( )

Deallocates all the FFMPEG parameters.

Definition at line 601 of file vpFFMPEG.cpp.

References COLORED, endWrite(), and GRAY_SCALED.

Referenced by ~vpFFMPEG().

bool vpFFMPEG::endWrite ( )

Ends the writing of the video and close the file.

Returns
It returns true if the file was closed without problem

Definition at line 838 of file vpFFMPEG.cpp.

References vpTRACE.

Referenced by vpVideoWriter::close(), and closeStream().

bool vpFFMPEG::getFrame ( vpImage< vpRGBa > &  I,
unsigned int  frame 
)

Gets the $ frame $ th frame from the video and stores it in the image $ I $.

Parameters
I: The vpImage used to stored the video's frame.
frame: The index of the frame which has to be read.
Returns
It returns true if the frame could be read. Else it returns false.

Definition at line 294 of file vpFFMPEG.cpp.

References COLORED, GRAY_SCALED, and vpTRACE.

Referenced by vpVideoReader::getFrame().

bool vpFFMPEG::getFrame ( vpImage< unsigned char > &  I,
unsigned int  frame 
)

Gets the $ frame $ th frame from the video and stores it in the image $ I $.

Parameters
I: The vpImage used to stored the video's frame.
frame: The index of the frame which has to be read.
Returns
It returns true if the frame could be read. Else it returns false.

Definition at line 393 of file vpFFMPEG.cpp.

References COLORED, GRAY_SCALED, and vpTRACE.

unsigned long vpFFMPEG::getFrameNumber ( ) const
inline

Gets the video's frame number.

Returns
The value of the video's frame number.

Definition at line 216 of file vpFFMPEG.h.

double vpFFMPEG::getFramerate ( ) const
inline

Return the framerate used to encode the video. The video stream need to be opened before calling this function.

Exceptions
vpException::ioError: If the video stream was not opened priviously.

Definition at line 223 of file vpFFMPEG.h.

References vpException::ioError.

Referenced by vpVideoReader::getFramerate().

int vpFFMPEG::getHeight ( ) const
inline

Gets the video's height.

Returns
The value of the video's height.

Definition at line 233 of file vpFFMPEG.h.

int vpFFMPEG::getWidth ( ) const
inline

Gets the video's width.

Returns
The value of the video's width.

Definition at line 239 of file vpFFMPEG.h.

bool vpFFMPEG::initStream ( )

This method initializes the conversion parameters.

It browses the video and lists all the frame. It sets the number of frame in the video.

Returns
It returns true if the method was executed without any problem. Else it returns false.

Definition at line 235 of file vpFFMPEG.cpp.

References COLORED, GRAY_SCALED, and vpTRACE.

Referenced by vpVideoReader::open().

bool vpFFMPEG::openEncoder ( const char *  filename,
unsigned int  w,
unsigned int  h,
AVCodecID  codec = AV_CODEC_ID_MPEG1VIDEO 
)

Allocates and initializes the parameters depending on the video to write.

Parameters
filename: Path to the video which has to be writen.
w,h: Width and height of the image which will be saved.
codec: Type of codec used to encode the video.

By default codec is set to AV_CODEC_ID_MPEG1VIDEO. But if installed, you can use one of the AVCodecID proposed by ffmpeg such as : AV_CODEC_ID_MPEG2VIDEO, AV_CODEC_ID_MPEG2VIDEO_XVMC, AV_CODEC_ID_MPEG4, AV_CODEC_ID_H264, ... (More AVCodecID can be found in the ffmpeg documentation).

Of course to use the codec it must be installed on your computer.

Returns
It returns true if the paramters could be initialized. Else it returns false.

Definition at line 672 of file vpFFMPEG.cpp.

Referenced by vpVideoWriter::open().

bool vpFFMPEG::openStream ( const char *  filename,
vpFFMPEGColorType  colortype 
)

Allocates and initializes the parameters depending on the video and the desired color type. One the stream is opened, it is possible to get the video encoding framerate getFramerate(), and the dimension of the images using getWidth() and getHeight().

Parameters
filename: Path to the video which has to be read.
colortype: Desired color map used to open the video. The parameter can take two values : COLORED and GRAY_SCALED.
Returns
It returns true if the paramters could be initialized. Else it returns false.

Definition at line 99 of file vpFFMPEG.cpp.

References COLORED, GRAY_SCALED, and vpTRACE.

Referenced by vpVideoReader::open().

bool vpFFMPEG::saveFrame ( vpImage< vpRGBa > &  I)

Saves the image I as frame of the video.

Parameters
I: the image to save.
Returns
It returns true if the image could be saved.

Definition at line 756 of file vpFFMPEG.cpp.

References vpTRACE.

Referenced by vpVideoWriter::saveFrame().

bool vpFFMPEG::saveFrame ( vpImage< unsigned char > &  I)

Saves the image I as frame of the video.

Parameters
I: the image to save.
Returns
It returns true if the image could be saved.

Definition at line 798 of file vpFFMPEG.cpp.

References vpTRACE.

void vpFFMPEG::setBitRate ( const unsigned int  rate)
inline

Sets the bit rate of the video when encoding.

Parameters
rate: the expected bit rate.

Definition at line 257 of file vpFFMPEG.h.

Referenced by vpVideoWriter::open().

void vpFFMPEG::setFramerate ( const int  framerate)
inline

Sets the framerate of the video when encoding.

Parameters
framerate: the expected framerate.

Definition at line 263 of file vpFFMPEG.h.

Referenced by vpVideoWriter::open().