Last active
October 13, 2022 07:18
-
-
Save conholdate-docs-gists/1b294837ec1f35350d24301aed9cff17 to your computer and use it in GitHub Desktop.
Convert From MP3 in C# | https://docs.conholdate.com/net/convert-from-mp3/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment