NAuduio音頻格式轉換

MP3轉wav音頻

using System;
using System.Collections.Generic;
using System.Text;
using NAudio;
using NAudio.Wave;

namespace Mp3ToWav
{
    class Program
    {
        static void Main(string[] args)
        {
            string mp3file;
            do 
            {
                do
                {
                    Console.Out.Write("Please enter the mp3 path:");
                    mp3file = Console.In.ReadLine();//讀取MP3文件路徑
                }
                while (!System.IO.File.Exists(mp3file));
            }
            while (!mp3file.EndsWith(".mp3")) ;
            string wavfile = mp3file.Replace(".mp3", ".wav");//生成輸出的wav文件路徑
            string wavpath = wavfile;
            //獲取音頻文件名稱以在控制檯中顯示。
            int index = wavfile.LastIndexOf("\\");
            string wavname = wavfile.Substring(index + 1, wavfile.Length - index - 1);
            index = mp3file.LastIndexOf("\\");
            string mp3name = mp3file.Substring(index + 1, mp3file.Length - index - 1);
            Console.Out.WriteLine("Converting {0} to {1}", mp3name, wavname);
            //第1步:使用Mp3FileReader讀取MP3文件。
            using (Mp3FileReader reader = new Mp3FileReader(mp3file))
            {
                //第2步:使用CreatePcmStream方法獲取wave流。
                using (WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(reader))
                {
                    //第3步:用WaveFileWriter將波形數據寫入文件。
                    WaveFileWriter.CreateWaveFile(wavfile, pcmStream);
                }
            }
            Console.Out.WriteLine("Conversion finish and wav is saved at {0}.\nPress any key to finish.", wavpath);
            Console.In.ReadLine();
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章