C# 使用ffmpeg.exe进行音频转换完整demo-asp.net转换代码

上一篇说了在winform下进行调用cmd.exe执行ffmpeg.exe进行音频转换完整demo.后来我又需要移植这个方式到asp.net中,但是asp.net和winform程序有很多不同。

需要修改WavConvertToAmr的方法,支持asp.net

1、WavConvertToAmr修改执行权限:如果在windows server中可能会遇到权限问题,需要配置IIS权限:

   先从IIS中找到你的网站,在右键--【属性】中看看使用的应用程序池是哪个,然后【在应用程序池】目录下找到它,右键---【属性】

         找到【标识】选项卡,再找到【预定义账户】,在后边的下拉菜单中选择“本地系统”就可以了!

IT分享

         这样一来,你的网站就可以随心所欲的执行cmd命令了,其实不仅仅是执行cmd命令,简直是至高无上的权限!

         提醒一下,这样更改的是应用程序池权限,因此所有使用这个应用程序池的网站都有很高的权限,这是相当危险的,还须谨慎使用!!

这个方法存在危险,也通过设置执行的用户名和密码来设置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
   /// <summary>
        /// 执行Cmd命令
        /// </summary>
        private string  Cmd(string c)
        {
            try
            {
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.StartInfo.FileName = "cmd.exe";
                process.StartInfo.UseShellExecute = false;
                
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.UserName = "user";
                //构造用户密码,假定密码为123,必须一个字符一个字符的添加  
                System.Security.SecureString password = new System.Security.SecureString();
                password.AppendChar('p');
                password.AppendChar('a');
                password.AppendChar('s');
                password.AppendChar('s'); 
                process.StartInfo.Password = password;  
                  
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                process.Start();
                process.StandardInput.WriteLine(c);
                process.StandardInput.AutoFlush = true;
                Thread.Sleep(1000);
                process.StandardInput.WriteLine("exit");
                process.WaitForExit();
                //StreamReader reader = process.StandardOutput;//截取输出流           
                string outStr = process.StandardOutput.ReadToEnd();
         
          process.Close();
           
                return outStr;
            }
            catch (Exception ex)
            {
                return "error"+ex.Message;
            }
        }

注意:构造用户密码,假定密码为123,必须一个字符一个字符的添加  !

2、在asp.net中调用方式:

1
2
3
4
5
6
7
8
   protected void Button1_Click(object sender, EventArgs e)
    {
        string fileName = "d:\2222.amr";
        string targetFileName = "d:\2222.mp3";
        WavConvertAmr.WavConvertToAmr toamr = new WavConvertAmr.WavConvertToAmr();
        string remsg = toamr.ConvertToAmr(Server.MapPath("./ffmpeg/"), fileName, targetFileName);
        
    }

将ffmpeg.exe放在网站的目录ffmpeg的下面

在asp.net要注意ffmepg的路径:

1
2
3
4
5
6
7
8
9
10
11
12
13
./当前目录
/网站主目录
../上层目录
~/网站虚拟目录
 如果当前的网站目录为E:wwwroot   应用程序虚拟目录为E:wwwrootcompany 浏览的页面路径为E:wwwrootcompany ewsshow.asp
在show.asp页面中使用
Server.MapPath("./")   返回路径为:E:wwwrootcompany ews
 Server.MapPath("/")    返回路径为:E:wwwroot
 Server.MapPath("../")   返回路径为:E:wwwrootcompany
 Server.MapPath("~/")   返回路径为:E:wwwrootcompany
 server.MapPath(request.ServerVariables("Path_Info")) 
 Request.ServerVariables("Path_Translated")  
上面两种方式返回路径为 D:wwwrootcompany ewsshow.asp
原文地址:https://www.cnblogs.com/mili3/p/4701842.html