Skip to content

Instantly share code, notes, and snippets.

@Saccarab
Created June 9, 2019 08:54
Show Gist options
  • Save Saccarab/c8e0540e8a9ba26c771c2808c804e066 to your computer and use it in GitHub Desktop.
Save Saccarab/c8e0540e8a9ba26c771c2808c804e066 to your computer and use it in GitHub Desktop.
fluent-ffmpeg merge audio with video
const command = ffmpeg()
const commandArray = []
command.addInput(`./best.mp4`)
command.addInput(`./best2.wav`)
commandArray.push(`[1]volume=0.1[a1]`)
command.addInput(`./voiceover.mp3`)
commandArray.push(`[2]volume=0.9[a2]`)
let ffmpegKeys = '[a1][a2]amix=inputs=2[a]'
commandArray.push(ffmpegKeys)
command.complexFilter(commandArray)
command.addOptions(['-map 0:v', '-map [a]', '-c:v copy'])
.format('mp4')
.on('error', (error) => {
console.log(error)
})
.on('end', function() {
console.log('Merging finished !');
})
.save('./merged.mp4');
@erickythierry
Copy link

like this:

import ffmpeg from 'fluent-ffmpeg'

function merge(video, audio) {
    ffmpeg()
        .addInput(video)
        .addInput(audio)
        .addOptions(['-map 0:v', '-map 1:a', '-c:v copy'])
        .format('mp4')
        .on('error', error => console.log(error))
        .on('end', console.log(' finished !'))
        .saveToFile('merged.mp4')
}
merge('./video.mp4', './audio.webm')

@luciano-repetti
Copy link

Hi! I get this error, I've been looking for a long time how to merge audio and video and it's taking a while

This is my code:

const audioStream = ytdl(URL as string, {
          filter: 'audioonly',
          quality: 'highestaudio',
        })

        const videoStream = ytdl(URL as string, {
          filter: (format) =>
            format.hasVideo &&
            (format.container === 'mp4' || format.container === 'webm'),
          quality: qualityOption,
        })


        const combinedStream = new PassThrough()

        // Usar ffmpeg para combinar video y audio y enviar la salida directamente al objeto de respuesta (res)
        ffmpeg()
          .input(videoStream)
          .input(audioStream)
          .addOptions(['-map 0:v', '-map 1:a', '-c:v copy'])
          .format('mp4')
          .on('error', (error) => {
            console.error('Error durante la combinación:', error)
            res.status(500).json({ error: 'Error durante la combinación de video y audio' })
          })
          .on('end', () => {
            console.log('Combinación de video y audio completada con éxito.')
          })
          .pipe(combinedStream, { end: true })

        // Transmitir la salida combinada al objeto de respuesta (res)
        combinedStream.pipe(res, { end: true })

        res.setHeader('Content-Type', 'video/mp4')

This is the error:

    at proto.mergeAdd.proto.addInput.proto.input (E:\proyectos\download_mp3\down_media\node_modules\fluent-ffmpeg\lib\options\inputs.js:42:15)
    at downloadMedia (webpack-internal:///(api)/./src/pages/api/downloadMedia.ts:57:91)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)```
    

Basically it is an error because ffmpeg does not allow more than one entry.

any help is welcome, thank's.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment