Get needed software
sudo apt-get install gpac mkvtoolnix
Extract video and audio from matroska file
First you need to check what tracks the matroska file contains with
mkvmerge --identify video.mkv
File 'video.mkv': container: Matroska
Track ID 1: video (V_MPEG4/ISO/AVC)
Track ID 2: audio (A_AC3)
Then extract video and audio according to their tracks with:
mkvextract tracks video.mkv 1:video.h264 2:audio.ac3
Extracting track 1 with the CodecID 'V_MPEG4/ISO/AVC' to the file 'video.h264'. Container format: AVC/h.264 elementary stream
Extracting track 2 with the CodecID 'A_AC3' to the file 'audio.ac3'. Container format: Dolby Digital (AC3)
Mux audio and video into mp4 containter
MP4Box -fps 24 -add video.h264 -add video.ac3 video.mp4
AVC-H264 import - frame size 1280 x 720 at 24.000 FPS
Import results: 31671 samples - Slices: 5672 I 125048 P 122648 B - 32288 SEI - 4936 IDR
AC3 import - sample rate 48000 - 6 channels
Saving to video.mp4: 0.500 secs Interleaving
I had to add
-fps 24 option because
MP4Box didn't detect video fps correctly. Depending on usage
AC3 audio track should be re-encoded into something else before muxing.
Now you have remuxed streams from matroska container into mp4 container without losing any quality.
MP4 doesn't officially support AC3 audio so the audio track should be converted into a supported format (eg. AAC, MP3) if you want the file to be playable with something else than VLC.