C# amr轉mp3 (ffmpeg)


剛開始是為了做微信朗讀投票,微信接口臨時音頻文件只保留三天,下載接口只提供amr格式音頻,當時想把臨時性音頻轉永久性素材來使用,結果微信不提供永久性音頻素材播放接口。只好把amr轉為mp3。。。。

     public bool ConvertToMp3(string pathBefore, string pathLater)
        {
            string c = System.Web.HttpContext.Current.Server.MapPath("/ffmpeg/") + @"ffmpeg.exe -i " + pathBefore + " " + pathLater;
            string output = "";
            RunCmd(c, out  output);
            if (!string.IsNullOrEmpty(output))
            {
                if (File.Exists(pathLater))
                    return true;
                else
                    return false;
            }
            else
                return false;
        }

        /// <summary>
        /// 執行Cmd命令
        /// </summary>
        private static void RunCmd(string cmd, out string output)
        {
            cmd = cmd.Trim().TrimEnd('&') + "&exit";
            using (Process p = new Process())
            {
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;        //是否使用操作系統shell啟動
                p.StartInfo.RedirectStandardInput = true;   //接受來自調用程序的輸入信息
                p.StartInfo.RedirectStandardOutput = true;  //由調用程序獲取輸出信息
                p.StartInfo.RedirectStandardError = true;   //重定向標准錯誤輸出
                p.StartInfo.CreateNoWindow = true;          //不顯示程序窗口
                p.Start();//啟動程序
                //向cmd窗口寫入命令
                p.StandardInput.WriteLine(cmd);
                p.StandardInput.AutoFlush = true;
                //獲取cmd窗口的輸出信息
                output = p.StandardOutput.ReadToEnd();
                p.WaitForExit();//等待程序執行完退出進程
                p.Close();
            }
        }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM