This script is intended to be used as MythTV user job. works similar to Mythbrake.


You will get a x264 / x265 encoded file in a matroska (.mkv) container.

  • uses projectx (SD recordings) / ffmpeg (HD recordings) to cut out commercials / utilizes recording's cut list
  • pulls data for title, subtitle, season and episode from the mythtv database
  • resulting filename scheme: TITLE - SxxExx - SUBTITLE.mkv (leaves out season / episode / subtitle info if there is none in MythTV's database, leaves out subtitle aswell if it's equal to the title)
  • will add one audio track for every language provided in recording to the resulting .mkv
  • selects preferred audio language as default language
  • the audio track with most channels is preferred over other tracks for the same language: e.g. 6ch (5.1) > 2ch (stereo)
  • ignores audio languages, if desired
  • waits for other encodings to end which were started by this script (configurable)
  • will start mythtranscode, HandBrakeCLI and mkvmerge with user defined system priority (nice)
  • if run by root, it allows you to set user:group of the encoded file

The script has gone under heavy changes which were done as a "fast" solution for some problems which came up with mythtranscode as cutting tool. So the script is now always choosing projectx for cutting SD material and ffmpeg for HD material (which by the way now is supported by the script :)) Cutting HD recordings will not be precise if you have to cut a lot out of the middle of the recording. Chopping off content from the beginning and ending of a recording works pretty well. I borrowed some code for this from another script on the wiki (lossless transcode + subtitles) and altered some values. The script isn't as clean as it was beforehand, but should do its work :)

Required software

  • mythutil - ships with mythtv (gentoo package media-video/mythtv)
  • HandBrakeCLI - (gentoo package media-video/handbrake)
  • mkvmerge - (gentoo package media-video/mkvtoolnix)
  • projectx_cli - (gentoo package media-video/projectx)
  • mediainfo - (gentoo package media-video/mediainfo)
  • ffmpeg - (gentoo package media-video/ffmpeg)
  • mplex - (gentoo package media-video/mjpegtools)
  • Required perl packages
    • DBI
    • File
    • Getopt


If you use different encoding qualities for different kind of recordings (like I do), you may start this script with the --quality option.

For example, I defined two user jobs

# -> x264 (LQ)
/root/bin/ --chanid=%CHANID% --starttime=%STARTTIME% --directory=%DIR% --file=%FILE% --quality=22

# -> x264 (HQ)
/root/bin/ --chanid=%CHANID% --starttime=%STARTTIME% --directory=%DIR% --file=%FILE% --quality=19

Take a look at the Handbrake documentation for more info on the quality setting of handbrake.


Take a close look at the "Config" section of the script and fit it to your needs / requirements.

I tried to document everything in detail in the comments.

General Syntax --chanid=[int value] --starttime=[int value] --directory=[string value] --file=[string value] --quality=[int value] --verbose

        --chanid        MythTV CHANID [REQUIRED]
        --starttime     MythTV STARTTIME [REQUIRED]
        --directory     MythTV storage directory [REQUIRED]
        --file          MythTV filename of recording [REQUIRED]
        --quality       Constant Quality factor [51..0] used by handbrake
                        Look up ''
                        for more information
                        [OPTIONAL] defaults to $videoQualityDefault
        --threads       Number of threads x264 will utilize for encoding
                        Look up ''
                        for more information
                        [OPTIONAL] integer / floating point number, defaults to 'auto'
        --noCrop        Disable auto image cropping by handbrake
                        [OPTIONAL] toggle
        --verbose       Write output of mythtranscode, handbrake and mkvmerge to logfile
                        [OPTIONAL] toggle

        For installation as mythtv user job, it may be called like this:
        ../ --chanid=%CHANID% --starttime=%STARTTIME% --directory=%DIR% --file=%FILE% --threads=1

Changing ownership of target file

The init script for your distribution will usually start mythbackend as non-privileged user (eg. mythtv). I run mythbackend as root, which allows this user job to set ownership of the final encoded file to another user.

If you wish to skip changing ownership after encoding, just empty the variable $fileOwner like this:

my $fileOwner = "";

If you dislike running mythbackend as root but would like to use this feature, you might want to setup sudo for your mythtv user and allow him to chown files in your target directory.

sudo mini-howto

Execute visudo command (you will have to be root to do this):

# visudo

Your sudoers file will show up in your preferred editor. Just add a line for your mythtv user which allows him to change ownership of files in your target directory without password.

mythtv ALL=NOPASSWD: /bin/chown *\:* /data/movies/x264/[^..]*

Save and quit your editor.