Using Video Presets

This topic describes how to use the TTranscoder class to convert a raw YUV video file into a compressed video file. The format of the output is configured with an AVBlocks preset.

The code snippets in this article are from the enc_preset_file sample.

Create a Transcoder object

Create a new TTranscoder object and enable the demo mode.

TTranscoder()
    .allowDemoMode(true)

Configure inputs and outputs

Use the TMediaSocket class to configure media sockets and the TMediaPin class to configure media pins.

Configure video input

  1. Create and configure a TVideoStreamInfo object

  2. Create a TMediaPin object and set its stream info with TMediaPin::streamInfo.

  3. Create a TMediaSocket object and add the pin with TMediaSocket::addPin.

  4. Add the socket to the transcoder with TTranscoder::addInput.

TMediaSocket()
    .file(opt.yuv_file)
    .streamType(StreamType::UncompressedVideo)
    .addPin(
        TMediaPin()
            .streamInfo(TVideoStreamInfo()
                .streamType(StreamType::UncompressedVideo)
                .frameWidth(opt.yuv_frame.width)
                .frameHeight(opt.yuv_frame.height)
                .colorFormat(opt.yuv_color.Id)
                .frameRate(opt.yuv_fps)
                .scanType(ScanType::Progressive)
            )
    )

Configure video output

  1. Create a TMediaSocket object for a predefined Preset by passing the preset name to its constructor

  2. Call the TMediaSocket::file method to set the output filename

  3. Add the socket to the transcoder with TTranscoder::addOutput

TMediaSocket(opt.preset.name)
    .file(opt.output_file)

Transcode

  1. Call the TTranscoder::open method.

  2. Call the TTranscoder::run method.

  3. Call the TTranscoder::close method.

TTranscoder()
    .allowDemoMode(true)
    .addInput(
        TMediaSocket()
            .file(opt.yuv_file)
            .streamType(StreamType::UncompressedVideo)
            .addPin(
                TMediaPin()
                    .streamInfo(TVideoStreamInfo()
                        .streamType(StreamType::UncompressedVideo)
                        .frameWidth(opt.yuv_frame.width)
                        .frameHeight(opt.yuv_frame.height)
                        .colorFormat(opt.yuv_color.Id)
                        .frameRate(opt.yuv_fps)
                        .scanType(ScanType::Progressive)
                    )
            )
    )
    .addOutput(
        // Construct from preset name — pre-configures all codec settings
        TMediaSocket(opt.preset.name)
            .file(opt.output_file)
    )
    .open()
    .run()
    .close();

Complete C++ code

#include <primo/avblocks/avb++.h>

#include <print>

#include "options.h"
#include "util.h"

using namespace primo::codecs;
using namespace primo::avblocks::modern;

bool encode(const Options& opt)
{
    try {
        deleteFile(opt.output_file.c_str());

        TTranscoder()
            .allowDemoMode(true)
            .addInput(
                TMediaSocket()
                    .file(opt.yuv_file)
                    .streamType(StreamType::UncompressedVideo)
                    .addPin(
                        TMediaPin()
                            .streamInfo(TVideoStreamInfo()
                                .streamType(StreamType::UncompressedVideo)
                                .frameWidth(opt.yuv_frame.width)
                                .frameHeight(opt.yuv_frame.height)
                                .colorFormat(opt.yuv_color.Id)
                                .frameRate(opt.yuv_fps)
                                .scanType(ScanType::Progressive)
                            )
                    )
            )
            .addOutput(
                // Construct from preset name — pre-configures all codec settings
                TMediaSocket(opt.preset.name)
                    .file(opt.output_file)
            )
            .open()
            .run()
            .close();

        std::println("Output: {}", opt.output_file);
        return true;

    } catch (const TAVBlocksException& ex) {
        std::println(stderr, "AVBlocks error: {}", ex.what());
        return false;
    } catch (const std::exception& ex) {
        std::println(stderr, "Error: {}", ex.what());
        return false;
    }
}

int main(int argc, char* argv[])
{
    Options opt;
    switch(prepareOptions(opt, argc, argv))
    {
        case Command: return 0;
        case Error:   return 1;
        case Parsed:  break;
    }

    TLibrary library;
    return encode(opt) ? 0 : 1;
}