MXVK Vulkan Framework 0.33.1
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
MXWrite Directory Reference

Directories

 
opencv_example

Files

 
mxwrite.cpp
 
mxwrite.hpp
 FFmpeg-based video writer used by MXWrite.

Detailed Description

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()).

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

  1. FFmpeg – The code specifically uses the following components:
    • libavcodec
    • libavformat
    • libavutil
    • libswscale
  2. Threads (C++11 and above) – Uses <thread> and <mutex> from the standard library.
  3. 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:
    brew install ffmpeg

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 "mxwrite.hpp"
#include <vector>
int main() {
Writer writer;
// 1. Open an output MP4
bool ok = writer.open("output.mp4", 1280, 720, 30.0f, "24"); // width=1280, height=720, fps=30, bitrate=24 CRF
if (!ok) {
return 1;
}
// 2. Prepare or capture frames in RGBA format.
// For demonstration, we'll just create a dummy buffer of size width * height * 4.
std::vector<uint8_t> dummyRGBA(1280 * 720 * 4, 255); // all white
// 3. Write frames
for (int i = 0; i < 60; ++i) { // e.g., 2 seconds of video at 30 FPS
writer.write(dummyRGBA.data());
}
// 4. Close and finalize
writer.close();
return 0;
}
FFmpeg-backed RGBA video writer.
Definition mxwrite.hpp:139
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.
Definition mxwrite.cpp:1039
void write(void *rgba_buffer)
Queue a host RGBA frame for immediate-mode encoding.
Definition mxwrite.cpp:1859
void close()
Close the writer and flush pending packets.
Definition mxwrite.cpp:2425
int main(int argc, char **argv)
Definition main.cpp:173
FFmpeg-based video writer used by MXWrite.

For explicit HEVC NVENC configuration, use EncodeOptions. Extra parameters use FFmpeg command-line spelling and override the built-in preset/tune defaults:

EncodeOptions options;
options.codec = "hevc_nvenc";
options.ffmpeg_options =
"-preset p6 -tune lossless -profile:v rext -pix_fmt yuv444p";
Writer writer;
if (!writer.open("output_hardware_lossless.mkv", 1920, 1080, 60.0f, options)) {
return 1;
}
User-configurable video encoder quality options.
Definition mxwrite.hpp:92
std::string codec
Encoder selection policy or exact FFmpeg encoder name.
Definition mxwrite.hpp:96
std::string ffmpeg_options
Additional FFmpeg-style video encoder options.
Definition mxwrite.hpp:97

MXWrite uses the FFmpeg libraries directly, so the parameter string contains video encoder/muxer options only; input and output filenames are supplied to Writer::open().

EncodeOptions::codec also accepts any exact video encoder name registered by the linked FFmpeg libraries. For example, libx264 and libx265 remain separate selections, and codecs such as libsvtav1, libvpx-vp9, prores_ks, ffv1, h264_qsv, hevc_vaapi, and h264_videotoolbox can be selected when present. MXWrite automatically chooses a compatible system-memory pixel format; hardware-frame-only encoders use FFmpeg's device and frame-pool APIs.

Applications can populate encoder controls without maintaining a hard-coded list:

for (const EncoderInfo &encoder : available_video_encoders()) {
std::cout << encoder.name << " (" << encoder.codec_name << ")\n";
}
for (const EncoderOptionInfo &option : video_encoder_options("libx265")) {
std::cout << "-" << option.name << ": " << option.help << "\n";
}
std::vector< EncoderInfo > available_video_encoders()
Definition mxwrite.cpp:980
std::vector< EncoderOptionInfo > video_encoder_options(std::string_view encoder_name)
Return the options exposed by one registered video encoder.
Definition mxwrite.cpp:1018
A video encoder reported by the linked FFmpeg installation.
Definition mxwrite.hpp:39
One configurable AVOption exposed by a video encoder.
Definition mxwrite.hpp:49

An encoder being registered does not guarantee that its hardware is usable. Device-backed encoders return a clear open error when the required GPU, driver, or operating-system device is unavailable.