我用下面的代码让它工作。
这并不完美,有时会抛出一个EPIPE
错误,因为 ffmpeg 将标准输入关闭得很快,但它的工作原理和为您的用例奠定了坚实的基础:
const { exec } = require("child_process");
const fs = require("fs");
// this (data) is your input file, that you pipe into ffmepg
const data = fs.readFileSync("./input.avi");
console.log("input data read, size:", data.length);
const ffmpeg = exec(`ffmpeg -ss 00:10:00 -y -f avi -i pipe: -vframes 1 output.jpg`);
ffmpeg.on("error", (err) => {
console.log("FFMEPG-ERROR", err)
});
ffmpeg.on("close", (code) => {
console.log("ffmepg exited", code);
});
ffmpeg.stdin.on("error", (err) => {
if (err.code === "EPIPE") {
// ignore EPIPE error
// throws sometimes a EPIPE error, ignore as long ffmpeg exit with code 0
} else {
console.log("ffmepg stdin error", err);
}
});
ffmpeg.stdin.write(data);
基本上生成 ffmpeg,从标准输入读取,-i pipe:
or -i -
,并开始将数据泵入标准输入。
要测试 ffmpeg 命令,您可以运行以下命令。
这会在 10 秒处创建输入数据的缩略图。
请记住调整文件类型(-f
选项)来匹配您的输入数据,因为 ffmpeg 在从标准输入读取时无法自动检测到它。
这-y
标志是覆盖已经存在的输出文件。
cat input.avi | ffmpeg -ss 00:10:00 -y -f avi -i pipe: -vframes 1 output.jpg
有些人提到使用-ss
作为寻求速度更快的第一个选项,但对我的测试没有影响。您可以在这里阅读更多内容:https://stackoverflow.com/a/27573049/5781499
如果您需要任何进一步的帮助,请告诉我。