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中找到你的网站,在右键--【属性】中看看使用的应用程序池是哪个,然后【在应用程序池】目录下找到它,右键---【属性】
找到【标识】选项卡,再找到【预定义账户】,在后边的下拉菜单中选择“本地系统”就可以了!
这样一来,你的网站就可以随心所欲的执行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:\wwwroot\company 浏览的页面路径为E:\wwwroot\company\news\show.asp 在show.asp页面中使用 Server.MapPath( "./" ) 返回路径为:E:\wwwroot\company\news Server.MapPath( "/" ) 返回路径为:E:\wwwroot Server.MapPath( "../" ) 返回路径为:E:\wwwroot\company Server.MapPath( "~/" ) 返回路径为:E:\wwwroot\company server.MapPath(request.ServerVariables( "Path_Info" )) Request.ServerVariables( "Path_Translated" ) 上面两种方式返回路径为 D:\wwwroot\company\news\show.asp
|
转载网址:
http://www. suxxxxxxxxchso.com/projecteactual/csharp-ffmpeg-demo-mp3-amr-mp4-asp.net.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?