Freitag, 25. Mai 2012

Archive mythtv recordings to mkv using mythnuv2mkv.sh

Even with the biggest hard disks your recordings will use all the available space some time. Yes, mythtv does deletes old recordings - but some of them you want to keep and archive forever.

There is a nice little script called mythnuv2mkv.sh. The original website is now down, the script has not been updated from the author in the last two years (last published version was v1.63). An updated and extended version is available at http://pastebin.com/ENMjrLK (v1.92). The script runs almost out of the box with mythtv 0.25 and below are the required steps.

Download the script and store it as /usr/bin/mythtvnuv2mkv.sh. Open the script in an editor of your choice and modify around line 2277 and locate:
cat > ${OUTBASE}_tags.xml <<-EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Tags SYSTEM "matroskatags.dtd">
<Tags>
<Tag>
<Targets>
EOF
 and add two lines before this block:

# Remove old file if there
[ -f ${OUTBASE}_tags.xml ] && rm -f ${OUTBASE}_tags.xml 
Save the script, chown owner and group to mythtv and set sticky-group bit.

As the script is quite IO intensive I have added an old 32GB USB stick to my system. The stick becomes available as /mnt/usb-tmp and will hold archived recordings.

Living in Europe I am receiving DVB-S in three formats: 576 (SD format, old resolution), 720p (HD) and 1080p (FullHD). 16:9 and 4:3 format will be automatically identified from mythtv. For each resolution I need one user job in MythTV Backend:

User Job1: Archive 1080: LC_ALL=C /usr/bin/mythnuv2mkv.sh --jobid=%JOBID% --quality=1080 --pass=two --denoise=OFF --deblock=ON --deleterec=OFF --crop=OFF --copydir=/mnt/usb-tmp --chanid=%CHANID% --starttime=%STARTTIME%

User Job2: Archive 720: LC_ALL=C /usr/bin/mythnuv2mkv.sh --jobid=%JOBID% --quality=720 --pass=two --denoise=OFF --deblock=ON --deleterec=OFF --crop=OFF --copydir=/mnt/usb-tmp --chanid=%CHANID% --starttime=%STARTTIME%

User Job3: Archive 576: LC_ALL=C /usr/bin/mythnuv2mkv.sh --jobid=%JOBID% --quality=576 --pass=two --denoise=ON --deblock=ON --deleterec=OFF --crop=OFF --copydir=/mnt/usb-tmp --chanid=%CHANID% --starttime=%STARTTIME%

Make sure /mnt/usb-tmp is writeable for mythtv user and group. The LC_ALL=C definition in front of each command is important for mkvmerge utility which will crash without.

Now to archive a recording you can add a cutlist to a recording and use the menu bar to start one of the three defined archive jobs. Archiving takes time - on my Quad-CPU 90 minutes 720p to mkv need three hours.

Results: Usually the file size are decreased from 10 GB to 2.8 GB.

You can also tweak some defaults in the beginning of the script (ogg vs. faac audio system, mkv or other containers, number of threads etc.).

To avoid stutter while playback you may consider to add a nice -19 to all three jobs and limit the number of concurrent jobs in the backend to two.