目的:音频分割成等时长的小音频,批量下载,故需要将其打包压缩
代码:
import JSZip from "jszip";
import FileSaver from "file-saver";
async downloadAllAudio() {
const zip = new JSZip();
const cache = {};
const promises = [];
this.downloadData.forEach((item) => {
const promise = this.getFile(item.url).then((data) => {
const file_name = item.fileName;
zip.file(file_name + ".wav", data, { binary: true });
cache[file_name] = data;
});
promises.unshift(promise);
});
Promise.all(promises).then(() => {
zip.generateAsync({ type: "blob" }).then((content) => {
saveAs(content, "分割后的音频.zip"); // 利用file-saver保存文件 自定义文件名
});
});
},
getFile(url) {
return new Promise((resolve, reject) => {
fetch(url)
.then((data) => {
// console.log(data);
return data;
})
.then((res) => {
resolve(res.blob());
});
});
},
借鉴网址
[https://www.cnblogs.com/wuqilang/p/13567136.html]