Vorbis Encoder (Run)

This article explains how you can use {transcoder-run-net} to encode a WAV file to a Vorbis OGG file.

The code snippets in this article are from the enc_vorbis_file .NET 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.

static int Main(string[] args)
{
    var opt = new Options();

    if (!opt.Prepare(args))
        return opt.Error ? (int)ExitCodes.OptionsError : (int)ExitCodes.Success;

    Library.Initialize();

    bool encodeResult = Encode(opt);

    Library.Shutdown();

    return encodeResult ? (int)ExitCodes.Success : (int)ExitCodes.EncodingError;
}

Configure Input Socket

The input socket points directly to the WAV input file.

    // create input socket
    MediaSocket inSocket = new MediaSocket();
    inSocket.File = opt.InputFile;

Configure Output Socket

The output socket writes an OGG container with a Vorbis audio stream.

static MediaSocket CreateOutputSocket(Options opt)
{
    // create stream info to describe the output audio stream
    AudioStreamInfo asi = new AudioStreamInfo();
    asi.StreamType = StreamType.Vorbis;

    // The default bitrate is 128000. You can set it to 192000, 256000, etc.
    // asi.Bitrate = 192000;

    // Optionally set the sampling rate and the number of the channels, e.g. 44.1 Khz, Mono 
    // asi.SampleRate = 44100;
    // asi.Channels = 1;

    // create a pin using the stream info 
    MediaPin pin = new MediaPin();
    pin.StreamInfo = asi;

    // finally create a socket for the output container format which is OGG in this case
    MediaSocket socket = new MediaSocket();
    socket.StreamType = StreamType.Ogg;

    socket.Pins.Add(pin);

    // output to a file
    socket.File = opt.OutputFile;

    return socket;
}

Configure and Run Transcoder

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.

static bool Encode(Options opt)
{
    // transcoder will fail if output exists (by design)
    DeleteFile(opt.OutputFile);

    // Create output directory if needed
    string outputDir = System.IO.Path.GetDirectoryName(opt.OutputFile);
    if (!string.IsNullOrEmpty(outputDir))
        System.IO.Directory.CreateDirectory(outputDir);

    // create input socket
    MediaSocket inSocket = new MediaSocket();
    inSocket.File = opt.InputFile;

    // create output socket
    MediaSocket outSocket = CreateOutputSocket(opt);

    // create Transcoder
    using (Transcoder transcoder = new Transcoder())
    {
        transcoder.AllowDemoMode = true;
        transcoder.Inputs.Add(inSocket);
        transcoder.Outputs.Add(outSocket);

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

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

        transcoder.Close();
    }

    return true;
}

Complete Code

Here’s the complete working example that demonstrates Vorbis encoding using AVBlocks for .NET.

using System;
using PrimoSoftware.AVBlocks;

namespace CliSample
{
    class Program
    {
        static int Main(string[] args)
        {
            var opt = new Options();

            if (!opt.Prepare(args))
                return opt.Error ? (int)ExitCodes.OptionsError : (int)ExitCodes.Success;

            Library.Initialize();

            // Set license information. Without this AVBlocks runs in Demo mode.
            // Library.SetLicense("<license-string>");

            bool encodeResult = Encode(opt);

            Library.Shutdown();

            return encodeResult ? (int)ExitCodes.Success : (int)ExitCodes.EncodingError;
        }

        static bool Encode(Options opt)
        {
            // transcoder will fail if output exists (by design)
            DeleteFile(opt.OutputFile);

            // Create output directory if needed
            string outputDir = System.IO.Path.GetDirectoryName(opt.OutputFile);
            if (!string.IsNullOrEmpty(outputDir))
                System.IO.Directory.CreateDirectory(outputDir);

            // create input socket
            MediaSocket inSocket = new MediaSocket();
            inSocket.File = opt.InputFile;

            // create output socket
            MediaSocket outSocket = CreateOutputSocket(opt);

            // create Transcoder
            using (Transcoder transcoder = new Transcoder())
            {
                transcoder.AllowDemoMode = true;
                transcoder.Inputs.Add(inSocket);
                transcoder.Outputs.Add(outSocket);

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

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

                transcoder.Close();
            }

            return true;
        }

        static void DeleteFile(string filename)
        {
            try
            {
                if (System.IO.File.Exists(filename))
                    System.IO.File.Delete(filename);
            }
            catch { }
        }

        static MediaSocket CreateOutputSocket(Options opt)
        {
            // create stream info to describe the output audio stream
            AudioStreamInfo asi = new AudioStreamInfo();
            asi.StreamType = StreamType.Vorbis;

            // The default bitrate is 128000. You can set it to 192000, 256000, etc.
            // asi.Bitrate = 192000;

            // Optionally set the sampling rate and the number of the channels, e.g. 44.1 Khz, Mono 
            // asi.SampleRate = 44100;
            // asi.Channels = 1;

            // create a pin using the stream info 
            MediaPin pin = new MediaPin();
            pin.StreamInfo = asi;

            // finally create a socket for the output container format which is OGG in this case
            MediaSocket socket = new MediaSocket();
            socket.StreamType = StreamType.Ogg;

            socket.Pins.Add(pin);

            // output to a file
            socket.File = opt.OutputFile;

            return socket;
        }

        static void PrintError(string action, ErrorInfo e)
        {
            if (action != null)
            {
                Console.Write("{0}: ", action);
            }

            if (ErrorFacility.Success == e.Facility)
            {
                Console.WriteLine("Success");
                return;
            }
            else
            {
                Console.WriteLine("{0}, facility:{1} code:{2} hint:{3}", e.Message ?? "", e.Facility, e.Code, e.Hint ?? "");
            }
        }

        enum ExitCodes : int
        {
            Success = 0,
            OptionsError = 1,
            EncodingError = 2,
        }
    }
}

How to Run

See the build instructions for macOS and the enc_vorbis_file .NET sample for details.

Command Line

enc_vorbis_file --input <wav file> --output <ogg file>

Examples

List options:

./bin/net10.0/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:

# Linux and macOS
mkdir -p ./output/enc_vorbis_file

./bin/net10.0/enc_vorbis_file \
  --input ./assets/aud/equinox-48KHz.wav \
  --output ./output/enc_vorbis_file/equinox-48KHz.ogg
# Windows
mkdir -Force -Path ./output/enc_vorbis_file

./bin/net10.0/enc_vorbis_file `
  --input ./assets/aud/equinox-48KHz.wav `
  --output ./output/enc_vorbis_file/equinox-48KHz.ogg