Easy3D 2.6.1
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
VideoEncoder Class Reference

A class that encodes video frames (e.g., images) into a video file. More...

#include <easy3d/video/video_encoder.h>

Public Types

enum  PixelFormat { PIX_FMT_RGB_888 , PIX_FMT_BGR_888 , PIX_FMT_RGBA_8888 , PIX_FMT_BGRA_8888 }
 The supported pixel formats for the video frames. More...
 

Public Member Functions

 VideoEncoder (const std::string &file_name, int framerate=30, int bitrate=8 *1024 *1024)
 Constructor to initialize the video encoder.
 
 ~VideoEncoder ()
 Destructor to clean up resources.
 
bool encode (const unsigned char *image_data, int width, int height, PixelFormat pixel_format)
 Encodes one frame to the video stream.
 

Static Public Member Functions

static bool is_size_acceptable (int width, int height)
 Checks if the image size (width, height) is acceptable.
 

Detailed Description

A class that encodes video frames (e.g., images) into a video file.

The output format is automatically guessed according to the file extension. Below is an example:

VideoEncoder encoder(image_file, 30, 8 * 1024 * 1024); // framereate: 30 fps. bitrate: 8 Mbits/sec.
for (std::size_t i = 0; i < image_files.size(); ++i) {
std::vector<unsigned char> data;
int w, h, c;
if (ImageIO::load(file_name, data, w, h, c, 0, false))
encoder.encode(data.data(), w, h, c == 3 ? VideoEncoder::PIX_FMT_RGB_888 : VideoEncoder::PIX_FMT_RGBA_8888);
}
static bool load(const std::string &file_name, std::vector< unsigned char > &data, int &width, int &height, int &channels, int requested_channels=0, bool flip_vertically=true)
Load image data from a file.
Definition image_io.cpp:41
VideoEncoder(const std::string &file_name, int framerate=30, int bitrate=8 *1024 *1024)
Constructor to initialize the video encoder.
Definition video_encoder.cpp:595
@ PIX_FMT_RGB_888
Packed RGB 8:8:8, 24bpp, RGBRGB...
Definition video_encoder.h:76
@ PIX_FMT_RGBA_8888
Packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition video_encoder.h:78

Member Enumeration Documentation

◆ PixelFormat

The supported pixel formats for the video frames.

Enumerator
PIX_FMT_RGB_888 

Packed RGB 8:8:8, 24bpp, RGBRGB...

PIX_FMT_BGR_888 

Packed BGR 8:8:8, 24bpp, BGRBGR...

PIX_FMT_RGBA_8888 

Packed RGBA 8:8:8:8, 32bpp, RGBARGBA...

PIX_FMT_BGRA_8888 

Packed BGRA 8:8:8:8, 32bpp, BGRABGRA...

Constructor & Destructor Documentation

◆ VideoEncoder()

VideoEncoder ( const std::string & file_name,
int framerate = 30,
int bitrate = 8 * 1024 * 1024 )
explicit

Constructor to initialize the video encoder.

Parameters
file_nameThe name of the output video file, e.g., "D:/result.mp4". The output format is automatically guessed according to the file extension, which can be:
  • mp4: MPEG-4 Part 14;
  • mpeg: MPEG-1 Systems / MPEG program stream;
  • avi: Audio Video Interleaved;
  • mov: QuickTime / MOV; If it can't be guessed this way then "mp4" is used by default.
framerateThe frame rate (normally between 25 and 60, default is 30 fps).
bitrateThe bit rate (default is 8 Mbits/sec).

Member Function Documentation

◆ encode()

bool encode ( const unsigned char * image_data,
int width,
int height,
PixelFormat pixel_format )

Encodes one frame to the video stream.

Parameters
image_dataThe input image data as a 1D array of 'unsigned char' pointing to the pixel data. The pixel data consists of 'height' rows of 'width' pixels, with each pixel has one of the following structures.
widthThe video width (must be a multiple of 8).
heightThe video height (must be a multiple of 8).
pixel_formatThe pixel format. Correspondences between image structures and pixel/OpenGL formats are:
  • RGB 8:8:8, 24bpp <---> PIX_FMT_RGB_888 <---> GL_RGB
  • BGR 8:8:8, 24bpp <---> PIX_FMT_BGR_888 <---> GL_BGR
  • RGBA 8:8:8:8, 32bpp <---> PIX_FMT_RGBA_8888 <---> GL_RGBA
  • BGRA 8:8:8:8, 32bpp <---> PIX_FMT_BGRA_8888 <---> GL_BGRA
Returns
True on success, false otherwise.

◆ is_size_acceptable()

static bool is_size_acceptable ( int width,
int height )
inlinestatic

Checks if the image size (width, height) is acceptable.

Parameters
widthThe width of the image.
heightThe height of the image.
Returns
True if the size is acceptable, false otherwise.

The documentation for this class was generated from the following files: