
VideoRecorderMp4 C++ library
v1.0.2
Table of contents
- Overview
- Versions
- Library files
- VideoRecorderMp4 class description
- Test application
- Build and connect to your project
Overview
The VideoRecorderMp4 C++ library provides functionality for recording video to files with control over 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. The library uses the C++17 standard and depends on the following libraries: mp4v2 (source code included, Mozilla Public License) and Frame (describes the video frame class, source code included, Apache 2.0 license). Additionally, the test application depends on the VCodecLibav library (source code included) to prepare test H264 frames.
Versions
Table 1 - Library versions.
| Version | Release date | What’s new |
|---|---|---|
| 1.0.0 | 31.03.2025 | - First version |
| 1.0.1 | 14.08.2025 | - Fix compiler warnings. |
| 1.0.2 | 15.11.2025 | - VCodecLibav submodule updated for test application. |
Library files
The library is supplied as source code only. The user is provided with 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
The VideoRecorderMp4 class is declared in the 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 VideoRecorderMp4 class version. Method declaration:
static std::string getVersion();
The method can be used without a VideoRecorderMp4 class instance:
std::cout << "VideoRecorderMp4 v" << cr::video::VideoRecorderMp4::getVersion();
Console output:
VideoRecorderMp4 v1.0.1
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. Format: <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 it. 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 the output file. This value will be recorded in the .mp4 video file header for playback. Final file example format: 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 must have the H264 pixel format. |
Returns: TRUE if the video frame was recorded or FALSE if not.
stop method
The stop() method stops video recording and closes the output video file. Each recording should be stopped by calling this method; otherwise, the MP4 file will not be valid. Method declaration:
void stop();
Test application
The test application is designed to test the VideoRecorderMp4 library with different parameters. After starting, the test application will prompt the user to set the folder name, maximum folder size, and maximum file size. The 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.
The recording can be interrupted by pressing Ctrl+C. After that, you can finish the recording or restart it.
^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 follow these steps. For example, if your repository has the following structure:
CMakeLists.txt
src
CMakeList.txt
yourLib.h
yourLib.cpp
Create a 3rdparty folder and copy the VideoRecorderMp4 repository folder there. Your repository’s new structure:
CMakeLists.txt
src
CMakeList.txt
yourLib.h
yourLib.cpp
3rdparty
VideoRecorderMp4
Create a CMakeLists.txt file in the 3rdparty folder. The 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 from compilation (by default, the test application is excluded from compilation when 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 the following 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!