04.04
If, like me, you have alot of MKV’s lying around on your machine, my PS3 will play them using the PS3 Media Server (which is awesome by the way, nothing comes close as far as i’m concerned) as it will use mplayer or tsMuxeR to transcode them on the fly to the PS3.
But, why bother? Why put my PC through the stress of re-encoding video on the fly, just so my PS3 can play them. MKV is just a container, it contains a perfectly playable Mpeg stream and a DD 5.1 sound track, that the ps3 can play flawlessly, it just doesn’t understand the MKV format (yet, come on Sony…).
So, let’s just remux the video to an M2TS, as these can be played by the PS3. Remuxing means, taking the audio/video stream we are after from the file, and not re-encoding them at all, just repackaging them. As i said before, MKV is just a container, it is a method of storing video/audio content.
First off, you will need some MKV tools, lets get those quickly.
% sudo apt-get install mkvtoolnix
To remux, you will need a tool called tsMuxeR, which is freeware, thankfully. It has also been recently updated to include the up-till-now windows only gui. Which is great!
So, let’s download it, fire up a terminal of your choice, and lets get cracking. We’ll be working in your home folder first.
% cd ~/ % mkdir tsmuxer % cd tsmuxer % wget http://www.smlabs.net/tsMuxer/tsMuxeR_1.8.35\(b\).tar.gz % tar zxvf ts*.gz
Now, you will have a few files in that folder, we are only interested in one of them for this post, but feel free to have a play with the GUI. Let’s move the tsMuxeR into the bin folder so our script we’re about to write will be able to access it.
% sudo mv tsMuxeR /usr/bin/
Now, tsMuxeR, should be accessible from anywhere on your machine. Test it just by typing tsMuxeR and it should spit out the version number, and some usage information.
So lets get onto our script, we want to be able to convert any MKVs, to M2TS, whilst also pulling out the language that we speak, with the best track. I.e. if there is a 5.1 channel track, use that instead of stereo.
This is a script that i have modified that was written a while ago, but it does the job, quite well too.
# /bin/bash # - a simple wrapper around the tsmuxeR # Creates a m2ts from a "standard" mkv (assuming video is MPEG4, and sound is AC3 or DTS) # # v0.1 initial version # v0.2 added DTS support # v0.3 changed to tsmuxer linux version + added multiple audio lang support # # Usage: mkvtom2ts filename.mkv # # Change this variable here, to change the languages it will strip from the MKV video. AUDIO_LANGS="und eng" #Loop through all MKVs in the current directory for f in ./*.mkv do BASENAME=$(basename "$f" .mkv) DEST_FILE=$BASENAME.m2ts MPEG4_TRACK_NO=`mkvinfo "$f" | grep V_MPEG4/ISO/AVC -B10 | grep Track\ number\:\ | awk '{ print $5 }'` for AUDIO_LANG in $AUDIO_LANGS do AC3_TRACK_NO=`mkvinfo "$f" | grep A_AC3 -B10 -C3 | grep Language\:\ $AUDIO_LANG -B13 | grep Track\ number\:\ | awk '{ print $5 }'` DTS_TRACK_NO=`mkvinfo "$f" | grep A_DTS -B10 -C3 | grep Language\:\ $AUDIO_LANG -B13 | grep Track\ number\:\ | awk '{ print $5 }'` if [ -n "$AC3_TRACK_NO" -o -n "$DTS_TRACK_NO" ] then break fi done echo "Video(V_MPEG4/ISO/AVC) track no : $MPEG4_TRACK_NO" echo "Audio(A_AC3) $AUDIO_LANG track no : $AC3_TRACK_NO" echo "Audio(A_DTS) $AUDIO_LANG track no : $DTS_TRACK_NO" #audio ac3->direct muxing if [[ $AC3_TRACK_NO -gt "0" ]] then echo "Found ac3 track, muxing directly..." rm -f mux.meta echo "MUXOPT --no-pcr-on-video-pid --new-audio-pes --vbr" >> mux.meta echo "V_MPEG4/ISO/AVC, "$f", level=4.1, insertSEI, contSPS, track=$MPEG4_TRACK_NO, lang=eng.meta" >> mux.meta echo "A_AC3, "$f", track=$AC3_TRACK_NO, lang=eng" >> mux.meta tsMuxeR mux.meta $DEST_FILE rm -f mux.meta else if [[ $DTS_TRACK_NO -gt "0" ]] then echo "No ac3 but dts, converting to ac3.." mkvextract tracks "$f" $DTS_TRACK_NO:"$BASENAME.dts" $MPEG4_TRACK_NO:"$BASENAME.mpeg4" dcadec -r -o wavall "$BASENAME.dts" > "$BASENAME.wav" aften "$BASENAME.wav" "$BASENAME.ac3" echo "Muxing..." rm -f mux.meta echo "MUXOPT --no-pcr-on-video-pid --new-audio-pes --vbr" >> mux.meta echo "V_MPEG4/ISO/AVC, "$BASENAME.mpeg4", level=4.1, insertSEI, contSPS, track=1, lang=eng" >> mux.meta echo "A_AC3, "$BASENAME.ac3", track=1, lang=eng" >> mux.meta tsMuxeR mux.meta $DEST_FILE rm -f mux.meta $BASENAME.dts $BASENAME.wav $BASENAME.ac3 $BASENAME.mpeg4 else echo "No ac3 or dts, exiting..." fi fi if (disaster-condition) then #Abandon the loop. break fi done
Have a quick read over the script, don’t worry about not understanding it, it’s not important, so long as it works
Lets make this file, and put it somewhere we can always use it.
% gedit allmkv2m2tsNow copy all the code above which does the converting, and paste it into that file, then save and close gedit.
% sudo mv allmkv2m2ts /usr/bin/mkvall2m2ts % sudo chmod 777 /usr/bin/allmkv2m2ts
It is now in the bin folder, so can be accessed anywhere, it can also used and edited by anyone.
And thats it. Now navigate yourself to your folder with all the MKVs in the root, and just type..
% allmkv2m2tsAsif by magic, it should now start re-muxing all your MKVs to M2TS.
Please feel free to comment, and if you wrote the original script, please contact me and i will link to you.
Thanks for the tutorial! I was able to combine it with this article.
thanks a lot for this, I need this gonne try it on ubuntu 9.10
the mkv have subs, can i also convert that?
I’m afraid i’ve never had any MKV’s with subs, i suggest having a look at the ts muxer gui application.
tnx, how i get muxer gui application?, I am now use mkvall2m2ts,
Thanks
greets, Dennis
hi good job, great script
but have you any idea, how we can do with mkv with subtitle ?
i’ve got many mkv like that, i know that mkvtoolnix can extrat it
but i don’t know how to do it automaticly and mux it
carn you help me
In my opinion, this is a brilliant script. I’m not as well versed and don’t understand what you are doing in sections of it like echo’ing to mux.meta – but it’s awesome.
I have to ask, have you ever considered Bashing MKV to M4V? The PS3 will play MP4 files with AC3 audio passthrough muxed with MP4Box, added benefit over M2TS that iTunes will catalog it.
Thanks.