0

我正在尝试将许多.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);
        }
      }

)

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

我究竟做错了什么 ?

非常感谢。

4

1 回答 1

0

找到解决方案:

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

解决方案:只是添加了索引并更改了功能。

filenamesArray.map((filename, index) => {
          return new Promise((res, rej) => {
            api_link.split(tempReviewFolderPath, filename, index);
            res(filename);
          });
        });
于 2022-01-13T05:57:43.200 回答