一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

asp.net音频转换的.amr转.mp3教程(利用ffmpeg转换法)

时间:2016-12-30 编辑:简简单单 来源:一聚教程网

前言

上篇文章已经跟大家分享了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("");
  }
 }
 }
 
 publicstringConvertToMp3(stringpathBefore,stringpathLater)
 {
 stringc = Server.MapPath("/ffmpeg/") +@"ffmpeg.exe -i "+ pathBefore +" "+ pathLater;
 stringstr = RunCmd(c);
 returnstr;
 }
 
 ///
 /// 执行Cmd命令
 ///
 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;
 }
 }
}
 

接着来张效果图:

热门栏目