C# Web应用调用EXE文件的一些实践

简介:

需求

最近同事使用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

整数



音量大小

调用方法:


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没有成功,因协作需要,我们需要尝试多种方法进行解决,而不是要在第一时间要求其它团队更改设计。



相关文章
|
29天前
|
前端开发 算法 API
构建高性能图像处理Web应用:Next.js与TailwindCSS实践
本文分享了构建在线图像黑白转换工具的技术实践,涵盖技术栈选择、架构设计与性能优化。项目采用Next.js提供优秀的SSR性能和SEO支持,TailwindCSS加速UI开发,WebAssembly实现高性能图像处理算法。通过渐进式处理、WebWorker隔离及内存管理等策略,解决大图像处理性能瓶颈,并确保跨浏览器兼容性和移动设备优化。实际应用案例展示了其即时处理、高质量输出和客户端隐私保护等特点。未来计划引入WebGPU加速、AI增强等功能,进一步提升用户体验。此技术栈为Web图像处理应用提供了高效可行的解决方案。
|
29天前
|
存储 算法 安全
如何控制上网行为——基于 C# 实现布隆过滤器算法的上网行为管控策略研究与实践解析
在数字化办公生态系统中,企业对员工网络行为的精细化管理已成为保障网络安全、提升组织效能的核心命题。如何在有效防范恶意网站访问、数据泄露风险的同时,避免过度管控对正常业务运作的负面影响,构成了企业网络安全领域的重要研究方向。在此背景下,数据结构与算法作为底层技术支撑,其重要性愈发凸显。本文将以布隆过滤器算法为研究对象,基于 C# 编程语言开展理论分析与工程实践,系统探讨该算法在企业上网行为管理中的应用范式。
60 8
|
13天前
|
缓存 前端开发 应用服务中间件
Web端实时通信技术SSE在携程机票业务中的实践应用
本文介绍了携程机票前端基于Server-Sent Events(SSE)实现服务端推送的企业级全链路通用技术解决方案。文章深入探讨了 SSE 技术在应用过程中包括方案对比、技术选型、链路层优化以及实际效果等多维度的技术细节,为类似使用场景提供普适性参考和借鉴。
37 7
|
14天前
|
存储 监控 算法
基于 C# 的局域网计算机监控系统文件变更实时监测算法设计与实现研究
本文介绍了一种基于C#语言的局域网文件变更监控算法,通过事件驱动与批处理机制结合,实现高效、低负载的文件系统实时监控。核心内容涵盖监控机制选择(如事件触发机制)、数据结构设计(如监控文件列表、事件队列)及批处理优化策略。文章详细解析了C#实现的核心代码,并提出性能优化与可靠性保障措施,包括批量处理、事件过滤和异步处理等技术。最后,探讨了该算法在企业数据安全监控、文件同步备份等场景的应用潜力,以及未来向智能化扩展的方向,如文件内容分析、智能告警机制和分布式监控架构。
38 3
|
1月前
|
存储 监控 算法
基于 C# 时间轮算法的控制局域网上网时间与实践应用
在数字化办公与教育环境中,局域网作为内部网络通信的核心基础设施,其精细化管理水平直接影响网络资源的合理配置与使用效能。对局域网用户上网时间的有效管控,已成为企业、教育机构等组织的重要管理需求。这一需求不仅旨在提升员工工作效率、规范学生网络使用行为,更是优化网络带宽资源分配的关键举措。时间轮算法作为一种经典的定时任务管理机制,在局域网用户上网时间管控场景中展现出显著的技术优势。本文将系统阐述时间轮算法的核心原理,并基于 C# 编程语言提供具体实现方案,以期深入剖析该算法在局域网管理中的应用逻辑与实践价值。
33 5
|
16天前
|
Web App开发 前端开发 JavaScript
鸿蒙5开发宝藏案例分享---Web适配一多开发实践
这是一份实用的鸿蒙Web多设备适配开发指南,针对开发者在不同屏幕尺寸下的布局难题提供了解决方案。文章通过三大法宝(相对单位、媒体查询和窗口监听)详细介绍如何实现智能适配,并提供了多个实战案例,如宫格布局、对话框变形和自适应轮播图等。此外,还分享了调试技巧及工具推荐,帮助开发者快速上手并优化性能。最后鼓励读者实践探索,并提示更多官方资源等待发现。
|
4月前
|
中间件 关系型数据库 数据库
docker快速部署OS web中间件 数据库 编程应用
通过Docker,可以轻松地部署操作系统、Web中间件、数据库和编程应用。本文详细介绍了使用Docker部署这些组件的基本步骤和命令,展示了如何通过Docker Compose编排多容器应用。希望本文能帮助开发者更高效地使用Docker进行应用部署和管理。
113 19
|
3月前
|
存储 消息中间件 缓存
支持百万人超大群聊的Web端IM架构设计与实践
本文将回顾实现一个支持百万人超大群聊的Web端IM架构时遇到的技术挑战和解决思路,内容包括:通信方案选型、消息存储、消息有序性、消息可靠性、未读数统计。希望能带给你启发。
82 0
支持百万人超大群聊的Web端IM架构设计与实践
|
5月前
|
Web App开发 编解码 vr&ar
使用Web浏览器访问UE应用的最佳实践
在3D/XR应用开发中,尤其是基于UE(虚幻引擎)开发的高精度场景,传统终端因硬件局限难以流畅运行高帧率、复杂效果的三维应用。实时云渲染技术,将渲染任务转移至云端服务器,降低终端硬件要求,确保用户获得流畅体验。具备弹性扩展、优化传输协议、跨平台支持和安全性等优势,适用于多种终端和场景,特别集成像素流送技术,帮助UE开发者实现低代码上云操作,简化部署流程,保留UE引擎的强大开发能力,确保画面精美且终端轻量化。
276 17
使用Web浏览器访问UE应用的最佳实践
|
5月前
|
Kubernetes Java 持续交付
小团队 CI/CD 实践:无需运维,Java Web应用的自动化部署
本文介绍如何使用GitHub Actions和阿里云Kubernetes(ACK)实现Java Web应用的自动化部署。通过CI/CD流程,开发人员无需手动处理复杂的运维任务,从而提高效率并减少错误。文中详细讲解了Docker与Kubernetes的概念,并演示了从创建Kubernetes集群、配置容器镜像服务到设置GitHub仓库Secrets及编写GitHub Actions工作流的具体步骤。最终实现了代码提交后自动构建、推送镜像并部署到Kubernetes集群的功能。整个过程不仅简化了部署流程,还确保了应用在不同环境中的稳定运行。
220 9

热门文章

最新文章