fluent-ffmpeg / node-fluent-ffmpeg

A fluent API to FFMPEG (http://www.ffmpeg.org)
MIT License
7.82k stars 874 forks source link

How to concat audio with video using fleunt--ffmpeg #1118

Open rafapgomes-zz opened 3 years ago

rafapgomes-zz commented 3 years ago

Im trying to merge a audio and a video file using ffmpeg-concat. I can merge two mp4 files with no audio, but when I try to put my mp3 file I receive an error

Version information

Code to reproduce


var ffmpeg = require("fluent-ffmpeg");

var firstFile = "./imagensconvertidas/1.mp4";
var secondFile = "./imagensconvertidas/2.mp4";
var thirdFile = "./imagensconvertidas/3.mp4";
var outPath = "out.mp4";

var proc = ffmpeg(firstFile)
    .addInput(secondFile)
    .addInput(thirdFile)
    .addInput("./musicas/1.mp3")
    .mergeToFile(outPath)
    .on('end', function() {
      console.log('files have been merged succesfully');
    })
    .on('error', function(err) {
      console.log('an error happened: ' + err.message);
    })

(note: if the problem only happens with some inputs, include a link to such an input file)

Expected results

files have been merged succesfully

Observed results

an error happened: ffmpeg exited with code 1: Cannot find a matching stream for unlabeled input pad 3 on filter Parsed_concat_0

wesbos commented 1 year ago

Same issue - I try merge three mp3s, and I get this error:

Cannot find a matching stream for unlabeled input pad 2 on filter Parsed_concat_0

The first input has multiple streams, but I am selecting 0:0. If I switch it so the first input only has 1 stream, it works

xts-bit commented 1 year ago

@wesbos Any solution how to merge audio to a video in nodejs?

wesbos commented 1 year ago

@xts-bit I switched to this package: https://github.com/FFmpeg-wasm/FFmpeg.wasm

xts-bit commented 1 year ago

@wesbos Can you give me an example to do that? for adding a audio to a video in nodejs

wesbos commented 1 year ago

Ask ChatGPT for the FFmpeg commands, they translate perfect to the wasm package

xts-bit commented 1 year ago

@wesbos Are you sure i tried this.

import { path as ffmpegPath } from '@ffmpeg-installer/ffmpeg';
import ffmpeg from '@ffmpeg.wasm/main';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import path from 'path';

// ffmpeg.setFfmpegPath(ffmpegPath);

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const video = path.resolve(__dirname, 'video1.mp4');
const audio = path.resolve(__dirname, 'audio.mp3');
const destination = path.resolve(__dirname, 'output.mp4');

setTimeout(() => {
  ffmpeg()
    .input(video)
    .input(audio)
    .complexFilter([
      {
        filter: 'amix', options: { inputs: 2, duration: 'longest' }
      }
    ])
    .on('end', async function (output) {
      console.log(output, 'files have been merged and saved.')
    })
    .saveToFile(destination)
}, 3000);
xts-bit commented 1 year ago

@wesbos Can you please help?

klesun commented 4 months ago

From my experience wasm package, while usable, was magnitudes slower than actual ffmpeg, so if performance is crucial, I would not recommend it