Convert audio formats

About audio file formats

The audio file format is a category of digital file formats for the representation of audio information along with its meta-data. Multiple audio file formats exist based on the nature of data contained within the audio file. Such files can be stored in compressed as well as uncompressed audio file formats. Popular audio file formats include MP3, WAV, PCM, and WMA.

Supported audio file conversions

FromTo
AACAudio: AAC, AC3, AIFF, FLAC, M4A, MP3, OGG, WAV, WMA
AC3Audio: AAC, AC3, AIFF, FLAC, M4A, MP3, OGG, WAV, WMA
AIFFAudio: AAC, AC3, AIFF, FLAC, M4A, MP3, OGG, WAV, WMA
FLACAudio: AAC, AC3, AIFF, FLAC, M4A, MP3, OGG, WAV, WMA
M4AAudio: AAC, AC3, AIFF, FLAC, M4A, MP3, OGG, WAV, WMA
MP3Audio: AAC, AC3, AIFF, FLAC, M4A, MP3, OGG, WAV, WMA
OGGAudio: AAC, AC3, AIFF, FLAC, M4A, MP3, OGG, WAV, WMA
WAVAudio: AAC, AC3, AIFF, FLAC, M4A, MP3, OGG, WAV, WMA
WMAAudio: AAC, AC3, AIFF, FLAC, M4A, MP3, OGG, WAV, WMA

Convert to another audio format

With GroupDocs.Conversion you can easily convert your audio file into another audio file format.

To allow audio conversions, GroupDocs.Conversion provides an extension point to offload actual audio conversion to an audio processing library, but at the same time gives you the simplicity of conversion setup. The extension point is the IAudioConnector interface.

First, you must decide which audio processing library you will use. Different libraries have different setup processes.

In our example, we will use the FFMPEG library. We recommend using Chocolatey to install the FFMPG library and all needed dependencies. To install FFMPEG with Chocolatey, run the following console command:

choco install ffmpeg

Once the audio processing library is installed, you must implement an IAudioConnector interface. For this implementation, the FFMpegCore NuGet package must be installed in your project. The following snippet provides a sample implementation:

public class AudioConnector : IAudioConnector
{
    private readonly Dictionary<AudioFileType, Action<FFMpegArgumentOptions>> _optionsMap = new Dictionary<AudioFileType, Action<FFMpegArgumentOptions>>();

    public AudioConnector()
    {
        _optionsMap.Add(AudioFileType.Mp3, SetMp3ConvertOptions);
        _optionsMap.Add(AudioFileType.Ogg, SetOggConvertOptions);
        _optionsMap.Add(AudioFileType.Aac, SetAacConvertOptions);
        _optionsMap.Add(AudioFileType.Ac3, SetAc3ConvertOptions);
        _optionsMap.Add(AudioFileType.Flac, SetFlacConvertOptions);
        _optionsMap.Add(AudioFileType.Wma, SetWmaConvertOptions);
        _optionsMap.Add(AudioFileType.Wav, SetWavConvertOptions);
        _optionsMap.Add(AudioFileType.M4a, SetM4aConvertOptions);
        _optionsMap.Add(AudioFileType.Aiff, SetAiffConvertOptions);
    }

    public Stream ConvertAudio(Stream sourceStream, AudioConvertOptions convertOptions)
    { 
        var resultStream = new MemoryStream();


        var arguments = FFMpegArguments
            .FromPipeInput(new StreamPipeSource(sourceStream))
            .OutputToPipe(new StreamPipeSink(resultStream), options =>
            {
                if (_optionsMap.ContainsKey(convertOptions.Format))
                {
                    _optionsMap[convertOptions.Format].Invoke(options);
                }
                else
                {
                    throw new InvalidOperationException(
                        $"Conversion to {convertOptions.Format.Extension} is not supported at the moment");
                }
            });
        arguments.ProcessSynchronously();
            
        return resultStream;
    }

    private void SetMp3ConvertOptions(FFMpegArgumentOptions options)
    {
        options.WithAudioCodec(AudioCodec.LibMp3Lame);
        options.ForceFormat("mp3");
    }

    private void SetOggConvertOptions(FFMpegArgumentOptions options)
    {
        options.WithAudioCodec(AudioCodec.LibVorbis);
        options.ForceFormat("ogg");
    }

    private void SetAacConvertOptions(FFMpegArgumentOptions options)
    {
        options.WithAudioCodec(AudioCodec.Aac);
        options.ForceFormat("adts");
    }

    private void SetAc3ConvertOptions(FFMpegArgumentOptions options)
    {
        options.WithAudioCodec(AudioCodec.Ac3);
        options.ForceFormat("ac3");
    }

    private void SetFlacConvertOptions(FFMpegArgumentOptions options)
    {
        options.ForceFormat("flac");
    }

    private void SetWmaConvertOptions(FFMpegArgumentOptions options)
    {
        options.ForceFormat("asf");
    }

    private void SetWavConvertOptions(FFMpegArgumentOptions options)
    {
        options.ForceFormat("wav");
    }

    private void SetM4aConvertOptions(FFMpegArgumentOptions options)
    {
        options.WithAudioCodec(AudioCodec.Aac);
        options.ForceFormat("adts");
    }

    private void SetAiffConvertOptions(FFMpegArgumentOptions options)
    {
        options.ForceFormat("aiff");
    }
}

Once the IAudioConnector interface is implemented, the MP3 to FLAC conversion code snippet looks like this:

// Load the source MP3 file
AudioLoadOptions loadOptions = new AudioLoadOptions();
loadOptions.SetAudioConnector(new AudioConnector());
using (Converter converter = new Converter("sample.mp3", () => loadOptions))
{
    // Set the convert options for FLAC format
    AudioConvertOptions options = new AudioConvertOptions {
        Format = AudioFileType.Flac
    };
    // Convert to FLAC format
    converter.Convert("converted.flac", options);
}

Put it simply - you install the audio processing library, implement the IAudioConnector interface which links GroupDocs.Conversion with the audio processing library, load an audio file into the Converter class providing the IAudioConnector instance, select the desired output format and GroupDocs.Conversion does all the rest.

Note
Refer to the API reference for more conversion options and customizations.