public Stream ConvertToMP3(string inputFile, ConvertOptions options)
        {
            var temp = Path.GetTempFileName();

            TemporaryFiles.Add(temp);
            Convert(inputFile, ".mp3", "-f mp3 " + options + " -y", temp);
            return(File.OpenRead(temp));
        }
        public Stream ConvertToHeaderlessWav(string inputFile, ConvertOptions options)
        {
            var temp = Path.GetTempFileName();

            TemporaryFiles.Add(temp);
            Convert(inputFile, ".wav", "-f s16le " + options + " -y", temp);
            return(File.OpenRead(temp));
        }
        public void ConvertToMP3Async(string inputFile, ConvertOptions options, object userState = null)
        {
            if (_async != null)
            {
                throw new InvalidOperationException("Conversion is already in progress.");
            }

            _async = AsyncOperationManager.CreateOperation(userState);
            ThreadPool.QueueUserWorkItem(x => OnConversionCompleted(new ConversionCompletedEventArgs {
                Output = ConvertToMP3(inputFile, options), State = _async.UserSuppliedState
            }));
        }
        public Stream ConvertToFLAC(string inputFile, ConvertOptions options)
        {
            // Convering to FLAC is a 2-way step. First we convert to WAV.
            var output = Convert(inputFile, ".wav", "-f wav -sample_fmt s16 " + options, null);

            if (output.Length == 0)
            {
                return(output);
            }
            FormatRepair.RepairWAVStream(output);

            // Offset the memory to temp folder.
            var temp = Path.GetTempFileName();
            var fs   = File.Open(temp, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);

            TemporaryFiles.Add(temp);

            // Next we encode using libFLAC's native encoder because FFmpeg unfortunately
            // does not support --no-padding and --no-seektable at the moment. *sigh*
            using (var reader = new WavReader(output))
                using (var writer = new FlacWriter(fs, reader.BitDepth, reader.Channels, reader.SampleRate))
                {
                    // Buffer for 1 second's worth of audio data
                    var buffer = new byte[1 * reader.Bitrate / 8];
                    int read;
                    do
                    {
                        OnConversionProgressChanged(new ConversionProgressChangedEventArgs {
                            SourceFormat = ".wav",
                            TargetFormat = ".flac",
                            Progress     = (int)(reader.InputStream.Position * 100 / reader.InputStream.Length)
                        });
                        read = reader.InputStream.Read(buffer, 0, buffer.Length);
                        writer.Write(buffer, 0, read);
                    } while (read > 0);
                }

            output.Close();
            return(fs);
        }