node.js执行cmd

node.js执行cmd

最近在写一个关于语音显示的问题,需要用到node.js执行cmd,记录一下自己的写法
在js文件中

const cp = require('child_process')
//执行cmd
function runCmdTest(inputPath,outputPath) {
let exePath='E:\\2022\\ddd\\src\\http\\voiceConversion\\ypzh.exe' //需要运行的程序
    cp.exec(`${exePath} ${inputPath} ${outputPath}`, (err, stdout, stderr) => {
  })
}
//node.js写入文件
function writeFile(data, content) {
    fs.writeFile(data, content, function (err) {
        if (err) {
            return console.log("文件写入失败!" + err.message)
        }
        console.log("文件写入成功!")
    })
}
//获取随机数
function wanderNumber(n) {
    var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
];
    global.res = "";
    for (var i = 0; i < n; i++) {
        var id = Math.ceil(Math.random() * 61);
        res += chars[id];
    }
    return global.res;
}

引用

   //文件命名,时间戳加随机数
  let audioTempName = "voice" + new Date().getTime() + wanderNumber(4);
  let linshiTxt = osCunchu().tmpdir() + "\\" + audioTempName + ".txt"; 
   //建立txt格式文件,osCunchu().tmpdir()获取操作系统临时文件路径(const os = require("os");os.tmpdir())
   writeFile(linshiTxt, msg.voip_path);//从后端获取的数据写入到建立的txt文件中
   //运行cmd,C:\\Users\\Home\\Desktop\\test这个是用来存放转化后的文件存储地址(mp3),我放在了桌面
   runCmdTest(linshiTxt, "C:\\Users\\Home\\Desktop\\test");

最后就会得到一个mp3格式音频了
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值