【发布时间】: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