Beispiel #1
0
 public void ChangeNumberOfAudioChannels_CreatesFile()
 {
     using (var file = TempFile.FromResource(Resources._2Channel, ".wav"))
     {
         var outputPath = file.Path.Replace(".wav", "1ch.wav");
         FFmpegRunner.ChangeNumberOfAudioChannels(file.Path, outputPath, 1, new NullProgress());
         Assert.IsTrue(File.Exists(outputPath));
     }
 }
Beispiel #2
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// It's up to the caller to close and dispose of the stream.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        public static WaveStream GetOneChannelStreamFromAudio(string audioFilePath,
                                                              out Exception error, IProgress progress)
        {
            error = null;

            try
            {
                var stream = new WaveFileReader(audioFilePath);
                if (stream.WaveFormat.Channels == 1 || !FFmpegRunner.HaveNecessaryComponents)
                {
                    return(stream);
                }
            }
            catch (Exception e)
            {
                error = e;
                return(null);
            }

            var tmpFile = Path.Combine(Path.GetTempPath(), Path.GetFileName(audioFilePath));

            if (File.Exists(tmpFile))
            {
                File.Delete(tmpFile);
            }

            try
            {
                FFmpegRunner.ChangeNumberOfAudioChannels(audioFilePath, tmpFile, 1, progress);

                // WaveFileReader does not read the entire file into a buffer right away. Therefore,
                // the file will remain open but we want to delete it right away. So read it
                // into our own stream and pass that to WaveFileReader so the file is no longer
                // needed and can be deleted.
                return(new WaveFileReader(new MemoryStream(File.ReadAllBytes(tmpFile))));
            }
            catch (Exception e)
            {
                error = e;
                return(null);
            }
            finally
            {
                if (File.Exists(tmpFile))
                {
                    try { File.Delete(tmpFile); }
                    catch { }
                }
            }
        }