Add a new audio track to an existing file

There is yet another common use of FFMPEG, i.e. adding a new audio track to an existing file which can be a video-only file or a video file already with an audio track. A use case will be adding a second audio track to a file already with video and sound. The following is an example of the command to add audio.dts audio track to an existing mkv file:

ffmpeg -i input.mkv -i audio.dts -map 0 -map 1 -c copy output.mkv

Correcting for audio/video sync issues

Due to various reasons, the audio and video tracks sometimes may become out-of-sync in a video clip, i.e. there are some delay between the tracks affecting the overall viewing experience. Use the following as examples to fix the issue.

CASE 1: Audio happens before video (aka “need to delay audio stream 1”):

ffmpeg -i clip.mp4 -itsoffset 0.150 -i clip.mp4 -vcodec copy -acodec copy -map 0:0 -map 1:1 output.mp4

The “itsoffset” in the above example is placed before file 1 (remember that linux counts from 0, so 0 is the first and 1 is the second), so when the mapping happens, it says “Take the video of file 0 and the audio of file 1, leave the video of file 0 alone and apply the offset to the audio of file 1 and merge them into a new output file”. The delay is only .15 seconds.

CASE 2: Video happens before audio (aka “need to delay video stream 0”):

ffmpeg -i clip.mp4 -itsoffset 0.150 -i clip.mp4 -vcodec copy -acodec copy -map 0:1 -map 1:0 output.mp4

The “itsoffset” in the above example is placed before file 1. When the mapping happens, it says “Take the audio of file 0 and the video of file 1, leave the audio of file 0 alone and apply the offset to the video of file 1 and merge them into a new output file”. The delay is only .15 seconds.

Swap the audio tracks without re-encoding

Many video file formats allow you to have multiple sound tracks (e.g. karaoke videos) where the first audio track is treated as the default by many audio-visual editing applications. Many of those can only handle one audio track which is the first one. If you want to switch the audio tracks, you can use FFMPEG without re-encoding the media. The following example is the command to switch the two audio tracks in a video file:

ffmpeg -i input -map 0:v:0 -map 0:a:1 -map 0:a:0 -c copy output

Increasing volume in a video without re-encoding

I needed for a reliable, fast and non-destructive way to increase gain on a video’s audio track. I found it in using the ffmpeg tool, which is a swiss army knife for dealing with video and audio files. It’s conveniently cross-platform and works wonders.
To increase the volume of the first audio track for 10dB use:
ffmpeg -i inputfile -vcodec copy -af “volume=10dB” outputfile
To decrease the volume of the first audio track for 3dB use:
ffmpeg -i inputfile -vcodec copy -af “volume=-3dB” outputfile