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.
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
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.