asp.net音頻轉換的.amr轉.mp3教程(利用ffmpeg轉換法)

來源:互聯網
上載者:User

前言

上篇文章已經跟大家分享了asp.net利用七牛轉換法將.amr轉.mp3的方法,當時也說了還有另外一種方法是利用ffmpeg轉換法,下面這篇文章就給大家詳細介紹這種方法。這種方法相對第一種來說,要簡單的多!

FFmpeg的名稱來自MPEG視頻編碼通訊協定,前面的“FF”代表“Fast Forward”,FFmpeg是一套可以用來記錄、轉換數字音頻、視頻,並能將其轉化為流的開源電腦程式。可以輕易地實現多種視頻格式之間的相互轉換。

ffmpeg轉換法

首先,你得下載個“ffmpeg.exe” 外掛程式,然後把它放到你的項目中,如下圖:

程式中會調用該檔案,以助於轉換音頻格式!

上代碼:

 
 代碼如下 複製代碼
usingSystem;
usingSystem.Threading;
usingSystem.IO;
usingSystem.Diagnostics;
usingSystem.Security;
 
publicpartialclasscowala_201512Chritmas_amrtest : System.Web.UI.Page
{
 protectedvoidPage_Load(objectsender, EventArgs e)
 {
    if(!IsPostBack)
    {
      changedPlay.Visible =false;
    }
 }
 
 protectedvoidFfmpeg_Click(objectsender, EventArgs e)
 {
 if(AmrFileUp.HasFile)
 {
  stringkey = AmrFileUp.FileName;
  stringsavepath = Server.MapPath("~/upload/amr/") + key;
  AmrFileUp.SaveAs(savepath);
 
  stringmp3SavePth = Server.MapPath("~/upload/mp3/") + key.Split('.')[0].ToString() +".mp3";
 
  if(!string.IsNullOrEmpty(ConvertToMp3(savepath, mp3SavePth)))
  {
  changedPlay.Visible =true;
  changedPlay.Attributes.Add("src","upload/mp3/"+ key.Split('.')[0].ToString() +".mp3");
  Response.Write("<script>alert('轉換成功!');</script>");
  }
 }
 }
 
 publicstringConvertToMp3(stringpathBefore,stringpathLater)
 {
 stringc = Server.MapPath("/ffmpeg/") +@"ffmpeg.exe -i "+ pathBefore +" "+ pathLater;
 stringstr = RunCmd(c);
 returnstr;
 }
 
 /// <summary>
 /// 執行Cmd命令
 /// </summary>
 privatestringRunCmd(stringc)
 {
 try
 {
  ProcessStartInfo info =newProcessStartInfo("cmd.exe");
  info.RedirectStandardOutput =false;
  info.UseShellExecute =false;
  Process p = Process.Start(info);
  p.StartInfo.UseShellExecute =false;
  p.StartInfo.RedirectStandardInput =true;
  p.StartInfo.RedirectStandardOutput =true;
  p.StartInfo.RedirectStandardError =true;
  p.Start();
  p.StandardInput.WriteLine(c);
  p.StandardInput.AutoFlush =true;
  Thread.Sleep(1000);
  p.StandardInput.WriteLine("exit");
  p.WaitForExit();
  stringoutStr = p.StandardOutput.ReadToEnd();
  p.Close();
 
  returnoutStr;
 }
 catch(Exception ex)
 {
  return"error"+ ex.Message;
 }
 }
}
 

接著來張效果圖:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.