福州专业网站设计公司,服装网站建设规划书,物联网应用系统开发,如果自己建立网站目录
需求
范例运行环境
可执行文件的设计
调用可执行文件方法
RunExecuteFile
RunShellExecuteFile
方法的区别
WEB调用举例
小结 需求
最近同事使用Python开发了一款智能文字转语音的程序#xff0c;经讨论部署在WINDOWS环境服务器下#xff0c;因此需要生成目标…目录
需求
范例运行环境
可执行文件的设计
调用可执行文件方法
RunExecuteFile
RunShellExecuteFile
方法的区别
WEB调用举例
小结 需求
最近同事使用Python开发了一款智能文字转语音的程序经讨论部署在WINDOWS环境服务器下因此需要生成目标为可执行程序文件即EXE文件。需要在WEB应用程序里进行调用并传递相关参数。
该测试效果如下图 打开AI语音合成配置如下 如图配置中可以选择朗读人角色音量大小音调高低和控制语速选项 此款应用将在合成音视频中起到关键作用。
范例运行环境
操作系统 Windows Server 2019 DataCenter
.net版本 .netFramework4.7.1 或以上
开发工具VS2019 C#
可执行文件的设计
可执行文件 edgetts.exe 实现文字转语音功能其说明如下
序号参数类型值说明1-filename字符存在的文件名 word docx文档 txt文本文件 md markdown文档 2-s角色固定值主播的角色值3-p字符固定值音调高低4-r1位小数数值0.1开始的倍速默认为1.05-v整数0到100音量大小
调用方法
edgetts.exe 要转换的文件名 [-s 声音参数 -p 音调参数 -r速度参数 -v 音量参数] 调用举例
edgetts d:\tts\test.txt
edgetts d:\tts\test.txt -s yunyang -p default -r 1.0 -v 100
调用说明
1、除要转换的文件名为必要参数外其他参数均有默认值 2、转换程序不要放在根目录下 3、转换程序在转换文本相同路径下生成同名的mp3文件 4、转换程序需要连接外网
调用可执行文件方法
需要引用 using System.Diagnostics;
程序集 System.Diagnostics.Process.dll 提供对本地和远程进程的访问权限并能够启动和停止本地系统进程。
包括两种方法方法包括需要调用的可执行文件名和可提供的参数
RunExecuteFile public string RunExecuteFile(string filename,string arguments){Process prc new Process();try{prc.StartInfo.FileName filename;prc.StartInfo.Arguments arguments;prc.StartInfo.UseShellExecute false;//输入输出重定向prc.StartInfo.RedirectStandardError true;prc.StartInfo.RedirectStandardInput true;prc.StartInfo.RedirectStandardOutput true;prc.StartInfo.CreateNoWindow false;prc.Start();//获得输出string output prc.StandardOutput.ReadLine();return output;}catch (Exception ex){if (!prc.HasExited){prc.Close();}return ex.Message.ToString();}return ;}
RunShellExecuteFile public string RunShellExecuteFile(string filename, string arguments)
{System.Diagnostics.Process prc new System.Diagnostics.Process();prc.StartInfo.FileName filename;prc.StartInfo.Arguments arguments;prc.StartInfo.UseShellExecute true;prc.StartInfo.CreateNoWindow true;prc.Start();prc.WaitForExit();return ;
}
方法的区别
主要区别在于 UseShellExecute 的属性的 true 或 false 。该属性获取或设置指示是否使用操作系统 shell 启动进程的值。
如果应在启动进程时使用 shell则为 true 如果直接从可执行文件创建进程则为 false 。 .NET Framework 应用默认值为 true 。为 true 的时候表示可以尝试调用一切可以调用的程序但不限于EXE文件。
WEB调用举例
根据前面AI语音合成图示可编写如下后端调用示例代码
protected void Button1_Click(object sender, EventArgs e){string tts D:\\tts\\edgetts.exe;string tts_para -s x_speaker.SelectedValue;if (x_volume.Text ! ){tts_para -v x_volume.Text;}if (x_rate.Text ! ){tts_para -r x_rate.Text;}if (x_pitch.SelectedValue ! default){tts_para -p x_pitch.SelectedValue;}string cdir Request.PhysicalApplicationPath \\test\\ai\\;string[] allfs Directory.GetFiles(cdir);for (int i 0; i allfs.Length; i){string mp3 allfs[i].ToLower();File.Delete(mp3);}string guid System.Guid.NewGuid().ToString().Replace(-, );string txtfile Request.PhysicalApplicationPath \\test\\ai\\guid.txt;SaveToFile(txtfile,debug.Text, false, Encoding.UTF8, 512);string mp3file Request.PhysicalApplicationPath \\test\\ai\\guid.mp3;string rvRunShellExecuteFile(tts, txtfile tts_para);if (File.Exists(mp3file)){testaudio.Style[display] ;testaudio.Attributes[src] https:// Request.Url.Host /bfile/ai/ guid .mp3;string imgurl https:// Request.Url.Host /test/ai/images/boy.jpg;if (x_speaker.SelectedValue xiaoxiao || x_speaker.SelectedValue xiaoyi || x_speaker.SelectedValue yunxia){imgurl https:// Request.Url.Host /test/ai/images/girl.jpg;}layer.options_yes document.getElementById(testaudio).play();layer.closeAll();;layer.open(img src\imgurl\ width200/语音合成成功, 点这里播放, ok);}else{debug.Text rv;layer.open(未找到文件 tts txtfile tts_para, 确定, ok);}}public string SaveToFile(string PathFile,string filecontent,bool append,System.Text.Encoding encodtype,int buffersize){string rv;StreamWriter dfnew StreamWriter (PathFile,append,encodtype,buffersize);try{df.Write(filecontent);df.Close();}catch(Exception e){rve.Message;df.Close();}finally{df.Close();}return rv;}//SaveToFile Function前端代码示例如下 div idh5panel runatserver stylemargin-top:-50px classlogin-box query-panel
div styletext-align:leftasp:HyperLink IDbackurl Text返回 onclicklayer.open({ type: 2, shadeClose: false, content: 正在返回页面,请稍候... }); NavigateUrl/cc/prods/media/msIndex.aspx runatserver/ /divh2asp:Label IDfnamelabel runatserver Text文字转语音AI合成测试/asp:Label/h2div classuser-box style color:White; text-align:center; margin-bottom:50px
brbrdiv classuser-box style display:none1; padding-top:10px;div styledisplay:flexasp:TextBox TextModeMultiLine Rows6 IDdebug Height100px TextHello!欢迎来到立德云 stylecolor:White; width:100%; background: #fff;display:none1; background-color:Black;filter:opacity(50%); runatserver/asp:TextBox /div/divaudio idtestaudio runatserver autoplayautoplay styledisplay:none controls /audiodiv classuser-box stylemargin-bottom:0px;display:flex;width:100%;justify-content:flex-end;input typebutton value打开AI语音合成配置 style border-radius:5px onclickdocument.getElementById(ai_profile).style.display //divdiv idai_profile classuser-box styledisplay:none; margin-top:0px;div classform-horizontal style margin-left:20px; border-style:solid; border-width:1px; border-radius:5px; padding-left :50px;div classform-group style margin-top:30px;label classcol-sm-1 control-label stylefont-size:12pt; text-align:center;朗读人角色/labeldiv classcol-sm-2asp:DropDownList IDx_speaker checkSchemanotnull noClear CssClassform-control cName音调 AUTOCOMPLETEoff required runatserverasp:ListItem Valuexiaoxiao晓晓/asp:ListItemasp:ListItem Valuexiaoyi晓依/asp:ListItemasp:ListItem Valueyunjian云健/asp:ListItemasp:ListItem Valueyunxi云溪/asp:ListItemasp:ListItem Valueyunxia云霞/asp:ListItemasp:ListItem SelectedTrue Valueyunyang云扬/asp:ListItem/asp:DropDownList/divlabel classcol-sm-1 control-label style font-size:12pt; text-align:center;音量/labeldiv classcol-sm-1asp:TextBox IDx_volume checkSchemanotnull Text100 noClear CssClassform-control cName音量 AUTOCOMPLETEoff required runatserver/asp:TextBox/div/divdiv classform-grouplabel classcol-sm-1 control-label style font-size:12pt; text-align:center;音调/labeldiv classcol-sm-2asp:DropDownList IDx_pitch checkSchemanotnull noClear CssClassform-control cName音调 AUTOCOMPLETEoff required runatserverasp:ListItemdefault/asp:ListItemasp:ListItemx-low/asp:ListItemasp:ListItemlow/asp:ListItemasp:ListItemmedium/asp:ListItemasp:ListItemhigh/asp:ListItemasp:ListItemx-high/asp:ListItem/asp:DropDownList/divlabel classcol-sm-1 control-label style font-size:12pt; text-align:center;语速/labeldiv classcol-sm-1asp:TextBox IDx_rate checkSchemanotnull Text1.0 noClear CssClassform-control cName语速 AUTOCOMPLETEoff required runatserver/asp:TextBox/div/div/div/divdiv classuser-box styletext-align:center; display:none1; padding-top:10px;div aligncenterasp:Button IDButton1 TextAI语音合成 OnClientClicklayer.open({ type: 2, shadeClose: false, content: 正在进行AI语音合成... }); stylewidth:30%; background-color:#1E90FF;color:White;border-color:#87CEFA;padding-left:10px; padding-right:10px CssClassform-control runatserver onclickButton1_Click //div/divdiv classuser-box styletext-align:center; display:nonevideo idcoplayer autoplayautoplay controlscontrols webkit-playsinline playsinline x5-playsinline x-webkit-airplayallow stylemargin: 0px auto; width:100% runatserver /videoa idb_rate onclickrate(this); style float:right; line-height:25px; margin-right:10px; color:#fff;display:none;1x/a /divdiv classann labelasp:Literal IDx_introduce runatserver//label/div/div
script srchttps://res2.wx.qq.com/open/js/jweixin-1.6.0.js/scriptscript typetext/javascript srchls.min.0.12.4.js /scriptscript typetext/javascript srctcplayer.v4.min.js /script
script typetext/javascript languagejavascript src/master/js/jquery.js /script!-- BASIC JS LIABRARY --/div
小结
在实际的应用中调用 RunShellExecuteFile 方法更加通用一些本示例调用 RunExecuteFile没有成功因协作需要我们需要尝试多种方法进行解决而不是要在第一时间要求其它团队更改设计。
layer弹出框的代码请参考我的上传资源layer 移动版弹出层组件的改造版
调用成功后会显示如下图 如图我们看到使用了 H5 的 video 控件进行了演示播放。
再次感谢您的阅读欢迎讨论指教