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.
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:
publicclassAudioConnector:IAudioConnector{privatereadonlyDictionary<AudioFileType,Action<FFMpegArgumentOptions>>_optionsMap=newDictionary<AudioFileType,Action<FFMpegArgumentOptions>>();publicAudioConnector(){_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);}publicStreamConvertAudio(StreamsourceStream,AudioConvertOptionsconvertOptions){varresultStream=newMemoryStream();vararguments=FFMpegArguments.FromPipeInput(newStreamPipeSource(sourceStream)).OutputToPipe(newStreamPipeSink(resultStream),options=>{if(_optionsMap.ContainsKey(convertOptions.Format)){_optionsMap[convertOptions.Format].Invoke(options);}else{thrownewInvalidOperationException($"Conversion to {convertOptions.Format.Extension} is not supported at the moment");}});arguments.ProcessSynchronously();returnresultStream;}privatevoidSetMp3ConvertOptions(FFMpegArgumentOptionsoptions){options.WithAudioCodec(AudioCodec.LibMp3Lame);options.ForceFormat("mp3");}privatevoidSetOggConvertOptions(FFMpegArgumentOptionsoptions){options.WithAudioCodec(AudioCodec.LibVorbis);options.ForceFormat("ogg");}privatevoidSetAacConvertOptions(FFMpegArgumentOptionsoptions){options.WithAudioCodec(AudioCodec.Aac);options.ForceFormat("adts");}privatevoidSetAc3ConvertOptions(FFMpegArgumentOptionsoptions){options.WithAudioCodec(AudioCodec.Ac3);options.ForceFormat("ac3");}privatevoidSetFlacConvertOptions(FFMpegArgumentOptionsoptions){options.ForceFormat("flac");}privatevoidSetWmaConvertOptions(FFMpegArgumentOptionsoptions){options.ForceFormat("asf");}privatevoidSetWavConvertOptions(FFMpegArgumentOptionsoptions){options.ForceFormat("wav");}privatevoidSetM4aConvertOptions(FFMpegArgumentOptionsoptions){options.WithAudioCodec(AudioCodec.Aac);options.ForceFormat("adts");}privatevoidSetAiffConvertOptions(FFMpegArgumentOptionsoptions){options.ForceFormat("aiff");}}
Once the IAudioConnector interface is implemented, the MP3 to FLAC conversion code snippet looks like this:
With v24.10 and later:
// Load the source MP3 fileAudioLoadOptionsloadOptions=newAudioLoadOptions();loadOptions.SetAudioConnector(newAudioConnector());using(Converterconverter=newConverter("sample.mp3",(LoadContextloadContext)=>loadOptions)){// Set the convert options for FLAC formatAudioConvertOptionsoptions=newAudioConvertOptions{Format=AudioFileType.Flac};// Convert to FLAC formatconverter.Convert("converted.flac",options);}
Before v24.10:
// Load the source MP3 fileAudioLoadOptionsloadOptions=newAudioLoadOptions();loadOptions.SetAudioConnector(newAudioConnector());using(Converterconverter=newConverter("sample.mp3",()=>loadOptions)){// Set the convert options for FLAC formatAudioConvertOptionsoptions=newAudioConvertOptions{Format=AudioFileType.Flac};// Convert to FLAC formatconverter.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.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.