Convert Raw Video To Compressed Video

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

enc_yuv_preset_file Sample

The code snippets in this article are from the enc_yuv_preset_file sample app. The complete source code of the enc_yuv_preset_file app is available in the following BitBucket repositories:

.NET

Create a Transcoder object

Use the standard new operator to create a Transcoder object.

using (var transcoder = new Transcoder())
{
    // Transcoder demo mode must be enabled, 
    // in order to use the OEM release for testing (without a valid license).
    transcoder.AllowDemoMode = true;

    // Code that uses Transcoder goes here
}

Configure inputs and outputs

Use the MediaSocket class to configure media sockets and the MediaPin class to configure media pins.

Configure video input

  1. Create and configure a VideoStreamInfo object
  2. Create a MediaPin object and set the MediaPin.StreamInfo property.
  3. Create a MediaSocket object and add the pin to MediaSocket.Pins.
  4. Add the socket to Transcoder.Inputs.
// Configure input
// The input stream frame rate determines the playback speed
var instream = new VideoStreamInfo {
    StreamType = PrimoSoftware.AVBlocks.StreamType.UncompressedVideo,
    FrameRate = opt.YuvFps, 
    FrameWidth = opt.YuvWidth,
    FrameHeight = opt.YuvHeight,
    ColorFormat = opt.YuvColor.Id,
    ScanType = ScanType.Progressive
};

var inpin = new MediaPin {
    StreamInfo = instream
};

var insocket = new MediaSocket {
    StreamType = PrimoSoftware.AVBlocks.StreamType.UncompressedVideo,
    File = opt.YuvFile
};

insocket.Pins.Add(inpin);

transcoder.Inputs.Add(insocket);

Configure video output

  1. Call the MediaSocket.FromPreset static method to create a MediaSocket object for a predefined preset
  2. Set the MediaSocket.File property to the output filename.
  3. Add the socket to Transcoder.Outputs.
// Configure output
var outsocket = MediaSocket.FromPreset(opt.OutputPreset.Name);
outsocket.File = opt.OutputFile;

transcoder.Outputs.Add(outsocket);

Transcode

  1. Call the Transcoder.Open method.
  2. Call the Transcoder.Run method.
  3. Call the Transcoder.Close method.
bool res = transcoder.Open();
PrintError("Open Transcoder", transcoder.Error);
if (!res)
    return false;

res = transcoder.Run();
PrintError("Run Transcoder", transcoder.Error);
if (!res)
    return false;

transcoder.Close();

Complete .NET code

static bool Encode(Options opt)
{
    using (var transcoder = new Transcoder())
    {
        // Transcoder demo mode must be enabled, 
        // in order to use the OEM release for testing (without a valid license).
        transcoder.AllowDemoMode = true;

        // Configure input
        // The input stream frame rate determines the playback speed
        var instream = new VideoStreamInfo {
            StreamType = PrimoSoftware.AVBlocks.StreamType.UncompressedVideo,
            FrameRate = opt.YuvFps, 
            FrameWidth = opt.YuvWidth,
            FrameHeight = opt.YuvHeight,
            ColorFormat = opt.YuvColor.Id,
            ScanType = ScanType.Progressive
        };

        var inpin = new MediaPin {
            StreamInfo = instream
        };

        var insocket = new MediaSocket {
            StreamType = PrimoSoftware.AVBlocks.StreamType.UncompressedVideo,
            File = opt.YuvFile
        };

        insocket.Pins.Add(inpin);

        transcoder.Inputs.Add(insocket);

        // Configure output
        var outsocket = MediaSocket.FromPreset(opt.OutputPreset.Name);
        outsocket.File = opt.OutputFile;

        transcoder.Outputs.Add(outsocket);

        bool res = transcoder.Open();
        PrintError("Open Transcoder", transcoder.Error);
        if (!res)
            return false;

        res = transcoder.Run();
        PrintError("Run Transcoder", transcoder.Error);
        if (!res)
            return false;

        transcoder.Close();
    }

    return true;
}

C++

Create a Transcoder object

Use the createTranscoder function from the primo::avblocks::Library namespace to create a new Transcoder* object. When the object is no longer needed, free it with the release method.

auto transcoder = primo::make_ref(Library::createTranscoder());

// Transcoder demo mode must be enabled, 
// in order to use the production release for testing (without a valid license)
transcoder->setAllowDemoMode(TRUE);

Configure inputs and outputs

Use the MediaSocket C++ interface to configure media sockets and the MediaPin C++ interface to configure media pins.

Configure video input

  1. Create and configure a VideoStreamInfo* object
  2. Create a MediaPin* object and set its stream info with MediaPin::setStreamInfo.
  3. Create a MediaSocket* object and add the pin to MediaSocket::pins.
  4. Add the socket to Transcoder::inputs.
// Configure input
// The input stream frame rate determines the playback speed
{
    auto instream = primo::make_ref(Library::createVideoStreamInfo());
    instream->setStreamType(StreamType::UncompressedVideo);
    instream->setFrameRate(opt.yuv_fps);
    instream->setFrameWidth(opt.yuv_width);
    instream->setFrameHeight(opt.yuv_height);
    instream->setColorFormat(opt.yuv_color.Id);
    instream->setScanType(ScanType::Progressive);

    auto inpin = primo::make_ref(Library::createMediaPin());
    inpin->setStreamInfo(instream.get());

    auto insocket = primo::make_ref(Library::createMediaSocket());
    insocket->setStreamType(StreamType::UncompressedVideo);
    insocket->setFile(opt.yuv_file.c_str());
    insocket->pins()->add(inpin.get());

    transcoder->inputs()->add(insocket.get());
}

Configure video output

  1. Call the Library::createMediaSocket static method to create a MediaSocket object for a predefined preset
  2. Call the MediaSocket::setFile method to set the output filename.
  3. Add the socket to Transcoder::outputs.
// Configure output
{
    auto outsocket = primo::make_ref(Library::createMediaSocket(opt.preset.name));
    outsocket->setFile(opt.output_file.c_str());

    transcoder->outputs()->add(outsocket.get());
}

Transcode

  1. Call the Transcoder::open method.
  2. Call the Transcoder::run method.
  3. Call the Transcoder::close method.
bool_t res = transcoder->open();
printError(L"Open Transcoder", transcoder->error());
if (!res)
    return false;

res = transcoder->run();
printError(L"Run Transcoder", transcoder->error());
if (!res)
    return false;

transcoder->close();

Complete C++ code

bool encode(const Options& opt)
{
    auto transcoder = primo::make_ref(Library::createTranscoder());

    // Transcoder demo mode must be enabled, 
    // in order to use the production release for testing (without a valid license)
    transcoder->setAllowDemoMode(TRUE);

    // Configure input
    // The input stream frame rate determines the playback speed
    {
        auto instream = primo::make_ref(Library::createVideoStreamInfo());
        instream->setStreamType(StreamType::UncompressedVideo);
        instream->setFrameRate(opt.yuv_fps);
        instream->setFrameWidth(opt.yuv_width);
        instream->setFrameHeight(opt.yuv_height);
        instream->setColorFormat(opt.yuv_color.Id);
        instream->setScanType(ScanType::Progressive);

        auto inpin = primo::make_ref(Library::createMediaPin());
        inpin->setStreamInfo(instream.get());

        auto insocket = primo::make_ref(Library::createMediaSocket());
        insocket->setStreamType(StreamType::UncompressedVideo);
        insocket->setFile(opt.yuv_file.c_str());
        insocket->pins()->add(inpin.get());

        transcoder->inputs()->add(insocket.get());
    }

    // Configure output
    {
        auto outsocket = primo::make_ref(Library::createMediaSocket(opt.preset.name));
        outsocket->setFile(opt.output_file.c_str());

        transcoder->outputs()->add(outsocket.get());
    }

    bool_t res = transcoder->open();
    printError(L"Open Transcoder", transcoder->error());
    if (!res)
        return false;

    res = transcoder->run();
    printError(L"Run Transcoder", transcoder->error());
    if (!res)
    {
        transcoder->close();
        return false;
    }

    transcoder->close();

    return true;
}


Last updated on April 8th, 2017 02:55:54 PM