Simple Audio Converter¶
Building a simple audio converter using C++ and the AVBlocks Plus TTranscoder.
Source Audio¶
For an audio source we use the kahvi011_kennybeltrey-hydrate.mp3 file from the Internet Archive. The original audio format is MPEG Audio Layer 3, 44.1 KHz, Joint Stereo, 136 Kbps, Variable Bit Rate
Overview¶
These snippets are fully functional audio converters. They take kahvi011_kennybeltrey-hydrate.mp3 as input and convert it to AAC, saved as kahvi011_kennybeltrey-hydrate.aac.
macOS¶
Code¶
#include <primo/avblocks/avb++.h>
#include <iostream>
using namespace primo::codecs;
using namespace primo::avblocks;
using namespace primo::avblocks::modern;
int main(int argc, const char *argv[]) {
TLibrary library;
auto inputFile = "kahvi011_kennybeltrey-hydrate.mp3";
auto outputFile = "kahvi011_kennybeltrey-hydrate.aac";
auto inputInfo = TMediaInfo().file(inputFile);
if (inputInfo.tryOpen()) {
TMediaSocket inputSocket(inputInfo);
// Configure the output from the AAC preset
TMediaSocket outputSocket(Preset::Audio::Generic::AAC);
outputSocket.file(outputFile);
// Create Transcoder and configure it with
// the input and output sockets
TTranscoder transcoder;
transcoder.addInput(inputSocket);
transcoder.addOutput(outputSocket);
// Allow demo mode for the transcoder when
// using the demo version of the library
transcoder.allowDemoMode(true);
// Run the transcoder
if (transcoder.tryOpen()) {
transcoder.run();
transcoder.close();
} else {
std::cerr << "transcoder.tryOpen() failed: "
<< transcoder.error().message()
<< std::endl;
}
}
return 0;
}
How to run¶
Follow the steps in Create a Command Line Tool using CMake on macOS, but use the code from this article.
Download the kahvi011_kennybeltrey-hydrate.mp3 song from the Internet Archive and save it in the project directory:
curl \
-L -o kahvi011_kennybeltrey-hydrate.mp3 \
https://archive.org/download/kahvi011/kahvi011_kennybeltrey-hydrate.mp3
Run the application. Wait a few seconds for the Transcoder to finish. The converted file kahvi011_kennybeltrey-hydrate.aac will be in the project directory.
Linux¶
Code¶
#include <primo/avblocks/avb++.h>
#include <iostream>
using namespace primo::codecs;
using namespace primo::avblocks;
using namespace primo::avblocks::modern;
int main(int argc, const char *argv[]) {
TLibrary library;
auto inputFile = "kahvi011_kennybeltrey-hydrate.mp3";
auto outputFile = "kahvi011_kennybeltrey-hydrate.aac";
auto inputInfo = TMediaInfo().file(inputFile);
if (inputInfo.tryOpen()) {
TMediaSocket inputSocket(inputInfo);
// Configure the output from the AAC preset
TMediaSocket outputSocket(Preset::Audio::Generic::AAC);
outputSocket.file(outputFile);
// Create Transcoder and configure it with
// the input and output sockets
TTranscoder transcoder;
transcoder.addInput(inputSocket);
transcoder.addOutput(outputSocket);
// Allow demo mode for the transcoder when
// using the demo version of the library
transcoder.allowDemoMode(true);
// Run the transcoder
if (transcoder.tryOpen()) {
transcoder.run();
transcoder.close();
} else {
std::cerr << "transcoder.tryOpen() failed: "
<< transcoder.error().message()
<< std::endl;
}
}
return 0;
}
How to run¶
Follow the steps in Create a Command Line Tool using CMake on Ubuntu, but use the code from this article.
Download the kahvi011_kennybeltrey-hydrate.mp3 song from the Internet Archive and save it in the project directory:
curl \
-L -o kahvi011_kennybeltrey-hydrate.mp3 \
https://archive.org/download/kahvi011/kahvi011_kennybeltrey-hydrate.mp3
Run the application. Wait a few seconds for the Transcoder to finish. The converted file kahvi011_kennybeltrey-hydrate.aac will be in the project directory.
Windows¶
Code¶
#include <primo/avblocks/avb++.h>
#include <windows.h>
#include <iostream>
using namespace primo::codecs;
using namespace primo::avblocks;
using namespace primo::avblocks::modern;
int wmain(int argc, wchar_t* argv[])
{
// needed for Windows Media Codecs
CoInitializeEx(nullptr, COINITBASE_MULTITHREADED);
{
TLibrary library;
auto inputFile = L"kahvi011_kennybeltrey-hydrate.mp3";
auto outputFile = L"kahvi011_kennybeltrey-hydrate.aac";
auto inputInfo = TMediaInfoW().file(inputFile);
if (inputInfo.tryOpen()) {
TMediaSocketW inputSocket(inputInfo);
// Configure the output from the AAC preset
TMediaSocketW outputSocket(Preset::Audio::Generic::AAC);
outputSocket.file(outputFile);
// Create Transcoder and configure it with
// the input and output sockets
TTranscoderW transcoder;
transcoder.addInput(inputSocket);
transcoder.addOutput(outputSocket);
// Allow demo mode for the transcoder when
// using the demo version of the library
transcoder.allowDemoMode(true);
// Run the transcoder
if (transcoder.tryOpen()) {
transcoder.run();
transcoder.close();
} else {
std::cerr << "transcoder.tryOpen() failed: "
<< transcoder.error().message()
<< std::endl;
}
}
}
CoUninitialize();
return 0;
}
How to run¶
Follow the steps in Create a Command Line Tool using CMake on Windows, but use the code from this article.
Download the kahvi011_kennybeltrey-hydrate.mp3 song from the Internet Archive and save it in the project directory:
Invoke-WebRequest `
-Uri https://archive.org/download/kahvi011/kahvi011_kennybeltrey-hydrate.mp3 `
-OutFile kahvi011_kennybeltrey-hydrate.mp3
Run the application. Wait a few seconds for the Transcoder to finish. The converted file kahvi011_kennybeltrey-hydrate.aac will be in the project directory.