如何在Node.js中使用ffmpeg合并.mkv中的.m4a和.m4v

时间:2019-06-29 17:59:24

标签: node.js ffmpeg

我正在使用下面的脚本下载vimeo的流。

问题是脚本下载了各种.m4s片段,并为音频创建了.m4a文件,为视频创建了.m4v文件。因此,我必须通过cmd手动使用ffmpeg命令将它们合并为一个.mkv文件

我想在脚本中添加命令行,以便自动完成。我尝试了几种不同的命令以及它们之间的组合,例如.exec,.execFile,.spawn,所有命令均未成功

相同的例子:

1)使用execFile


const child = execFile('ffmpeg', ['-i', 'input.m4v' , '-i' , 'input.m4a' , '-c' , 'copy' , 'output.mkv' ], (error, stdout, stderr) => {
    if (error) {
        console.error('stderr: =============================', stderr);
        throw error;
    }
    console.log('stdout: ==========================', stdout);
});

console.log('here');

2)使用exec


const child = exec('ffmpeg', ['-i', 'input.m4v' , '-i' , 'input.m4a' , '-c' , 'copy' , 'output.mkv', (error, stdout, stderr) => {
    if (error) {
        console.error('stderr: =============================', stderr);
        throw error;
    }
    console.log('stdout: ==========================', stdout);
});

console.log('here');

3)使用生成物


var cmd = 'D:\vimeo\ffmpeg';

var args = [
    '-i', 'D:\vimeo\input.m4a',
    '-i', 'D:\vimeo\input.m4v', 
    '-c', 'copy', 'D:\vimeo\output.mkv'
];

var proc = spawn(cmd, args);

proc.stdout.on('data', function(data) {
    console.log(data);
});

proc.stderr.on('data', function(data) {
    console.log(data);
});

proc.on('close', function() {
    console.log('finished');
});

我在做什么错了?


错误提示是

1)

D:\vimeo\vimeo-downloader.js:94
const child = execFile('ffmpeg', ['-i', 'input.m4v' , '-i' , 'input.m4a' , '-c' , 'copy' , 'output.mkv' ], (error, stdout, stderr) => {
              ^

ReferenceError: execFile is not defined
    at Object.<anonymous> (D:\vimeo\vimeo-downloader.js:94:15)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

2)

D:\vimeo\vimeo-downloader.js:100
});
 ^

SyntaxError: Unexpected token )
    at Module._compile (internal/modules/cjs/loader.js:721:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

3)

D:\vimeo\vimeo-downloader.js:102
var proc = spawn(cmd, args);
           ^

ReferenceError: spawn is not defined
    at Object.<anonymous> (D:\vimeo\vimeo-downloader.js:102:12)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)   

0 个答案:

没有答案