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¶
Create and configure a
TVideoStreamInfoobjectCreate a
TMediaPinobject and set its stream info withTMediaPin::streamInfo.Create a
TMediaSocketobject and add the pin withTMediaSocket::addPin.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¶
Create a
TMediaSocketobject for a predefinedPresetby passing the preset name to its constructorCall the
TMediaSocket::filemethod to set the output filenameAdd the socket to the transcoder with
TTranscoder::addOutput
TMediaSocket(opt.preset.name)
.file(opt.output_file)
Transcode¶
Call the
TTranscoder::openmethod.Call the
TTranscoder::runmethod.Call the
TTranscoder::closemethod.
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;
}