Simplest Audio Converter

This topic is about building an audio converter in C#, C++, and Visual Studio.

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

Code

The following code snippet will take any audio format supported by AVBlocks and will convert it to AAC audio in an ADTS container (*.AAC).

.NET

using PrimoSoftware.AVBlocks;

namespace SimpleConverter
{
    class Program
    {
        static void Main(string[] args)
        {
            Library.Initialize();

            var inputInfo = new MediaInfo() {
                InputFile = "kahvi011_kennybeltrey-hydrate.mp3"
            };

            if (inputInfo.Load()) {
                var inputSocket = MediaSocket.FromMediaInfo(inputInfo);
                var outputSocket = MediaSocket.FromPreset(Preset.Audio.Generic.AAC);
                outputSocket.File = "kahvi011_kennybeltrey-hydrate.aac";

                using (var transcoder = new Transcoder()) {
                    transcoder.Inputs.Add(inputSocket);
                    transcoder.Outputs.Add(outputSocket);

                    if (transcoder.Open())
                    {
                        transcoder.Run();
                        transcoder.Close();
                    }
                }
            }

            Library.Shutdown();
        }
    }
}

How to run

Follow the steps to create a C# console application in Visual Studio, but in Program.cs use the code from this article.

Download the kahvi011_kennybeltrey-hydrate.mp3 song from the Internet Archive and save it in bin\x64\Debug under the project's directory.

Run the application in Visual Studio. Wait a few seconds for the Transcoder to finish. The converted file kahvi011_kennybeltrey-hydrate.aac will be in the bin\x64\Debug directory.

C++

Windows

// SimpleConverter.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

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

int _tmain(int argc, _TCHAR* argv[])
{
    // needed for Windows Media Codecs
    CoInitializeEx(nullptr, COINITBASE_MULTITHREADED);

    Library::initialize();

    auto inputInfo = primo::make_ref(Library::createMediaInfo());
    inputInfo->setInputFile(L"kahvi011_kennybeltrey-hydrate.mp3");

    if (inputInfo->load()) {
        auto inputSocket = primo::make_ref(
            Library::createMediaSocket(inputInfo.get())
        );

        auto outputSocket = primo::make_ref(
            Library::createMediaSocket(Preset::Audio::Generic::AAC)
        );
        outputSocket->setFile(L"kahvi011_kennybeltrey-hydrate.aac");

        auto transcoder = primo::make_ref(Library::createTranscoder());
        transcoder->inputs()->add(inputSocket.get());
        transcoder->outputs()->add(outputSocket.get());

        if (transcoder->open()) {
            transcoder->run();
            transcoder->close();
        }
    }

    Library::shutdown();

    CoUninitialize();

    return 0;
}
How to run

Follow the steps to create a C++ console application in Visual Studio, 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.

Run the application in Visual Studio. Wait a few seconds for the Transcoder to finish. The converted file kahvi011_kennybeltrey-hydrate.aac will be in the project directory.


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