C# amr音頻格式文件轉換成mp3格式
微信語音都是使用amr音頻格式的,這種格式文件放網頁端是無法播放的,這時候需要后臺轉碼成mp3格式
使用silk_v3_decoder.exe 和 lame.exe 文件通過cmd命令來轉換成mp3格式
工具介紹: https://github.com/kn007/silk-v3-decoder/tree/master/windows
static void Main(string[] args) { //取得當前工作目錄的完整限定路徑 string currentWorkDir = Environment.CurrentDirectory; //silk文件完全路徑 string silkFilePath = Path.Combine(currentWorkDir, "silk", "silk_v3_decoder.exe"); //lame文件完全路徑 string lameFilePath = Path.Combine(currentWorkDir, "silk", "lame.exe"); //amr文件完全路徑 string amrFilePath = Path.Combine(currentWorkDir, "amr", "msg.amr"); //pcm文件完全路徑 string pcmFilePath = Path.Combine(currentWorkDir, "amr", "msg.pcm"); //mp3文件路徑 string mp3FilePath = Path.Combine(currentWorkDir, "amr", DateTime.Now.ToString("yyyyMMddHHmmss") + ".mp3"); //先生成pcm文件 CmdHelper.ExecuteCmd(lamePath + "silk_v3_decoder.exe ", amrFilePath + " " + pcmFilePath); //生成mp3文件 CmdHelper.ExecuteCmd(lamePath + "lame.exe", " -r -s 24000 --preset voice " + pcmFilePath + " " + mp3FilePath); //刪除臨時pcm文件 if (File.Exists(pcmFilePath)) { File.Delete(pcmFilePath); } Console.WriteLine("Hello World!"); Console.ReadKey(); } /// <summary> /// 執行CMD命令返回信息 /// </summary> /// <param name="Command">命令</param> /// <returns>返回命令執行結果</returns> public static void ExecuteCmd(string fileName, string arg) { //創建一個ProcessStartInfo對象 使用系統shell 指定命令和參數 設置標準輸出 var psi = new ProcessStartInfo(fileName, arg) { RedirectStandardOutput = true }; //var psi = new ProcessStartInfo(Command) { RedirectStandardOutput = true }; psi.UseShellExecute = false; //啟動 var proc = Process.Start(psi); if (proc == null) { Console.WriteLine("Can not exec."); } else { //開始讀取 using (var sr = proc.StandardOutput) { while (!sr.EndOfStream) { Console.WriteLine(sr.ReadLine()); } if (!proc.HasExited) { proc.Kill(); } } } }
執行完就會生成對應的mp3文件了
轉自 http://www.mikeblog.cn/article/details/112
本文來自博客園,作者:jevan,轉載請注明原文鏈接:http://www.rzrgm.cn/DoNetCShap/p/18451970

浙公網安備 33010602011771號