Vorbis Encoder (Run)¶
This article explains how to use Transcoder::run to encode a WAV file to a Vorbis OGG file.
The code snippets in this article are from the enc_vorbis_file macOS sample.
Linux and Windows samples are also available:
Source Audio¶
For source we use the equinox-48KHz.wav file from the AVBlocks Assets repository. After downloading and unzipping you will find equinox-48KHz.wav in the aud subdirectory.
Code¶
This code takes a WAV file and encodes it to Vorbis audio in an OGG container.
Initialize AVBlocks¶
The first step in any AVBlocks application is to initialize the library. This must be done before using any other AVBlocks functionality. Always call Library::shutdown() at the end of the program to clean up resources.
int main(int argc, char* argv[])
{
Options opt;
switch(prepareOptions(opt, argc, argv))
{
case Command: return 0;
case Error: return 1;
case Parsed: break;
}
Library::initialize();
bool result = encode(opt);
Library::shutdown();
return result ? 0 : 1;
}
Configure Input Socket¶
The input socket points directly to the WAV input file.
bool encode(Options& opt)
{
// create input socket
primo::ref<MediaSocket> inSocket (Library::createMediaSocket());
inSocket->setFile(primo::ustring(opt.inputFile));
// ...existing code...
}
Configure Output Socket¶
The output socket writes an OGG container with a Vorbis audio stream.
primo::ref<MediaSocket> createOutputSocket(Options& opt)
{
// create stream info to describe the output audio stream
auto asi = primo::make_ref(Library::createAudioStreamInfo());
asi->setStreamType(StreamType::Vorbis);
// The default bitrate is 128000. You can set it to 192000, 256000, etc.
// asi->setBitrate(192000);
// Optionally set the sampling rate and the number of the channels, e.g. 44.1 Khz, Mono
// asi->setSampleRate(44100);
// asi->setChannels(1);
// create a pin using the stream info
auto pin = primo::make_ref(Library::createMediaPin());
pin->setStreamInfo(asi.get());
// finally create a socket for the output container format which is OGG in this case
auto socket = primo::make_ref(Library::createMediaSocket());
socket->setStreamType(StreamType::OGG);
socket->pins()->add(pin.get());
// output to a file
auto output_file = primo::ustring(opt.outputFile);
socket->setFile(output_file);
return socket;
}
Configure Transcoder and Encode¶
After creating the input and output sockets, the sample creates a transcoder, enables demo mode, adds the sockets, removes any existing output file, opens the transcoder, runs the encode, and closes it.
bool encode(Options& opt)
{
// create input socket
primo::ref<MediaSocket> inSocket (Library::createMediaSocket());
inSocket->setFile(primo::ustring(opt.inputFile));
// create output socket
auto outSocket = createOutputSocket(opt);
// create transcoder
auto transcoder = primo::make_ref(Library::createTranscoder());
transcoder->setAllowDemoMode(true);
transcoder->inputs()->add(inSocket.get());
transcoder->outputs()->add(outSocket.get());
// transcoder will fail if output exists (by design)
deleteFile(primo::ustring(opt.outputFile));
if (!transcoder->open())
{
printError("Transcoder open", transcoder->error());
return false;
}
if (!transcoder->run())
{
printError("Transcoder run", transcoder->error());
return false;
}
transcoder->close();
return true;
}
Complete C++ Code¶
Here’s the complete working example from enc_vorbis_file.cpp:
#include <primo/avblocks/avb.h>
#include <primo/platform/reference++.h>
#include <primo/platform/error_facility.h>
#include <primo/platform/ustring.h>
#include "util.h"
#include "options.h"
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace primo::codecs;
using namespace primo::avblocks;
using namespace std;
primo::ref<MediaSocket> createOutputSocket(Options& opt)
{
// create stream info to describe the output audio stream
auto asi = primo::make_ref(Library::createAudioStreamInfo());
asi->setStreamType(StreamType::Vorbis);
// The default bitrate is 128000. You can set it to 192000, 256000, etc.
// asi->setBitrate(192000);
// Optionally set the sampling rate and the number of the channels, e.g. 44.1 Khz, Mono
// asi->setSampleRate(44100);
// asi->setChannels(1);
// create a pin using the stream info
auto pin = primo::make_ref(Library::createMediaPin());
pin->setStreamInfo(asi.get());
// finally create a socket for the output container format which is OGG in this case
auto socket = primo::make_ref(Library::createMediaSocket());
socket->setStreamType(StreamType::OGG);
socket->pins()->add(pin.get());
// output to a file
auto output_file = primo::ustring(opt.outputFile);
socket->setFile(output_file);
return socket;
}
bool encode(Options& opt)
{
// create input socket
primo::ref<MediaSocket> inSocket (Library::createMediaSocket());
inSocket->setFile(primo::ustring(opt.inputFile));
// create output socket
auto outSocket = createOutputSocket(opt);
// create transcoder
auto transcoder = primo::make_ref(Library::createTranscoder());
transcoder->setAllowDemoMode(true);
transcoder->inputs()->add(inSocket.get());
transcoder->outputs()->add(outSocket.get());
// transcoder will fail if output exists (by design)
deleteFile(primo::ustring(opt.outputFile));
if (!transcoder->open())
{
printError("Transcoder open", transcoder->error());
return false;
}
if (!transcoder->run())
{
printError("Transcoder run", transcoder->error());
return false;
}
transcoder->close();
return true;
}
int main(int argc, char* argv[])
{
Options opt;
switch(prepareOptions(opt, argc, argv))
{
case Command: return 0;
case Error: return 1;
case Parsed: break;
}
Library::initialize();
bool result = encode(opt);
Library::shutdown();
return result ? 0 : 1;
}
How to Run¶
See the build instructions for macOS and the enc_vorbis_file example for details.
Command Line¶
enc_vorbis_file --input <wav file> --output <ogg file>
Examples¶
List options:
./bin/x64/enc_vorbis_file --help
Usage: enc_vorbis_file --input <wav file> --output <ogg file>
-h, --help
-i, --input input WAV file
-o, --output output OGG file
Encode the input file ./assets/aud/equinox-48KHz.wav into output file ./output/enc_vorbis_file/equinox-48KHz.ogg:
mkdir -p ./output/enc_vorbis_file
./bin/x64/enc_vorbis_file \
--input ./assets/aud/equinox-48KHz.wav \
--output ./output/enc_vorbis_file/equinox-48KHz.ogg