Combine Audio and Video with ffmpeg

By  on  

We have audio.  We have video.  We appreciate each of those media on their own but you can create an awesome work of art if you combine the two.  After all, how could you watch a sports highlight video without setting the visuals to some obscure techno track?  Blasphemy.

My favorite A/V utility, ffmpeg, allows you to merge an audio file and a video file into one brilliant piece of art:

ffmpeg -i video.mp4 -i audio.m4a -c:v copy -c:a copy output.mp4

You can even use an animated GIF for the video piece:

ffmpeg -i video.gif -i audio.m4a -c:v copy -c:a copy output.mp4

Banter aside, there are many good cases for combining an audio track with a video track, many examples being seen on YouTube.  Happy media creation!

Recent Features

  • By
    Designing for Simplicity

    Before we get started, it's worth me spending a brief moment introducing myself to you. My name is Mark (or @integralist if Twitter happens to be your communication tool of choice) and I currently work for BBC News in London England as a principal engineer/tech...

  • By
    39 Shirts – Leaving Mozilla

    In 2001 I had just graduated from a small town high school and headed off to a small town college. I found myself in the quaint computer lab where the substandard computers featured two browsers: Internet Explorer and Mozilla. It was this lab where I fell...

Incredible Demos

  • By
    MooTools’ AutoCompleter Plugin

    One of the famous MooTools plugins is Harald Kirschner's AutoCompleter plugin. AutoCompleter takes a term input by the user and searches for matches -- an obviously help to the user. Here's how to make the most of Harald's great plugin. The XHTML All we...

  • By
    Printing MooTools Accordion Items

    Sometimes we're presented with unforeseen problems when it comes to our JavaScript effects. In this case, I'm talking about printing jQuery and MooTools accordions. Each "closed" accordion content element has its height set to 0 which means it will be hidden when the...

Discussion

  1. Antoni

    There is a Firefox extension for this if anybody prefers UI over command line, https://addons.mozilla.org/en-US/firefox/addon/media-converter-and-muxer

  2. If you don’t want to re-encode audio and video, you can use following command:

    ffmpeg -i video.mp4 -i audio.m4a -c:v copy -c:a copy output.mp4
    

    It will execute a lot faster.

    • Updated! Thank you!

    • Bumpledink Scrotleworth

      You can specify to copy both at once like this:

      ffmpeg -i video.mp4 -i audio.m4a -c copy output.mp4
    • This works like charm and looks few flags shorter too.

  3. Hi David,

    Nice article. Would these same commands work efficiently on android too?

    We have a mute video to which we hope to add audio recorded from phone.

    Any guidance would be great.

  4. JC

    Can’t find ffmpeg in a zip file. Any other similar tool that opens in Windows?

  5. Kevin

    I’m not a programmer so I’m sort of confused and frustrated at what I’m looking at. I have no idea how to use this lol

  6. howard Johnson

    Here is an example batch file that you can drag the two files onto to do the joining, delete the two files and rename to the original. (drag by the mp4 file)

    @echo off 
    "C:\Program Files (x86)\ffmpeg\bin\ffmpeg.exe" -i %1 -i %2 -c:v copy -c:a copy d:\folder\output.mp4
    del %1
    del %2
    copy d:\folder\output.mp4 %1
    del d:\folder\output.mp4 
    pause
    
  7. Ravi Sharma

    where do we keep ffmpeg.exe file and that audio and video files to whom we merge.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!