问题描述
我正在尝试从视频中切出特定部分,然后使用nodejs和ffmpeg将所有这些部分合并到一个视频文件中.
I am trying to cut specific parts out from a video and then merge all those parts into a single video file using nodejs and ffmpeg.
这是我的代码,目前我只能将视频中的一部分从 .setStartTime
剪切为 .setDuration
,并且正在保存该部分.
Here's my code and currently I can cut only one part out of the video from .setStartTime
to .setDuration
and that part is being saved.
var ffmpeg = require('fluent-ffmpeg');
var command = ffmpeg()
.input('./videos/placeholder-video.mp4')
.setStartTime('00:00:03')
.setDuration('02')
.output('./videos/test.mp4')
.on('start', function(commandLine) {
console.log('Started: ' + commandLine);
})
.on('end', function(err) {
if(!err)
{
console.log('conversion Done');
}
})
.on('error', function(err){
console.log('error: ', +err);
}).run();
如何从视频中切出多个部分并将它们合并到一个视频文件中.我知道 .mergeToFile
方法,但是在从视频中剪切不同部分后如何使用它.
How do I cut out multiple parts out from the video and merge them in a single video file. I know about .mergeToFile
method but how do I use it after cutting different parts from my video.
我尝试使用 .setStartTime
和 .setDuration
两次,如下所示,但第一个被忽略了.
I tried using .setStartTime
and .setDuration
twice like below but the first one's are being ignored.
.input('./videos/placeholder-video.mp4')
.setStartTime('00:00:03')
.setDuration('02')
.setStartTime('00:00:15')
.setDuration('05')
.output('./videos/test.mp4')
推荐答案
请阅读更新
尚无法测试,但请尝试以下操作:设置输入视频两次,如下所示:
Could not test it yet, but try following:Set the input video two times, like this:
.input('./videos/placeholder-video.mp4')
.setStartTime('00:00:03')
.setDuration('02')
.input('./videos/placeholder-video.mp4')
.setStartTime('00:00:15')
.setDuration('05')
然后,您可以使用 .mergeToFile
合并多个输入.
Then you can merge the multiple inputs with .mergeToFile
.
更新
由于 .setDuration
的限制,我的答案无法正常工作.它是一个输出选项,因此它定义了对输出文件进行代码转换的时间: https://github.com/fluent-ffmpeg/node-fluent-ffmpeg 它不用于定义输入的长度/持续时间.
My answer cannot work due to restriction of .setDuration
. It is an output option, so it defines how long transcoding to the output file is done: https://github.com/fluent-ffmpeg/node-fluent-ffmpegIt is not used to define the length/duration of the input.
另一个选项是 .loop
,但显然不支持此目的:
Another option would be .loop
, but apparently it is not supported for this purpose: https://video.stackexchange.com/questions/12905/repeat-loop-input-video-with-ffmpeg/12906#12906
如果您确实要使用nodejs,则需要多个命令
If you really want to use nodejs, you need multiple commands
- 将输入视频剪切为临时文件,每次剪切为一个
- 将临时文件合并到一个输出文件
类似这样的东西:
var command1 = ffmpeg()
.input('./small.mp4')
.seekInput('00:00:02')
.withDuration(1)
.output('./temp1.mp4')
.run();
var command2 = ffmpeg()
.input('./small.mp4')
.seekInput('00:00:01')
.withDuration(3)
.output('./temp2.mp4')
.run();
var command2 = ffmpeg()
.input('./temp1.mp4')
.input('./temp2.mp4')
.mergeToFile('./merge.mp4', './temp');
大问题:如果不存在临时文件,则您的nodejs脚本将无法启动.因此,使用bash或批处理脚本会容易得多.
Big problem: your nodejs script will not start if temporary files are not present.So it would be much more easier to use a bash or batch script.
这篇关于剪切视频中的多个部分并将它们合并在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!