看看效果叭
解压的文件
上传的文件格式
测试1|||测试1的文字
测试2|||测试2的文字
测试3|||测试3的文字
测试4|||测试4的文字
测试5|||测试5的文字
实现的逻辑如下
- 上传文件
- 解析txt
- 发送内容至百度语音合成
- 生成文件夹放置本次合成的mp3文件,并压缩成zip
- 发送zip的地址给前端
使用了 element-ui 的 el-upload 组件
<el-upload v-loading="loading" class="upload-demo" drag ref="upload" action="#" accept=".txt" :before-upload="onBeforeUploadImage" :http-request="UploadImage" :on-change="fileChange" :file-list="fileList" > <i class="el-icon-upload"></i> <div class="el-upload__text"> 将文件拖到此处,或 <em>点击上传</em> </div> <div class="el-upload__tip" slot="tip">只能上传txt文件,且不超过1M</div> </el-upload>
在上传之前判断上传的文件是否符合要求
onBeforeUploadImage(file) { const isTxt = file.type === "text/plain"; const isLt1M = file.size / 1024 / 1024 < 1; if (!isTxt) { this.$message.error("上传文件只能是txt格式!"); } if (!isLt1M) { this.$message.error("上传文件大小不能超过 1MB!"); } return isTxt && isLt1M; }
一次只上传一个文件,在文件列表更新时先清除之前的文件
fileChange(file) { let obj = this.onBeforeUploadImage(file.raw); if (obj) { this.$refs.upload.clearFiles(); this.fileList = [{ name: file.name, url: file.url }]; } }
上传的主要函数
UploadImage(param) { this.loading = true; const formData = new FormData(); formData.append("file", param.file); this.$axios({ url: process.env.VUE_APP_BASE_API + "api/txtToMp3", method: "post", data: formData }) .then(response => { if (response.data.code == 0) { this.loading = false; this.dialogVisible = true; this.url = response.data.data.url; } }) .catch(error => { console.log(error); }); }
node代码
用到的依赖项
const formidable = require("formidable"); //获取上传的txt,并保存 const path = require("path"); const AipSpeech = require("baidu-aip-sdk").speech; //百度语音合成sdk const fs = require("fs"); const compressing = require("compressing"); //压缩文件夹用
接口代码
router.post("/txtToMp3", async function (req, res, next) { let form = new formidable.IncomingForm(); form.encoding = "utf-8"; //编码 form.uploadDir = path.join(__dirname + "/../txt"); //保存上传文件地址 form.keepExtensions = true; //保留后缀 form.parse(req, function (err, fields, files) { let filename; filename = files.file.name; let nameArray = filename.split("."); //分割 let type = nameArray[nameArray.length - 1]; let name = ""; for (let i = 0; i < nameArray.length - 1; i++) { name = name + nameArray[i]; } let date = new Date(); let time = "_" + date.getTime(); let avatarName = name + time + "." + type; let newPath = form.uploadDir + "/" + avatarName; fs.renameSync(files.file.path, newPath); //移动文件 fs.readFile(newPath, "utf-8", function (err, data) { if (err) { console.log(err); new Result(null, "读取失败").fail(res); } else { let client = new AipSpeech( 0, "百度语音合成key", "百度语音合成secret" ); let resultData = data.split("\n"); let number = resultData.length; let formTime = new Date().getTime(); let mp3FileDir = path.join(__dirname + "/../mp3_" + formTime); fs.mkdirSync(mp3FileDir); for (let i in resultData) { setTimeout(function(){ if (resultData[i].indexOf("|||") != -1) { let text = resultData[i].split("|||")[1]; // 语音合成,保存到本地文件 client.text2audio(text, { spd: 4, per: 4 }).then( function (result) { if (result.data) { let time = resultData[i].split("|||")[0] + "_voice"; let avatarName_mp3 = mp3FileDir + "/" + time + ".mp3"; fs.writeFileSync(avatarName_mp3, result.data); number--; if (number == 0) { let zipFileName = "zip/mp3_" + formTime + ".zip"; compressing.zip .compressDir(mp3FileDir, zipFileName) .then(() => { let item = { url: zipFileName, }; new Result(item, "压缩成功").success(res); }) .catch((err) => { new Result(null, "压缩失败").fail(res); }); } } else { // 合成服务发生错误 new Result(null, "合成失败").fail(res); } }, function (err) { console.log(err); } ); } else { new Result(null, "文件格式错误").fail(res); } },i * 20) } } }); }); });
PS:
在node部分,在判断需要合成的文件是否全部完成时,我是通过number的值等于0判断完成,不知道大佬们有啥好方法不?
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年09月21日
2024年09月21日
- 黑神话悟空上品鼠禁卫精魄获取方法一览|上品鼠禁卫精魄收集攻略
- 优美的旋律至真的情怀《品茶轻音乐·极品典藏Vol.1CD1》[WAV+CUE]
- 曾嵘《峥嵘岁月DSD(试音碟)》WAV+CUE
- 小提琴天碟AlfredoCampoli坎波利-贝多芬D大调小提琴协奏曲与普罗米修斯作品选FLAC
- 博主称本周游戏业内动荡无比:高价PS5 Pro、Xbox裁员等
- 米哈游也搞AI?米哈游AI大模型Glossa完成备案
- G胖变G瘦后:玩家担心其健康状况:如果他出事 游戏业将发生巨变
- 群星.1995-SUMMER.PASSION.REMIX【宝丽金】【WAV+CUE】
- 王栎鑫.2008-天生一对(EP)【种子音乐】【WAV+CUE】
- 曾淑勤.1993-金选集【点将】【WAV+CUE】
- 《命运2》新枪涉嫌抄袭Bungie响应正在调查
- 《刺客信条:影》引发争议日本预购表现低迷
- 黑神话悟空上品狸侍长精魄获取方法一览|上品狸侍长精魄收集攻略
- 大自然环保音乐系列《风花雪月》4CD/DTS[WAV分轨]
- 模拟之声慢刻CD甄妮《86精选[日本母带]》[低速原抓WAV+CUE]