tools_batch_files 1.0.34 → 1.0.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tools_batch_files",
3
- "version": "1.0.34",
3
+ "version": "1.0.35",
4
4
  "description": "批处理视频工具",
5
5
  "keywords": [
6
6
  "utils",
@@ -29,6 +29,12 @@ const queueCount = 10;
29
29
  //起始任务下标
30
30
  let taskIndex = 0;
31
31
 
32
+ //已完成数量
33
+ let completeCount = 0;
34
+
35
+ //错误文件重试次数
36
+ let retryError = 0;
37
+
32
38
  //执行目录
33
39
  const exeDir = __dirname;
34
40
  //当前工作目录
@@ -48,7 +54,13 @@ const ZIP_FILES_DIR = "zip";
48
54
  //拷贝 - 存放上传失败的文件
49
55
  const Error_Files_Dir = "failed_audio";
50
56
  //水印
51
- const watermarkAudioPath = path.join(__dirname, "..", "vocal_print", "mz.mp3");
57
+ const watermarkAudioPath = path.join(
58
+ __dirname,
59
+ "..",
60
+ "..",
61
+ "vocal_print",
62
+ "mz.mp3"
63
+ );
52
64
  const longWatermarkAudioPath = path.join(
53
65
  __dirname,
54
66
  "..",
@@ -77,7 +89,7 @@ const watermarkAudio = (indexFilePath, originFilePath, fileName, duration) => {
77
89
  `[1:a]atrim=0:duration=${duration},volume=1.5[a1]`, // 对声纹音频流进行截取操作,并设置音量,重命名为a1
78
90
  `[a0][a1]amix=inputs=2[a]`, // 合并两个音频流为一个输出流,重命名为a
79
91
  `[a]volume=3.0`, // 设置输出音频的音量
80
- ].join(";");
92
+ ];
81
93
 
82
94
  if (duration <= 5) {
83
95
  //直接合并,放在末尾,下次补上
@@ -108,7 +120,7 @@ const watermarkAudio = (indexFilePath, originFilePath, fileName, duration) => {
108
120
  .input(originFilePath)
109
121
  .input(vocalPath) // 声纹音频文件
110
122
 
111
- .complexFilter(complexFilter)
123
+ .complexFilter(complexFilter.join(";"))
112
124
  .output(outputFile)
113
125
  .on("error", (err) => {
114
126
  logger("添加声纹出错: " + err);
@@ -272,8 +284,8 @@ const postData = (dataParams, indexFilePath, index) => {
272
284
  logger(`第${index}条等待接口返回结果……`);
273
285
 
274
286
  // return axios.post("http://192.168.102.61:9999/upload/sound", formData, {
275
- // return axios.post("http://127.0.0.1:9999/upload/sound", formData, {
276
- return axios.post("http://192.168.101.149:9999/upload/sound", formData, {
287
+ // return axios.post("http://192.168.101.149:9999/upload/sound", formData, {
288
+ return axios.post("http://127.0.0.1:9999/upload/sound", formData, {
277
289
  headers: {
278
290
  "Content-Type": "multipart/form-data",
279
291
  },
@@ -289,7 +301,8 @@ async function postDataWithRetry(
289
301
  indexFilePath,
290
302
  index,
291
303
  fileName,
292
- hash
304
+ hash,
305
+ type
293
306
  ) {
294
307
  let retryCount = 0; // 当前重试次数
295
308
 
@@ -300,7 +313,7 @@ async function postDataWithRetry(
300
313
  logger("请求成功!");
301
314
  logger(resData.data.code);
302
315
  // 文件名和索引值
303
- successLogger(hash, index, fileName);
316
+ successLogger(hash, index, fileName, type);
304
317
  removeDirectory(indexFilePath);
305
318
  return;
306
319
  } else if (resData.data.code === 300) {
@@ -327,7 +340,7 @@ async function postDataWithRetry(
327
340
  /**
328
341
  * 任务
329
342
  */
330
- const task = async (row, index, hash) => {
343
+ const task = async (row, index, hash, type) => {
331
344
  //index文件夹 output/0
332
345
  const indexFilePath = path.join(workDir, "output", index + "");
333
346
  let fileName = row.fileName;
@@ -393,7 +406,14 @@ const task = async (row, index, hash) => {
393
406
  await archiveZip(fileName, indexFilePath, originFilePath);
394
407
 
395
408
  // 重试机制
396
- await postDataWithRetry(dataParams, indexFilePath, index, fileName, hash);
409
+ await postDataWithRetry(
410
+ dataParams,
411
+ indexFilePath,
412
+ index,
413
+ fileName,
414
+ hash,
415
+ type
416
+ );
397
417
 
398
418
  logger(
399
419
  `----------------------------------------第${index}条结束---------------------------------end`
@@ -414,35 +434,41 @@ const task = async (row, index, hash) => {
414
434
  }
415
435
  };
416
436
 
417
- const run = (index, data, hash) => {
437
+ const run = (index, data, hash, type) => {
418
438
  try {
419
439
  logger(
420
440
  `run-------------------------------------第${index}条开始------------------------------------`
421
441
  );
422
442
  const row = data[index];
423
443
 
424
- if (!row) {
444
+ if (completeCount >= data.length) {
425
445
  taskIndex = 0;
426
446
  logger(
427
447
  index +
428
448
  "=========================当前任务,最后一条已结束!========================="
429
449
  );
430
450
 
451
+ successLogger(hash, index, "done");
452
+
431
453
  // 是否存在错误列表? 存在的话,继续遍历
432
- const errorExcelPath = path.join(workDir, `error${hash}.xlsx`);
433
- if (isExist(errorExcelPath)) {
434
- const jsonData2 = readExcel(errorExcelPath);
435
- queue(taskIndex, jsonData2, hash);
436
- } else {
437
- logger("跑干了!");
454
+ const status = runErrorList(taskIndex, hash);
455
+ if (!status) {
456
+ logger(
457
+ "》》》》》》》》》》音频批量任务任务结束《《《《《《《《《《" + hash
458
+ );
438
459
  }
439
460
 
440
461
  return;
441
462
  }
442
463
 
443
- task(row, index, hash)
464
+ if (!row) {
465
+ return;
466
+ }
467
+
468
+ task(row, index, hash, type)
444
469
  .then(() => {
445
470
  taskIndex++;
471
+ completeCount++;
446
472
  run(taskIndex, data, hash);
447
473
  })
448
474
  .catch((err) => {
@@ -450,15 +476,52 @@ const run = (index, data, hash) => {
450
476
  });
451
477
  } catch (error) {
452
478
  logger("捕获错误!" + error);
479
+ completeCount++;
453
480
  taskIndex++;
454
481
  run(taskIndex, data, hash);
455
482
  }
456
483
  };
457
484
 
458
- const queue = (taskIndex, jsonData, hash) => {
459
- for (let i = 0; i < queueCount; i++) {
460
- run(i + taskIndex, jsonData, hash);
485
+ const queue = (index, jsonData, hash, type) => {
486
+ const rest = jsonData.length - index;
487
+ const count = queueCount > rest ? rest : queueCount;
488
+ const queueList = [];
489
+ completeCount = index;
490
+
491
+ for (let i = 0; i < count; i++) {
492
+ queueList.push(run(i + index, jsonData, hash, type));
461
493
  }
494
+
495
+ taskIndex += count - 1;
496
+ return Promise.all(queueList);
497
+ };
498
+
499
+ //yunxing cuowu wenjian
500
+ const runErrorList = (index, hash, parentHash) => {
501
+ const fileHash = parentHash || hash;
502
+ const errorExcelPath = path.join(workDir, `error${fileHash}.xlsx`);
503
+
504
+ if (retryError > 5) {
505
+ console.log("出现无法解析的错误");
506
+ return false;
507
+ }
508
+
509
+ taskIndex = index;
510
+ let taskHash = hash;
511
+ //新的运行任务-重新赋予新的任务id
512
+ if (index === 0) {
513
+ taskHash = generateUniqueHash().slice(0, 8);
514
+ }
515
+
516
+ if (isExist(errorExcelPath)) {
517
+ retryError++;
518
+
519
+ logger("开始运行错误列表");
520
+ jsonData = readExcel(errorExcelPath); // 数据来源于error excel
521
+ queue(index, jsonData, taskHash, fileHash);
522
+ return true;
523
+ }
524
+ return false;
462
525
  };
463
526
 
464
527
  const main = () => {
@@ -473,18 +536,31 @@ const main = () => {
473
536
 
474
537
  const successPath = path.join(workDir, "success.txt");
475
538
  if (isExist(successPath)) {
476
- const { loghash, lastItemIndex } = readTxt(successPath);
477
- const errorExcelPath = path.join(workDir, `error${loghash}.xlsx`);
478
- logger("成功日志存在,异常中断或者跑完了。");
539
+ const { loghash, lastItemIndex, fileName, parentHash } =
540
+ readTxt(successPath);
479
541
 
480
542
  hash = loghash;
481
- taskIndex = lastItemIndex + 1; // lastItemIndex为最后一项,所有要+1
482
-
483
- if (isExist(errorExcelPath)) {
484
- jsonData = readExcel(errorExcelPath); // 数据来源于error excel
485
- queue(taskIndex, jsonData, hash);
543
+ taskIndex = parseInt(lastItemIndex) + 1; // lastItemIndex为最后一项,所有要+1
544
+
545
+ //当前任务已经结束
546
+ if (fileName === "done") {
547
+ //找寻是否存在未上传的错误文件列表
548
+ const status = runErrorList(0, hash);
549
+ if (!status) {
550
+ logger(
551
+ "》》》》》》》》》》音频批量任务任务结束《《《《《《《《《《" + hash
552
+ );
553
+ }
486
554
  } else {
487
- logger("跑干了!");
555
+ //当前任务异常中断
556
+
557
+ //中断的任务类型为错误任务
558
+ if (parentHash) {
559
+ runErrorList(taskIndex, hash, parentHash);
560
+ } else {
561
+ jsonData = readExcel(excelDir);
562
+ queue(taskIndex, jsonData, hash);
563
+ }
488
564
  }
489
565
  } else {
490
566
  hash = generateUniqueHash().slice(0, 8);
package/utils/logger.js CHANGED
@@ -17,11 +17,15 @@ const logger = (log) => {
17
17
  * 成功日志
18
18
  * @param {*} 日志内容 hash index fileName
19
19
  */
20
- const successLogger = (hash, index, fileName) => {
20
+ const successLogger = (hash, index, fileName, type = "") => {
21
21
  console.log(fileName);
22
- fs.writeFileSync("success.txt", hash + " " + index + " " + fileName + "\n", {
23
- flag: "a",
24
- });
22
+ fs.writeFileSync(
23
+ "success.txt",
24
+ hash + " " + index + " " + fileName + " " + type + "\n",
25
+ {
26
+ flag: "a",
27
+ }
28
+ );
25
29
  };
26
30
 
27
31
  //错误日志- xlsx
@@ -86,9 +86,8 @@ function readTxt(path) {
86
86
 
87
87
  const lastItem = filteredArray[filteredArray.length - 1];
88
88
 
89
- const loghash = lastItem.split(" ")[0];
90
- const lastItemIndex = parseInt(lastItem.split(" ")[1]);
91
- return { loghash, lastItemIndex };
89
+ const [loghash, lastItemIndex, fileName, parentHash] = lastItem.split(" ");
90
+ return { loghash, lastItemIndex, fileName, parentHash };
92
91
  } catch (error) {
93
92
  console.error("readErrorTxt函数出错:", error);
94
93
  return {};