Add an audio file to and mkv video

21,338

Solution 1

kdenlive and add a second audio track, then remux

Solution 2

You can install mkvtoolnix;

sudo apt-get install mkvtoolnix

Then use mkvmerge from the installation to mux audio and video into the mkv container from the command line.

There is also a Gui tool, mkvtoolnix-gui that is a front end to mkvtoolnix.

sudo apt-get install mkvtoolnix-gui

Example to mux video file called, input-video.mkv with a sound file called soundfile.mp3, creating a new muxed mkv file called output-with-sound.mkv, do the following in the directory where you have the video without audio and the audio file:

mkvmerge -o output-with-sound.mkv -A input-video.mkv soundfile.mp3

More examples including synchronization here: http://www.bunkus.org/videotools/mkvtoolnix/doc/mkvmerge.html

Solution 3

I'm not sure why mencoder didn't work for you, but you can also do the same thing with ffmpeg. You just need to use two inputs to ffmpeg.

 ffmpeg -i track.mp3 -i out.mkv -vcodec copy -acodec libfaac -ab 192k final.mkv

I didn't try it, but I don't think you can do -acodec copy, especially since you're using two different codecs. Just pick whatever you want in the output and add that in, its really fast compared to the video.

Share:
21,338
Javier López
Author by

Javier López

Updated on September 17, 2022

Comments

  • Javier López
    Javier López almost 2 years

    I'm quite new to multimedia things, I'm trying to find a way to make nice screencasts, I've already been able to capture my desktop with ffmpeg:

    $ ffmpeg -f alsa -ac 2 -i hw:0,0 -f x11grab -r 30 -s $(xwininfo -root | grep 'geometry' | awk '{print $2;}') -i :0.0 -acodec pcm_s16le -vcodec libx264 -vpre lossless_ultrafast -threads 0 -y out.mkv
    

    It works pretty nice, but I'd like to add music in the background, if I add it with mencoder:

    mencoder  -ovc copy -oac mix -audiofile track.mp3 out.mkv -o out.mp4
    

    It delete the video sound, I don't want to replace it, I want to add music to my explanation, any ideas?

  • Javier López
    Javier López over 13 years
    OMG!, I dint' know kdenlive, I'll definitely check it out!, thx, if anyone else knows how to do it in the console please let me know.
  • RobotHumans
    RobotHumans over 13 years
    sox merge described here: sox.sourceforge.net/sox.html#lbAJ I would split the audio out of the video the sox merge them, then mux it back together with the video
  • Javier López
    Javier López over 13 years
    thx a lot aking that's just what I'll do
  • Javier López
    Javier López over 13 years
    thx a lot sabacon!, I didn't know about mkvmerge, I'll use it to mux back the mixed audio
  • augusto
    augusto over 8 years
    correct install command is sudo apt-get install mkvtoolnix-gui
  • Sabacon
    Sabacon over 8 years
    @augusto Thanks a lot, I did not notice the extra hyphen, and to know this thing was there for so long.
  • user2513149
    user2513149 over 5 years
    VLC cannot open the mkv file after adding or removing an audio track using this command.
  • Tiwenty
    Tiwenty over 3 years
    Yes, mkv muxing in ffmpeg is busted. See this issue trac.ffmpeg.org/ticket/6037, they don't even seem to care. Ffmpeg is notorious for being slow, but this is even closing the eyes on the issue.