C#調(diào)用exe文件的方法詳解
需求
最近同事使用Python開發(fā)了一款智能文字轉(zhuǎn)語音的程序,經(jīng)討論部署在WINDOWS環(huán)境服務(wù)器下,因此需要生成目標為可執(zhí)行程序文件,即EXE文件。需要在WEB應(yīng)用程序里進行調(diào)用,并傳遞相關(guān)參數(shù)。
該測試效果如下圖:
打開AI語音合成配置如下:
如圖配置中,可以選擇朗讀人角色,音量大小,音調(diào)高低和控制語速選項, 此款應(yīng)用將在合成音視頻中起到關(guān)鍵作用。
范例運行環(huán)境
操作系統(tǒng): Windows Server 2019 DataCenter
.net版本: .netFramework4.7.1 或以上
開發(fā)工具:VS2019 C#
可執(zhí)行文件的設(shè)計
可執(zhí)行文件 edgetts.exe 實現(xiàn)文字轉(zhuǎn)語音功能,其說明如下:
序號 | 參數(shù) | 類型 | 值 | 說明 |
---|---|---|---|---|
1 | -filename | 字符 | 存在的文件名 | word docx文檔 txt文本文件 md markdown文檔 |
2 | -s | 角色 | 固定值 | 主播的角色值 |
3 | -p | 字符 | 固定值 | 音調(diào)高低 |
4 | -r | 1位小數(shù)數(shù)值 | 0.1開始的倍速 | 默認為1.0 |
5 | -v | 整數(shù) | 0到100 | 音量大小 |
調(diào)用方法:
edgetts.exe 要轉(zhuǎn)換的文件名 [-s 聲音參數(shù) -p 音調(diào)參數(shù) -r速度參數(shù) -v 音量參數(shù)]
調(diào)用舉例:
edgetts d:\tts\test.txt
edgetts d:\tts\test.txt -s yunyang -p default -r 1.0 -v 100
調(diào)用說明:
1、除要轉(zhuǎn)換的文件名為必要參數(shù)外,其他參數(shù)均有默認值
2、轉(zhuǎn)換程序不要放在根目錄下
3、轉(zhuǎn)換程序在轉(zhuǎn)換文本相同路徑下生成同名的mp3文件
4、轉(zhuǎn)換程序需要連接外網(wǎng)
調(diào)用可執(zhí)行文件方法
需要引用 using System.Diagnostics;
程序集 System.Diagnostics.Process.dll 提供對本地和遠程進程的訪問權(quán)限并能夠啟動和停止本地系統(tǒng)進程。
包括兩種方法,方法包括需要調(diào)用的可執(zhí)行文件名和可提供的參數(shù):
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 ""; }
方法的區(qū)別
主要區(qū)別在于 UseShellExecute 的屬性的 true 或 false 。該屬性獲取或設(shè)置指示是否使用操作系統(tǒng) shell 啟動進程的值。
如果應(yīng)在啟動進程時使用 shell,則為 true ;如果直接從可執(zhí)行文件創(chuàng)建進程,則為 false 。 .NET Framework 應(yīng)用默認值為 true 。為 true 的時候表示可以嘗試調(diào)用一切可以調(diào)用的程序,但不限于EXE文件。
WEB調(diào)用舉例
根據(jù)前面AI語音合成圖示,可編寫如下后端調(diào)用示例代碼:
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="文字轉(zhuǎn)語音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="音調(diào)" 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;"> 音調(diào) </label> <div class="col-sm-2"> <asp:DropDownList ID="x_pitch" checkSchema="notnull" noClear CssClass="form-control" cName="音調(diào)" 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>
小結(jié)
在實際的應(yīng)用中,調(diào)用 RunShellExecuteFile 方法更加通用一些,本示例調(diào)用 RunExecuteFile沒有成功,因協(xié)作需要,我們需要嘗試多種方法進行解決,而不是要在第一時間要求其它團隊更改設(shè)計。
調(diào)用成功后會顯示如下圖:
如圖我們看到使用了 H5 的 video 控件進行了演示播放。
到此這篇關(guān)于C#調(diào)用exe文件的方法詳解的文章就介紹到這了,更多相關(guān)C#調(diào)用exe內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用GZipStream解壓縮數(shù)據(jù)文件的方法
這篇文章主要介紹了C#使用GZipStream解壓縮數(shù)據(jù)文件的方法,實例分析了C#中GZipStream方法的原理與使用技巧,需要的朋友可以參考下2015-04-04C# winfrom異步加載數(shù)據(jù)不影響窗體UI的操作方法
這篇文章主要介紹了C# winfrom 異步加載數(shù)據(jù)不影響窗體UI,在 WinForms 應(yīng)用程序中,如果數(shù)據(jù)加載是一個比較耗時的操作,直接在主線程中進行加載會導(dǎo)致 UI 凍結(jié),這是因為 WinForms 的 UI 是單線程的,主線程被阻塞時就無法處理其他 UI 相關(guān)的任務(wù)2024-12-12C# Onnx CenterNet實現(xiàn)目標檢測的示例詳解
這篇文章主要為大家詳細介紹了C# Onnx CenterNet實現(xiàn)目標檢測的相關(guān)知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12