如何使用 Node.js 将 webm 文件列表转换并合并为单个大型 mp4 文件?

问题描述 投票:0回答:1

我在浏览器中使用 MediaRecorder 每 5 秒获取一块 webm 文件,因此最终会得到存储在 AWS S3 中的 webm 文件列表。

然后,我目前正在尝试借助 ffmpeg 将它们转换为 Node.js 应用程序中的一个 MP4 文件。我的代码如下所示:

async function convertAndMergeWebmStreams(webmStreams, outputFilePath) {
    return new Promise((resolve, reject) => {
        const command = ffmpeg();
    
        // Create a PassThrough stream to combine multiple readable streams
        const combinedStream = new PassThrough();
        
        // Pipe each webmStream into the combinedStream
        webmStreams.forEach(webmStream => {
            webmStream.pipe(combinedStream, { end: false });
        });

        // Set the combinedStream as the input for ffmpeg
        command.input(combinedStream);
    
        // Set input options
        command.inputOptions('-f webm');
    
        // Set output options
        command.outputOptions([
            '-c:v copy',
            '-c:a aac',
        ]);
    
        // Set the output format
        command.toFormat('mp4');
    
        // Set the output file path
        command.output(outputFilePath);
    
        // Event handling
        command
            .on('start', commandLine => {
                console.log('Spawned Ffmpeg with command: ' + commandLine);
            })
            .on('codecData', data => {
                console.log('Input is ' + data.audio + ' audio ' +
                'with ' + data.video + ' video');
            })
            .on('progress', progress => {
                console.log('Processing: ' + progress.percent + '% done');
            })
            .on('stderr', stderrLine => {
                console.log('Stderr output: ' + stderrLine);
            })
            .on('error', (err, stdout, stderr) => {
                console.error('Error converting and merging streams:', err);
                console.error('ffmpeg stdout:', stdout);
                console.error('ffmpeg stderr:', stderr);
                reject(err);
            })
            .on('end', () => {
                console.log('Conversion and merging finished successfully.');
                resolve();
            });
    
        // Run the command
        command.run();
    });
}

现在 ffmpeg 说:

[matroska,webm @ 0x3712a6f0] EBML header parsing failed
[in#0 @ 0x3712a5f0] Error opening input: Invalid data found when processing input
Error opening input file pipe:0.
Error opening input files: Invalid data found when processing input

convertAndMergeWebmStreams error:  Error: ffmpeg exited with code 183: Error opening input file pipe:0.
Error opening input files: Invalid data found when processing input

基于此链接,这似乎是一个已知问题,即它没有来自浏览器的 MediaRecorder 的有效 EBML 标头。还有另一种方法可以合并它们并获得一个 MP4 文件吗?

node.js ffmpeg mediarecorder webm web-mediarecorder
1个回答
0
投票

原来是因为我的 webms 是一个较大文件的块,这使得除第一个文件之外的所有文件都没有有效的标头。

© www.soinside.com 2019 - 2024. All rights reserved.