【问题标题】:Unable to serially execute ```execFile()``` with ```ffmpeg-static-electron``` binary Electron无法使用 ```ffmpeg-static-electron ``` binary Electron 串行执行 ```execFile()```
【发布时间】:2022-01-12 23:52:08
【问题描述】:

我正在尝试将多个 .mp4 视频拆分为 .jpg 图像。

设置使用electron-react-app。我使用了preload 脚本来启用对 React 组件的有限功能的访问。

问题:无法对视频文件名数组连续执行对execFile() 的调用。第一个视频文件正在成功转换,但尽管使用了promises,但仍无法转换剩余的视频。

预加载脚本中的函数(在 React 组件中访问):

const ffmpeg = require("ffmpeg-static-electron")
const { execFile } = require("child_process");
...
{split: (tempReviewFolderPath, filename) => {
    execFile(`${ffmpeg.path}`, [
      "-i",
      `${path.join(tempReviewFolderPath, filename)}`,
      "-vf",
      "fps=3",
      `${path.join(tempReviewFolderPath, "image-%d.jpg")}`,
    ]),
      (error, stdout, stderr) => {
        if (error) {
          console.log(error);
        } else {
          console.log(stdout, stderr);
        }
      };
  }
}

React组件:

tempReviewFolderPath 是包含视频的文件夹的filepath

filenamesArray 是一个包含视频文件名字符串的数组。

"main_process:video_to_split"electron ipc 中使用的通道,它以(通道,回调)为参数。

useEffect(() => {
("main_process:video_to_split", 

 ({ tempReviewFolderPath, filenamesArray }) => {
        try {
          Promise.all(
            filenamesArray.map((filename) => {
              return new Promise((resolve) => {
                preLoad.split(tempReviewFolderPath, filename);
                resolve(filename);
              });
            })
          ).catch(console.error);
        } catch (error) {
          console.log(error);
        }
      })

}, []}

这只会分割第一个视频。

我也试过了:-

预加载脚本

const execFile = require("util").promisify(require("child_process").execFile);

{split: async (tempReviewFolderPath, filename) => {
    await execFile(`${ffmpeg.path}`, [
      "-i",
      `${path.join(tempReviewFolderPath, filename)}`,
      "-vf",
      "fps=3",
      `${path.join(tempReviewFolderPath, "image-%d.jpg")}`,
    ]),
      (error, stdout, stderr) => {
        if (error) {
          console.log(error);
        } else {
          console.log(stdout, stderr);
        }
      };
  }
}

React组件:

useEffect(() => {
("main_process:video_to_split", 

 ( tempReviewFolderPath, filenamesArray }) => {
        try {
              preLoad.split(tempReviewFolderPath, filename);
             } catch (error) {
          console.log(error);
        }
      })

}, []}

最后(出于绝望):

useEffect(() => {
  ("main_process:video_to_split", 

({ tempReviewFolderPath, filenamesArray }) => {
        let filename1 = filenamesArray[0];
        let filename2 = filenamesArray[1];

        try {
          preLoad.split(tempReviewFolderPath, filename1);
          preLoad.split(tempReviewFolderPath, filename2);
        } catch (error) {
          console.log(error);
        }
      }

)

同样的问题 - 只有第一个视频被分割。

我做错了什么?

非常感谢。

【问题讨论】:

    标签: node.js child-process execfile electron-react-boilerplate


    【解决方案1】:

    找到解决方案:

    与“image-%d.jpg”相关的问题,它只是在每次迭代时被地图覆盖。

    解决方法:只是增加了索引,改变了功能。

    filenamesArray.map((filename, index) => {
              return new Promise((res, rej) => {
                api_link.split(tempReviewFolderPath, filename, index);
                res(filename);
              });
            });
    

    【讨论】:

      猜你喜欢
      • 2021-11-26
      • 2021-11-06
      • 2023-04-02
      • 2022-01-16
      • 2019-02-01
      • 2018-07-03
      • 2022-01-23
      • 2019-06-12
      • 2020-12-22
      相关资源
      最近更新 更多