jbillo / xenonmkv

MKV to MP4 container conversion tool with minimal re-encoding
http://xenonmkv.ev98.net/
GNU General Public License v2.0
19 stars 8 forks source link

About XenonMKV

XenonMKV is a video container conversion tool that takes MKV files and outputs them as MP4 files. It does not re-encode video, and only decodes and encodes audio as necessary.

You'll find this tool useful for converting videos for devices that support H.264 video and AAC audio, but do not understand the MKV container. Other uses include converting videos with AC3 or DTS audio to AAC audio (2 channel or 5.1 channels).

Originally, XenonMKV was meant for Xbox 360 consoles, but I'm finding now that this tool is much more useful for my BlackBerry PlayBook. You may also find it useful for Roku devices or to pre-convert videos to reduce the likelihood that Plex will need to re-encode them.

Disclosure

Note: During early development of this application, I worked for BlackBerry. The opinions expressed here are my own and don’t necessarily represent those of my previous or current employer(s). All code is developed on my own time without use of company resources.

System Requirements

XenonMKV was built and tested on a standard Ubuntu 12.04 LTS installation (x86_64), but most of the utilities and requirements here are possible to run on most popular *nix distributions. I've also given the suite of tools a cursory run on Ubuntu 13.10 and they seem to work. Once 14.04 is released in final form I plan to retest.

Windows 7 (64-bit) and Mac OS X 10.8 are also supported, but may work on different versions of OS X and Windows.

You will need at least Python 2.7 for the argparse library, and ideally Python 2.7.3 or later in the 2.x series.

Ubuntu 12.04

You will need some supporting packages. These will be installed automatically if they are not found on your system, or they can be installed beforehand.

On desktop installations, please ensure that 'Update Manager' is closed before installing dependendencies, or you will receive a nasty message in the form of:

E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?

Install All Dependencies (at once)

sudo apt-get install mediainfo mkvtoolnix mplayer faac gpac

At the current state of development, on Ubuntu you do not need to install any Python packages from requirements.txt as the dependent tools are installed with 'apt'. If this changes in the future, requirements can be installed by running:

sudo apt-get install python-setuptools && sudo easy_install -U pip
pip install -r requirements.txt

Individual Package Details

Ubuntu 10.04

Not currently still tested against, but here are the historical changes required to make XenonMKV functional:

Other Linux Distributions

Install the packages mentioned above, either from source or your distribution's package manager.

Windows

Eventually I will package the application as an .exe file for convenience with Windows installations. XenonMKV has only been tested on Windows 7, 64-bit.

As long as you have all the dependent tools installed, you should only need to have Python installed and add the Python directory to your PATH environment variable.

Basic Use

Development on Windows

To get things up and running for a development environment on Windows, you can perform the following steps:

        self.set_executables(compiler='gcc -O -Wall',
                             compiler_so='gcc -mdll -O -Wall',
                             compiler_cxx='g++ -O -Wall',
                             linker_exe='gcc',
                             linker_so='%s %s %s'
                                        % (self.linker_dll, shared_option,
                                           entry_point))

OS X

XenonMKV has been tested on OS X 10.8. For best results, use the packages offered by the installer. You will need at least OS X 10.6 and a 64-bit capable machine for some of the dependent applications.

Suggested Applications and Optional Tools

Usage

Basic usage with default settings:

xenonmkv.py /path/to/file.mkv

To ensure your Xbox 360 console will play the resulting file, at a possible expense of audio quality:

xenonmkv.py /path/to/file.mkv --profile xbox360

To see all command line arguments:

xenonmkv.py --help

For a quiet run (batch processing or in a cronjob):

xenonmkv.py /path/to/file.mkv -q

The -q option ensures you will never be prompted for input and would be useful for integration with software like SABnzbd+.

If you're reporting an issue, please run XenonMKV in debug/very verbose mode:

xenonmkv.py /path/to/file.mkv -vv

For the latest release of XenonMKV, I've included a really crummy script that handles batch encoding of MKV files on Linux, since I always screw up the parameters passed to find. Use:

batch.py source_directory <xenonmkv_parameters>

Suggestions/Caveats

Audio Downmixing/Re-Encoding

By default, XenonMKV tries not to resample, downmix or re-encode any part of the content provided. However, chances are your source files will contain AC3, DTS or MP3 audio that needs to be re-encoded. In this case, the original source audio will always be downmixed to a two channel AAC file before it is repackaged.

If the audio track in your MKV file is already AAC, the next thing to consider is your playback device. The Xbox 360 will not play audio in an MP4 container unless it is 2-channel stereo, which is a highly stupid limitation. Other devices, like the PlayBook, will happily parse up to 5.1 channel audio. By using either the --channels or --profile settings, you can tell XenonMKV how many channels of audio are acceptable from an AAC source before it will aggressively re-encode and downmix to 2-channel stereo.

In short, if you plan to play MP4s on your Xbox 360, definitely use the --profile xbox360 setting to make sure that no more than two channels make it into the output file. If your device is more reasonable, the default settings should be fine. More profiles will be added as users confirm their own device capabilities.

Known Issues

MP4Box crash with backtrace

Certain video files, when MP4Box loads them to rejoin into an MP4 container, will throw a glibc error beginning with:

*** glibc detected *** MP4Box: free(): invalid next size (fast): 0x0000000000cc8400 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7e626)[0x7f0b09a77626]
/usr/lib/nvidia-current/tls/libnvidia-tls.so.295.40(+0x1c01)[0x7f0b084c7c01]

This occurs with both nVidia proprietary and Nouveau open source drivers. The message above is displayed when using the "current" or "current-updates" versions (295.40, 295.49). When using the 173, 173-updates (173.14.35) or Nouveau open-source driver, the free() error is the same, but the backtrace is different:

*** glibc detected *** MP4Box: free(): invalid next size (fast): 0x00000000022d2420 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7e626)[0x7f5e9820a626]
/usr/lib/x86_64-linux-gnu/libgpac.so.1(minf_del+0x3f)[0x7f5e9867c69f]
/usr/lib/x86_64-linux-gnu/libgpac.so.1(mdia_del+0x25)[0x7f5e9867c395]
/usr/lib/x86_64-linux-gnu/libgpac.so.1(trak_del+0x33)[0x7f5e98680c03]
/usr/lib/x86_64-linux-gnu/libgpac.so.1(gf_isom_box_array_del+0x37)[0x7f5e98692c87]
/usr/lib/x86_64-linux-gnu/libgpac.so.1(moov_del+0x58)[0x7f5e9867c9c8]
/usr/lib/x86_64-linux-gnu/libgpac.so.1(gf_isom_box_array_del+0x37)[0x7f5e98692c87]
/usr/lib/x86_64-linux-gnu/libgpac.so.1(gf_isom_delete_movie+0x3a)[0x7f5e9869ad5a]
/usr/lib/x86_64-linux-gnu/libgpac.so.1(gf_isom_close+0x39)[0x7f5e9869c779]
MP4Box[0x40d30c]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7f5e981ad76d]
MP4Box[0x4085c1]

Sometimes this is an intermittent error, so MP4Box has a 3-retry mechanism that starts with the temporary files. At this time I'm not sure whether it's system memory or an issue with MP4Box/gpac. There were similar crashing issues in the original Windows version, which is why multiple versions of MP4Box were bundled and used for fallback.

If you do see this issue in your own testing, please report it and include a link to the file that causes the problem if possible. You may also be able to get the file to convert by using different command line options, such as --resume-previous --preserve-temp-files or including or excluding -vv.

Relevant system information:

$ uname -a
Linux ubuntu 3.2.0-29-generic #46-Ubuntu SMP Fri Jul 27 17:03:23
UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

My MP4Box version is the default from the 'gpac' Ubuntu 12.04 package, which is 0.4.5+svn3462~dfsg0-1:

$ MP4Box -version
MP4Box - GPAC version 0.4.6-DEV-rev
GPAC Copyright: (c) Jean Le Feuvre 2000-2005
(c) ENST 2005-200X
GPAC Configuration: --build=x86_64-linux-gnu --prefix=/usr --includedir=${prefix}/include --mandir=${prefix}/share/man --infodir=${prefix}/share/info --sysconfdir=/etc --localstatedir=/var --libdir=${prefix}/lib/x86_64-linux-gnu --libexecdir=${prefix}/lib/x86_64-linux-gnu --disable-maintainer-mode --disable-dependency-tracking --prefix=/usr --mandir=${prefix}/share/man --libdir=lib/x86_64-linux-gnu --extra-cflags='-Wall -fPIC -DPIC -I/usr/include/mozjs -DXP_UNIX' --enable-joystick --enable-debug --disable-ssl
Features: GPAC_HAS_JPEG GPAC_HAS_PNG

A fresh compile of the 0.5.0 version available at https://sourceforge.net/projects/gpac/ also appears to trigger this bug:

MP4Box - GPAC version 0.5.0-rev4065
GPAC Copyright: (c) Jean Le Feuvre 2000-2005
    (c) ENST 2005-200X
GPAC Configuration:  --enable-debug
Features: GPAC_DISABLE_3D

The version included with Ubuntu 10.04 (0.4.5-0.3ubuntu6) does not appear to have issues with the same files that fail on 0.4.6 and newer.