Skip to content
explainroo
FFmpeg and rendering

How to Add Audio to a Video with FFmpeg

Updated 3 min read

On this page
  1. Replace the video’s existing audio
  2. Keep the original sound as a separate track
  3. Check the output
  4. Make a narrated explainer with explainroo

To add a separate audio file to a video, tell FFmpeg which video and audio streams to use. A stream is one part of a media file, such as its video or audio; subtitles can also occupy a stream. FFmpeg combines the selected streams in a new file. This is called muxing.

To replace a video’s existing sound, use -map to select the video from the first file and the audio from the second. The command below copies the video without re-encoding and encodes the new audio as AAC, which works with MP4:

ffmpeg -i video.mp4 -i audio.mp3 \
  -map 0:v:0 -map 1:a:0 \
  -c:v copy -c:a aac -shortest output.mp4

If you want to make a narrated explainer video about a topic rather than edit existing footage, use explainroo. It uses FFmpeg to assemble the video, but it does not edit camera footage. The sections below explain how to run FFmpeg directly and how explainroo makes a new explainer.

Replace the video’s existing audio

In the command, -i adds an input file. FFmpeg numbers input files by their order in the command, and the -map options select the streams to use. Specify the streams you want because FFmpeg’s default selection does not combine every stream you might need. See the FFmpeg stream-selection documentation.

-c:v copy copies the video stream without re-encoding, while -c:a aac encodes the audio as AAC, a common format for MP4 files. -shortest ends the output when the shorter selected stream ends; remove it to let the video continue after the new audio ends. The FFmpeg audio-replacement example uses the same mapping approach.

Put both input options before the output filename. FFmpeg applies options in order, and many options apply to the next input or output file.

Keep the original sound as a separate track

If viewers should be able to choose between the original sound and the new audio, map both audio streams:

ffmpeg -i video.mp4 -i audio.mp3 \
  -map 0:v:0 -map 0:a? -map 1:a:0 \
  -c:v copy -c:a aac output.mp4

Here, 0:a? selects the original audio stream if the file has one. The output has separate tracks for the video, original audio, and new audio. The sounds are not blended together. The FFmpeg new-track example also uses explicit mapping to add an audio stream.

This command maps only video and audio, so subtitles and other streams are not included. Add mappings for any other streams you need. Encoding audio as AAC helps ensure MP4 compatibility. -c copy copies streams without re-encoding, but the output container must support their codecs.

Check the output

Play the new file to check that the audio starts and ends when intended. If the output stops early, check whether -shortest ended it when a mapped audio stream finished. If subtitles or another stream are missing, check the -map options. They control which streams FFmpeg includes.

To troubleshoot, check FFmpeg’s terminal messages for input and output stream details. They show which streams FFmpeg found and mapped.

Make a narrated explainer with explainroo

For a new narrated explainer or product demo, explainroo creates the narration and scenes, then uses FFmpeg to assemble them into an MP4. It is a free, open-source kit under the MIT license. It does not import or edit existing camera footage.

To use it, paste this prompt into a coding agent that can run shell commands:

Make me a short explainer video about [your topic]. Use explainroo for it: clone the explainroo repository, read its AGENTS.md and follow the steps.

The agent sets up explainroo, makes and checks the video, then provides an MP4 file. It writes script.md with the spoken words and scenes.js with the drawings and scene timing. An open voice model reads the script. Whisper identifies when the words are spoken so scenes appear at the right time. FFmpeg combines the video and audio.

The agent also checks the scene stills, text layout, and speech. explainroo works with several coding agents, and it works best with Claude Code. To install it yourself, see explainroo’s setup instructions for prerequisites and commands. The project repository contains the code and setup details.

Free and open source

Let your AI agent make the video

explainroo lets a coding agent like Claude Code or Codex make narrated explainer videos and product demos. The voice, the word timing and the rendering run on your own computer, with no API key and no cost per video.

Copy this into your AI agent

Make me a short explainer video about [your topic]. Use explainroo for it: clone https://github.com/vincentsch/explainroo, read its AGENTS.md and follow the steps.

Getting started Example videos GitHub

Made with explainroo: How a cache makes a website faster

More on ffmpeg and rendering

All guides on ffmpeg and rendering