How to concatenate two MP4 files using FFmpeg?

后端 未结 22 3097
遇见更好的自我
遇见更好的自我 2020-11-22 02:32

I\'m trying to concatenate two mp4 files using ffmpeg. I need this to be an automatic process hence why I chose ffmpeg. I\'m converting the two files into .ts files and th

22条回答
  •  旧时难觅i
    2020-11-22 03:18

    From the documentation here: https://trac.ffmpeg.org/wiki/Concatenate

    If you have MP4 files, these could be losslessly concatenated by first transcoding them to MPEG-2 transport streams. With H.264 video and AAC audio, the following can be used:

    ffmpeg -i input1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts
    ffmpeg -i input2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate2.ts
    ffmpeg -i "concat:intermediate1.ts|intermediate2.ts" -c copy -bsf:a aac_adtstoasc output.mp4
    

    This approach works on all platforms.

    I needed the ability to encapsulate this in a cross platform script, so I used fluent-ffmpeg and came up with the following solution:

    const unlink = path =>
      new Promise((resolve, reject) =>
        fs.unlink(path, err => (err ? reject(err) : resolve()))
      )
    
    const createIntermediate = file =>
      new Promise((resolve, reject) => {
        const out = `${Math.random()
          .toString(13)
          .slice(2)}.ts`
    
        ffmpeg(file)
          .outputOptions('-c', 'copy', '-bsf:v', 'h264_mp4toannexb', '-f', 'mpegts')
          .output(out)
          .on('end', () => resolve(out))
          .on('error', reject)
          .run()
      })
    
    const concat = async (files, output) => {
      const names = await Promise.all(files.map(createIntermediate))
      const namesString = names.join('|')
    
      await new Promise((resolve, reject) =>
        ffmpeg(`concat:${namesString}`)
          .outputOptions('-c', 'copy', '-bsf:a', 'aac_adtstoasc')
          .output(output)
          .on('end', resolve)
          .on('error', reject)
          .run()
      )
    
      names.map(unlink)
    }
    
    concat(['file1.mp4', 'file2.mp4', 'file3.mp4'], 'output.mp4').then(() =>
      console.log('done!')
    )
    

提交回复
热议问题