gpt4 book ai didi

node.js - 使用 ffmpeg 和 nodejs (fluent-ffmpeg) 在不重新编码的情况下剪切视频

转载 作者:行者123 更新时间:2023-12-04 23:12:09 34 4
gpt4 key购买 nike

我有一个快速的问题,
我正在尝试做一个云视频编辑器,我希望能够使用 nodejs 剪切视频。
我正在使用 fluent-ffmpeg .这是我的代码:

const cutVideo = async (sourcePath, outputPath, startTime, duration) => {
console.log('start cut video');

await new Promise((resolve, reject) => {
ffmpeg(sourcePath)
.setFfmpegPath(pathToFfmpeg)
.setFfprobePath(ffprobe.path)
.output(outputPath)
.setStartTime(startTime)
.setDuration(duration)
.on('end', function (err) {
if (!err) {
console.log('conversion Done');
resolve();
}
})
.on('error', function (err) {
console.log('error: ', err);
reject(err);
})
.run();
});
};
它工作正常,但不是最佳的,一旦我尝试编辑长视频(从视频中获得 10 分钟而不是 1 分钟),它就会超长。
我的理解是 ffmpeg 重新编码所有内容,这就是为什么编辑时间越长过程越长的原因。
有没有办法在不重新编码所有内容的情况下使用 node-fluent-ffmpeg 进行删减?
感谢社区!

最佳答案

在挖掘 Node 流利的文档和来自@szatmary 的信息之后,可以定义音频和视频编解码器。要使用相同的编解码器并避免重新编码所有视频,只需传递 copy作为 withVideoCodec 中的参数和 withAudioCodec :

const ffmpeg = require('fluent-ffmpeg');
const pathToFfmpeg = require('ffmpeg-static');
const ffprobe = require('ffprobe-static');

const cutVideo = async (sourcePath, outputPath, startTime, duration) => {
console.log('start cut video');

await new Promise((resolve, reject) => {
ffmpeg(sourcePath)
.setFfmpegPath(pathToFfmpeg)
.setFfprobePath(ffprobe.path)
.output(outputPath)
.setStartTime(startTime)
.setDuration(duration)
.withVideoCodec('copy')
.withAudioCodec('copy')
.on('end', function (err) {
if (!err) {
console.log('conversion Done');
resolve();
}
})
.on('error', function (err) {
console.log('error: ', err);
reject(err);
})
.run();
});
};

关于node.js - 使用 ffmpeg 和 nodejs (fluent-ffmpeg) 在不重新编码的情况下剪切视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63997589/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com