videorecordermp4_web_logo

VideoRecorderMp4 C++ library

v1.0.0

Table of contents

Overview

VideoRecorderMp4 C++ library provides methods for recording video to files with controlling file and folder size. This library is built upon the mp4v2 library. The library records .mp4 video files and accepts only H264 pixel format frames. The library is cross-platform and compatible with Windows and Linux OS. The library is built with C++17 standard. It depends on libraries: mp4v2 (source code included, Mozilla Public License) and Frame (describes video frame class, source code included, Apache 2.0 license). Additionally, the test application depends on VCodecLibav (source code included) library to prepare test H264 frames.

Versions

Table 1 - Library versions.

Version Release date What’s new
1.0.0 31.03.2025 - First version

Library files

The library is supplied only by source code. The user is given a set of files in the form of a CMake project (repository). The repository structure is shown below:

CMakeLists.txt -------------------- Main CMake file of the library.
3rdparty -------------------------- Folder with 3rd-party libraries.
    CMakeLists.txt ---------------- CMake file of 3rd-party libraries folder.
    mp4v2 ------------------------- Folder with mp4v2 library source code.
    Frame ------------------------- Folder with Frame library source code.
src ------------------------------- Folder with library source code.
    CMakeLists.txt ---------------- CMake file of the library.
    VideoRecorderMp4.h ------------ Main library header file.
    VideoRecorderMp4.cpp ---------- C++ implementation file.
    VideoRecorderMp4Version.h ----- Header file with library version.
    VideoRecorderMp4Version.h.in -- CMake service file to generate version header.
    impl -------------------------- Folder with implementation files.
        VideoRecorderMp4Impl.h ---- Implementation header file.
        VideoRecorderMp4Impl.cpp -- Implementation source file.
test ------------------------------ Folder for test application.
    3rdparty ---------------------- Folder with 3rd-party libraries for test application.
        CMakeLists.txt ------------ CMake file of 3rd-party libraries folder.
        VCodecLibav --------------- Folder with VCodecLibav library source code.
    CMakeLists.txt ---------------- CMake file for test application.
    main.cpp ---------------------- Source code of test application.

VideoRecorderMp4 class description

VideoRecorderMp4 class declaration

VideoRecorderMp4 class declared in VideoRecorderMp4.h file. Class declaration:

namespace cr
{
namespace video
{
/// Video recorder class.
class VideoRecorderMp4
{
public:

    /// Class constructor.
    VideoRecorderMp4();

    /// Class destructor.
    ~VideoRecorderMp4();

    /// Get current class version.
    static std::string getVersion();

    /// Init video recorder.
    bool init(std::string initString);

    /// Write video frame.
    bool write(Frame& frame);

    /// Stop video recording.
    void stop();
};
}
}

getVersion method

The getVersion() method returns a string of the current class version. Method declaration:

static std::string getVersion();

Method can be used without VideoRecorderMp4 class instance:

std::cout << "VideoRecorderMp4 v" << cr::video::VideoRecorderMp4::getVersion();

Console output:

VideoRecorderMp4 v1.0.0

init method

The init(…) method initializes the video recorder. The method checks if the destination folder exists; if it does not, it will be created. Method declaration:

bool init(std::string initString);
Parameter Value
initString Initialization string. Value: <folder>;<maxFolderSizeMB>;<maxFileSizeMB>;<prefix>;<fps>.
Example: “video;2000;200;test;30”.
Initialization string parameters:
folder - folder path to store video. The method will check if this folder exists and if not, the method will create the folder.
maxFolderSizeMB - maximum folder size (size of all files) in megabytes.
maxFileSizeMB - Maximum video file size in megabytes. If the size of the currently recorded video file exceeds the specified size, the library will stop recording and create a new file.
prefix - prefix for file names.
fps - FPS of output file. This value will be recorded in the .mp4 video file header for playback.
Final example file will look in the form of: prefix_day_month_year_hour_minute_second.mp4 for example: testVideo_10_12_2025_09_30_20.mp4.

Returns: TRUE if the video recorder is initialized or FALSE if not.

write method

The write(…) method writes an H264 frame to the output video file. Method declaration:

bool write(Frame& frame);
Parameter Value
frame Frame object to write. It should be H264 pixel format.

Returns: TRUE if the video frame was recorded or FALSE if not.

stop method

The stop() method stops video recording. The method stops recording and closes the output video file. Each recording should be stopped by this method, otherwise the mp4 file will not be valid. Method declaration:

void stop();

Test application

The test application is dedicated to testing the VideoRecorderMp4 library with different parameters. After starting, the test application will ask the user to set the folder name, maximum size of folder, and maximum size of file. Test application output will look like:

##################################
VideoRecorderMp4 v1.0.0 Test
##################################
Enter folder to write video: testFolder
Enter max folder size, MB: 20
Enter max file size, MB: 5
Press Ctrl+C to interrupt the recording.

Recording can be interrupted by pressing Ctrl+C. After that, the recording can be finished or restarted.

^CRecording is interrupted.
0 - Stop recording and exit test.
1 - Start new recording.
Choose option: 

Build and connect to your project

Before building the library with the test application, install the VCodecLibav library according to its instructions. Typical commands to build the VideoRecorderMp4 library:

cd VideoRecorderMp4 
mkdir build
cd build
cmake ..
make

If you want to connect the VideoRecorderMp4 library to your CMake project as source code, you can do the following. For example, if your repository has the structure:

CMakeLists.txt
src
    CMakeList.txt
    yourLib.h
    yourLib.cpp

Create folder 3rdparty and copy the VideoRecorderMp4 repository folder there. New structure of your repository:

CMakeLists.txt
src
    CMakeList.txt
    yourLib.h
    yourLib.cpp
3rdparty
    VideoRecorderMp4

Create CMakeLists.txt file in 3rdparty folder. CMakeLists.txt should contain:

cmake_minimum_required(VERSION 3.13)

################################################################################
## 3RD-PARTY
## dependencies for the project
################################################################################
project(3rdparty LANGUAGES CXX)

################################################################################
## SETTINGS
## basic 3rd-party settings before use
################################################################################
# To inherit the top-level architecture when the project is used as a submodule.
SET(PARENT ${PARENT}_YOUR_PROJECT_3RDPARTY)
# Disable self-overwriting of parameters inside included subdirectories.
SET(${PARENT}_SUBMODULE_CACHE_OVERWRITE                 OFF CACHE BOOL "" FORCE)

################################################################################
## INCLUDING SUBDIRECTORIES
## Adding subdirectories according to the 3rd-party configuration
################################################################################
if (${PARENT}_SUBMODULE_VIDEO_RECORDER_MP4)
    add_subdirectory(VideoRecorderMp4)
endif()

The 3rdparty/CMakeLists.txt file adds the VideoRecorderMp4 folder to your project and excludes the test application and example from compiling (by default, the test application and example are excluded from compiling if VideoRecorderMp4 is included as a sub-repository). Your repository’s new structure will be:

CMakeLists.txt
src
    CMakeList.txt
    yourLib.h
    yourLib.cpp
3rdparty
    CMakeLists.txt
    VideoRecorderMp4

Next, you need to include the 3rdparty folder in the main CMakeLists.txt file of your repository. Add this line at the end of your main CMakeLists.txt:

add_subdirectory(3rdparty)

Next, you need to include the VideoRecorderMp4 library in your src/CMakeLists.txt file:

target_link_libraries(${PROJECT_NAME} VideoRecorderMp4)

Done!


Table of contents