~/Blog

Brandon Rozek

Photo of Brandon Rozek

PhD Student @ RPI studying Automated Reasoning in AI and Linux Enthusiast.

Replace Audio in Video

Published on

Updated on

Warning: This post has not been modified for over 2 years. For technical posts, make sure that it is still relevant.

I recorded a video and wanted to touch up my audio in audacity. Here’s how I used ffmpeg to extract the audio, and then replace it with a modified version.

Extract Audio

If you know the format of the audio (mp3, ogg, aac) then it’s possible to do a byte copy of the audio track into a file:

ffmpeg -i input_video.mkv -vn -acodec copy output.aac
Argument Description
-i Input
-vn No Video
-acodec copy Copy audio stream directly

If you don’t know the audio codec and have mediainfo installed, then run

mediainfo --Inform="Audio;%Format%" input_video.mkv

If you gave up, then you can transcode the audio (will take longer than direct copy)

ffmpeg -i input_video.mkv -vn output.aac

Replacing Audio

Once you’re done touching up the audio (touchup.mp3), you’ll want to replace the existing audio with it.

ffmpeg -i input_video.mkv \
       -i touchup.mp3 \
       -c:v copy \
       -map 0:v:0 \
       -map 1:a:0 \
       output_video.mp4
Argument Description
-i Inputs
-c:v copy Make this a copy operation
-c:v copy -map 0:v:0 Map the video from the first input to the first video output
-map 1:a:0 Map the audio from the second input to the first video output
Reply via Email Buy me a Coffee
Was this useful? Feel free to share: Hacker News Reddit Twitter

Published a response to this? :