MXWrite - simple static library for writing RGBA video
FFmpeg-Based Video Writer
This repository provides a C++ class (Writer) that uses the FFmpeg libraries to write raw RGBA frames to an MP4 (or TS) file in H.264 format. It supports both a straightforward, frame-by-frame workflow (open(), write(), and close()) and a timestamp-based workflow (open_ts(), write_ts(), and close()).
Table of Contents
- Overview
- Dependencies
- Building
- Usage
- Key Implementation Details
- License
Overview
Writer is a C++ class that simplifies encoding and writing video frames to a container file. You can:
- Open an output file (MP4 or TS container).
- Write raw RGBA data (passed in as a pointer) to the file, converting it to YUV420P under the hood.
- Optionally use hardware acceleration (CUDA) if available.
- Close and finalize the file properly.
It can handle both:
- Frame-by-frame mode: For applications where you generate or capture frames at a known rate.
- Timestamp-based mode: For applications that require precise PTS (presentation timestamp) control, typically when your source frames arrive at irregular intervals.
Dependencies
Required Libraries
- FFmpeg – The code specifically uses the following components:
- libavcodec
- libavformat
- libavutil
- libswscale
- Threads (C++11 and above) – Uses <thread> and <mutex> from the standard library.
- A C++17 (or higher) compatible compiler.
To install FFmpeg development libraries on your platform:
- Ubuntu / Debian:
sudo apt-get update
sudo apt-get install ffmpeg libavcodec-dev libavformat-dev libavutil-dev libswscale-dev
- Windows:
- Install via vcpkg, MSYS2, or download and build FFmpeg from source.
- macOS:
Building
Configure and build:
mkdir -p build
cd build
cmake ..
cmake --build .
Usage
You can include the mxwrite.hpp header in your own C++ files and link against the library. Below is a quick example:
#include <vector>
bool ok = writer.
open(
"output.mp4", 1280, 720, 30.0f,
"24");
if (!ok) {
return 1;
}
std::vector<uint8_t> dummyRGBA(1280 * 720 * 4, 255);
for (int i = 0; i < 60; ++i) {
writer.
write(dummyRGBA.data());
}
return 0;
}
FFmpeg-backed RGBA video writer.
bool open(const std::string &filename, int width, int height, float fps, const char *crf)
Open an output file using the legacy CRF string interface.
void write(void *rgba_buffer)
Queue a host RGBA frame for immediate-mode encoding.
void close()
Close the writer and flush pending packets.
FFmpeg-based video writer used by MXWrite.
Basic Initialization (open/write)
Use Writer::open(...) to configure the output file, then call write(...) once per RGBA frame. This mode is the simplest path when you already know the target frame rate and are generating frames in a steady loop.
Timestamp-Based Writing (open_ts/write_ts)
Use Writer::open_ts(...) and write_ts(...) when frames arrive at irregular intervals and you want the encoder to preserve capture timing. The writer records timestamps internally and maps them into the stream's time base before muxing.
Key Implementation Details
- Writer can accept host RGBA buffers and, when CUDA support is enabled, device buffers for direct ingestion.
- EncodeOptions centralizes preset, tune, codec selection, CRF, realtime mode, queue backpressure, and HDR metadata. codec accepts auto, software, nvenc, h264_nvenc, and hevc_nvenc.
- The header is intended to be included directly by consumer code that links against the mxwrite target.
License
MXWrite is distributed under the GNU General Public License v3.0. See the root LICENSE file for the full text.