How to configure output socket for MP3 encoding in .NET#

Here is a quick snippet for configuring the output socket of a Transcoder for MP3 encoding using C#.

Diagram#

The Transcoder with an input MediaSocket and an output MediaSocket. The input MediaSocket only has a file. The output MediaSocket has a Pins connector that a MediaPin plugs into; the MediaPin holds an AudioStreamInfo with the StreamType and StreamSubType of the MP3 stream. The Transcoder with an input MediaSocket and an output MediaSocket. The input MediaSocket only has a file. The output MediaSocket has a Pins connector that a MediaPin plugs into; the MediaPin holds an AudioStreamInfo with the StreamType and StreamSubType of the MP3 stream.

Code#

This is the code for the output socket:

static MediaSocket CreateOutputSocket(Options opt)
{
    AudioStreamInfo asi = new AudioStreamInfo();
    asi.StreamType = StreamType.MpegAudio;
    asi.StreamSubType = StreamSubType.MpegAudioLayer3;

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

    // You can change the sampling rate and the number of the channels
    // asi.SampleRate = 44100;
    // asi.Channels = 1;

    MediaPin pin = new MediaPin();
    pin.StreamInfo = asi;

    MediaSocket socket = new MediaSocket();
    socket.StreamType = StreamType.MpegAudio;
    socket.StreamSubType = StreamSubType.MpegAudioLayer3;

    socket.Pins.Add(pin);

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

    return socket;
}

The complete program is available on GitHub:

Comments

comments powered by Disqus