FFmpeg: Change the audio track in a video without loss of quality (MP4)

Note that the duration of a video and audio should be the same. If a video track is shorter than audio, the video will stop and the audio will continue to play. If an audio track is shorter than a video track, then the audio track stops and the video continues.

Anyway I will show how to manage with this problem in the article.

Replace/Copy audio track in video container

A simple copy the audio track into the video can be performed by the following command:

ffmpeg -i video.mp4 -i audio.mp3 -map 0:v -map 1:a -c copy result.mp4

MP4 container supports MP3, AAC, AC3 audio formats. For AAC .m4a container is often using. We can copy audio from .m4a container using the same method:

ffmpeg -i video.mp4 -i audio.m4a -map 0:v -map 1:a -c copy result.mp4

What these options means

-i options means determine input file.

-map 0:v option means select video track in the first file (0 because numbering starts from zero).

-map 1:a option means select audio track in the second file.

-c copy option means copy selected tracks.

Using -shortest option

The option helps to align the duration of video and audio tracks. A video file is cropped at the moment when one of the tracks ends.

For example, the duration of video is 10 seconds and the duration of audio is 15 seconds. Then by using -shortest option video file duration will be 10 seconds (shortest track).

Example of using:

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

But in rare cases this option can deviate by ~1-10 seconds (depending on with which parameters the tracks were encoded).

Therefore, it is better to manually trim a video by time.

Get a specific time interval of audio track

Use -t option to set where an audio track must ends:

ffmpeg -i audio.mp3 -t 00:02:03 -c copy interval.mp3
ffmpeg -i video.mp4 -i interval.mp3 -map 0:v -map 1:a -c copy result.mp4

Use -ss option to set where an audio track must starts:

ffmpeg -ss 00:03:15 -i audio.mp3 -c copy interval.mp3
ffmpeg -i video.mp4 -i interval.mp3 -map 0:v -map 1:a -c copy result.mp4

Use both -ss and -t option to set certain interval of audio track:

ffmpeg -ss 00:01:05 -i audio.mp3 -t 45 -c copy interval.mp3
ffmpeg -i video.mp4 -i interval.mp3 -map 0:v -map 1:a -c copy result.mp4

What these options means

-ss option means set track start time.

-t option means set track end time.

-c copy option means copy selected fragment of the track.

Combine multiple audio tracks into one without quality loss

First, we need to create a text file and list audio tracks in the format I give below. If an audio file is located in the same directory where ffmpeg will be executed we don’t need to specify the path to the file, only its name and ‘file’ word before. Assume we call it mylist.txt:

mylist.txt:

file '/path/to/audio_1.mp3'
file '/path/to/audio_2.mp3'
file '/path/to/audio_3.mp3'

We can then concatenate them together with the following command:

ffmpeg -f concat -i mylist.txt -c copy concatenated.mp3