首页 > 解决方案 > 流畅的ffmpeg节点js命令

问题描述

const ffmpeg = require('fluent-ffmpeg');
const videoFile = './f1.mp4';

ffmpeg.ffprobe(videoFile, (err,metaData) => {
    const {duration} = metaData.format;


    const startingTime = parseInt(duration - 60);
    const clipDuration = 20;

    ffmpeg()
     .input(videoFile)
     .inputOptions([`-ss ${startingTime}`])
     .outputOptions([`-t ${clipDuration}`])
     .output('./result.mp4')
     .on('end', ()=> console.log('Done!'))
     .on('error',(err)=>console.error(err))
     .run();
});

所以这是我的节点 js 代码,我在其中剪切我选择的视频剪辑并给我一个输出。我通过节点索引运行它。js(代码在 index.js 文件中)我想创建一个可以在下面的命令行上运行的脚本

节点 index.js start_time end_time 输入/文件/path.mp4 输出/目录/

我的意思是它将是动态的,因为来自任何目录的任何输入文件和来自任何目录的任何输出文件都像一个函数,它将接受用户输入并相应地执行。无需手动设置。有没有办法做到这一点??我尝试了很多,但都是在命令行上手动或手动设置节点。我正在尝试创建一个动态 js 文件,它将为任何输入运行

标签: javascriptnode.jsffmpegfluent-ffmpeg

解决方案


你可能想要的是process.argv. 这是参数向量:参数列表,其中前两个是节点进程和正在运行的文件。例子:

const args = process.argv.slice(2);

if (!args.length !== 4) {
  console.error('Incorrect number of arguments');
  process.exit(1);
}

const [ startTime, endTime, inputFile, outputDirectory ] = args;

推荐阅读