Sunday, June 29, 2008

HOWTO: Remux .mkv to .m2ts on linux

A super fast'n'easy way to convert .mkv to a PS3 accepted format without any quality loss.

The script below can be used to remux a "standard" .mkv matroska file to a AVCHD .m2ts file which plays perfectly fine on the Sony PS3 for example. This is possible by using the great software tsmuxer by SmartLabs.


Updates:
  • changed to the native linux version of tsmuxer
  • support for muliple audio lang

Requisites

tsMuxer: http://www.videohelp.com/tools/tsMuxeR
mkvtoolnix: http://www.videohelp.com/tools/MKVtoolnix
dcadec: http://www.videolan.org/developers/libdca.html
aften: http://aften.sourceforge.net/

Installation

1. Make sure the apps above are installed, mkvtoolnix should be available from your dist repo.. i.e. apt-get
2. Copy the script below for example to /usr/local/bin/mkvtom2ts.sh and make it executable.
3. Edit the preferred default language list
4. Test it! mkvtom2ts.sh movie.mkv


#!/bin/bash
#
# mkvtom2ts.sh - 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
#

AUDIO_LANGS="eng und swe"


BASENAME=$(basename "$1" .mkv)
DEST_FILE=$BASENAME.m2ts

MPEG4_TRACK_NO=`mkvinfo "$1" | grep V_MPEG4/ISO/AVC -B10 | grep Track\ number\:\ | awk '{ print $5 }'`

for AUDIO_LANG in $AUDIO_LANGS
do
AC3_TRACK_NO=`mkvinfo "$1" | grep A_AC3 -B10 -C3 | grep Language\:\ $AUDIO_LANG -B13 | grep Track\ number\:\ | awk '{ print $5 }'`
DTS_TRACK_NO=`mkvinfo "$1" | 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, "$1", level=4.1, insertSEI, contSPS, track=$MPEG4_TRACK_NO, lang=eng" >>mux.meta
echo "A_AC3, "$1", 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 "$1" $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


Tuesday, March 25, 2008

It seems that the author of tsmuxer is working on a .srt -> sup builtin conversion, this means that we will be able to get subtitles in a much easier way than I initially thought :) Great stuff! I also noticed that txmuxer is available as a native Linux binary now! I will try to update the script soon to fix this and add some other tweaks as well.