How to Add Audio to a Video using FFmpeg

9 November 2022 | 2 min read
Casper Kloppenburg

Prerequisites

FFmpeg is a free and open-source video editing tool capable of trimming, cropping, concatenating, muxing, and transcoding almost any type of media file you throw at it.

It's also a very robust solution for implementing video automation, as we use it extensively in our own video editing API. For this tutorial we'll use FFmpeg 5.1.2, but any recent version will do.

Adding audio to a video

To add an audio track to a video file without the need to re-encode the video, use the following command:

$ ffmpeg -i video.mp4 -i audio.mp3 -map 0:v -map 1:a -c:v copy -c:a copy -shortest output.mp4
  • With the -map 0:v -map 1:a arguments, the video track from the first input (0:v) and the audio track from the second input (1:a) are mapped to the output.
  • The -c:v copy arguments tell FFmpeg not to re-encode the video.
  • By default, when FFmpeg receives multiple input files, it uses the length of the longest input file as the output duration. We're going to use the shortest duration here, so we'll use -shortest.

Fading out the audio

If the audio track is longer than the video, you might want to fade it out to keep it from cutting abruptly. This can be done with the afade filter:

$ ffmpeg -i video.mp4 -i audio.mp3 -af "afade=out:st=10:d=2" -map 0:v -map 1:a -c:v copy -shortest output.mp4 With st=10 and d=2, we specify a start and a duration for the afade filter. This results in the audio fading out after 10 seconds for 2 seconds.

Start automating today

Start with a full-featured trial with 50 credits, no credit card required.
Get started for free