需求
最近同事使用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 | -r | 1位小数数值 | 0.1开始的倍速 | 默认为1.0 |
| 5 | -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 rv=runshellexecutefile(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+"\" width=200/>语音合成成功!", "'点这里播放'", "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 df=new streamwriter (pathfile,append,encodtype,buffersize);
try
{
df.write(filecontent);
df.close();
}
catch(exception e)
{
rv=e.message;
df.close();
}
finally
{
df.close();
}
return rv;
}//savetofile function前端代码示例如下:
<div id="h5panel" runat="server" style="margin-top:-50px" class="login-box query-panel">
<div style="text-align:left"><asp:hyperlink id="backurl" text="返回" onclick="layer.open({ type: 2, shadeclose: false, content: '正在返回页面,请稍候...' });" navigateurl="/cc/prods/media/msindex.aspx" runat="server"/> </div>
<h2>
<asp:label id="fnamelabel" runat="server" text="文字转语音ai合成测试"></asp:label></h2>
<div class="user-box" style=" color:white; text-align:center; margin-bottom:50px">
<br><br>
<div class="user-box" style=" display:none1; padding-top:10px;">
<div style="display:flex">
<asp:textbox textmode="multiline" rows="6" id="debug" height="100px" text="hello!欢迎来到立德云!" style="color:white; width:100%; background: #fff;display:none1; background-color:black;filter:opacity(50%);" runat="server"></asp:textbox>
</div>
</div>
<audio id="testaudio" runat="server" autoplay="autoplay" style="display:none" controls> </audio>
<div class="user-box" style="margin-bottom:0px;display:flex;width:100%;justify-content:flex-end;">
<input type="button" value="打开ai语音合成配置" style=" border-radius:5px" onclick="document.getelementbyid('ai_profile').style.display=''" />
</div>
<div id="ai_profile" class="user-box" style="display:none; margin-top:0px;">
<div class="form-horizontal" style=" margin-left:20px; border-style:solid; border-width:1px; border-radius:5px; padding-left :50px;">
<div class="form-group" style=" margin-top:30px;">
<label class="col-sm-1 control-label" style="font-size:12pt; text-align:center;">
朗读人角色
</label>
<div class="col-sm-2">
<asp:dropdownlist id="x_speaker" checkschema="notnull" noclear cssclass="form-control" cname="音调" autocomplete="off" required="" runat="server">
<asp:listitem value="xiaoxiao">晓晓</asp:listitem>
<asp:listitem value="xiaoyi">晓依</asp:listitem>
<asp:listitem value="yunjian">云健</asp:listitem>
<asp:listitem value="yunxi">云溪</asp:listitem>
<asp:listitem value="yunxia">云霞</asp:listitem>
<asp:listitem selected="true" value="yunyang">云扬</asp:listitem>
</asp:dropdownlist>
</div>
<label class="col-sm-1 control-label" style=" font-size:12pt; text-align:center;">
音量
</label>
<div class="col-sm-1">
<asp:textbox id="x_volume" checkschema="notnull" text="100" noclear cssclass="form-control" cname="音量" autocomplete="off" required="" runat="server">
</asp:textbox>
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label" style=" font-size:12pt; text-align:center;">
音调
</label>
<div class="col-sm-2">
<asp:dropdownlist id="x_pitch" checkschema="notnull" noclear cssclass="form-control" cname="音调" autocomplete="off" required="" runat="server">
<asp:listitem>default</asp:listitem>
<asp:listitem>x-low</asp:listitem>
<asp:listitem>low</asp:listitem>
<asp:listitem>medium</asp:listitem>
<asp:listitem>high</asp:listitem>
<asp:listitem>x-high</asp:listitem>
</asp:dropdownlist>
</div>
<label class="col-sm-1 control-label" style=" font-size:12pt; text-align:center;">
语速
</label>
<div class="col-sm-1">
<asp:textbox id="x_rate" checkschema="notnull" text="1.0" noclear cssclass="form-control" cname="语速" autocomplete="off" required="" runat="server">
</asp:textbox>
</div>
</div>
</div>
</div>
<div class="user-box" style="text-align:center; display:none1; padding-top:10px;">
<div align="center">
<asp:button id="button1" text="ai语音合成" onclientclick="layer.open({ type: 2, shadeclose: false, content: '正在进行ai语音合成...' });"
style="width:30%; background-color:#1e90ff;color:white;border-color:#87cefa;padding-left:10px; padding-right:10px"
cssclass="form-control" runat="server" onclick="button1_click" />
</div>
</div>
<div class="user-box" style="text-align:center; display:none">
<video id="coplayer" autoplay="autoplay" controls="controls" webkit-playsinline playsinline x5-playsinline x-webkit-airplay="allow" style="margin: 0px auto; width:100%" runat="server" ></video>
<a id="b_rate" onclick="rate(this);" style=" float:right; line-height:25px; margin-right:10px; color:#fff;display:none;">1x</a>
</div>
<div class="ann" >
<label><asp:literal id="x_introduce" runat="server"/></label>
</div>
</div>
<script src="https://res2.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
<script type="text/javascript" src="hls.min.0.12.4.js"> </script>
<script type="text/javascript" src="tcplayer.v4.min.js"> </script>
<script type="text/javascript" language="javascript" src="/master/js/jquery.js" ></script><!-- basic js liabrary -->
</div>小结
在实际的应用中,调用 runshellexecutefile 方法更加通用一些,本示例调用 runexecutefile没有成功,因协作需要,我们需要尝试多种方法进行解决,而不是要在第一时间要求其它团队更改设计。
调用成功后会显示如下图:


如图我们看到使用了 h5 的 video 控件进行了演示播放。
到此这篇关于c#调用exe文件的方法详解的文章就介绍到这了,更多相关c#调用exe内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论