Convert Audio

About Audio File Formats

The audio file format is a category of digital file formats for representation of audio information along with its meta-data. A number of 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.

Convert from MP3

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

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

You first must decide which audio processing library will use. Different libraries have different setup process.

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

choco install ffmpeg

Once audio processing library is installed you must implement IAudioConnector. For this implementation the FFMpegCore nuget package must be installed in your project. The following snippet provides 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 IAudioConnector is implemented MP3 to FLAC conversion code snippet will look 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 audio processing library, impement IAudioConnector which links GroupDocs.Conversion with audio processing library, load an audio file into Converter providing the IAudioConnector instance, select desired output format and all the rest will be done by GroupDocs.Conversion.

© Aspose Pty Ltd 2001-2022. All Rights Reserved.