Monday, November 17, 2014

HOWTO: Get MCE USB remote working in Ubuntu without using LIRC for MythTV or Kodi (xbmc)




Some MCE USB remotes don’t work with in-kernel LIRC. Many people have reported problems with USB3 ports + xhci. 

This approach uses ir-keytable and remaps some MCE remote keys to emulate keyboard key-presses and works natively with applications like MythTV and Kodi (xbmc).


Tested on Ubuntu 14.04 (3.13.x kernel) and 14.10 (3.16.x kernel)
Using “ehome” MCE USB remote IR transceiver (Formosa Infrared Receiver)
Works on USB3 ports with no changes to the BIOS to disable xhci.


# Remove LIRC from your system
% sudo apt-get purge lirc


# If the /etc/lirc folder exists on your system, remove that as well
% sudo rm -rf /etc/lirc


# Install ir-keytable
% sudo apt-get install ir-keytable


# Find the device/eventID for your remote
% ir-keytable
You’ll get an output that looks like this:
Found /sys/class/rc/rc1/ (/dev/input/event10) with:
Driver mceusb, table rc-rc6-mce
Supported protocols: NEC RC-5 RC-6 JVC SONY SANYO LIRC other
Enabled protocols: NEC RC-5 RC-6 JVC SONY SANYO LIRC other
Name: Media Center Ed. eHome Infrared
bus: 3, vendor/product: 147a:e03e, version: 0x1001
Repeat delay = 500 ms, repeat period = 125 ms


# Test the remote keypresses using the eventID from the previous step.  This is similar to using “irw” with lirc.  The output shown below is generated for pressing the “OK” key once on the remote.
% sudo ir-keytable --test --device=/dev/input/event10
You should get an output that looks like the one below.  
1416166547.998594: event type EV_MSC(0x04): scancode = 0x800f0422
1416166547.998594: event type EV_KEY(0x01) key_down: KEY_OK(0x0001)
1416166547.998594: event type EV_SYN(0x00).
1416166548.104565: event type EV_MSC(0x04): scancode = 0x800f0422
1416166548.104565: event type EV_SYN(0x00).
1416166548.353099: event type EV_KEY(0x01) key_up: KEY_OK(0x0001)
1416166548.353099: event type EV_SYN(0x00).


# Generate the keymap for your remote and save the file under /etc/rc_keymaps/.  Call it myrc6_mce.  This will give you the scancodes and hexcodes for all the keys supported by your mceusb remote.
% sudo ir-keytable --read --device=/dev/input/event10 > /etc/rc_keymaps/myrc6_mce


A sample output is below:
scancode 0x800f0400 = KEY_NUMERIC_0 (0x200)
scancode 0x800f0401 = KEY_NUMERIC_1 (0x201)
scancode 0x800f0402 = KEY_NUMERIC_2 (0x202)
scancode 0x800f0403 = KEY_NUMERIC_3 (0x203)
scancode 0x800f0404 = KEY_NUMERIC_4 (0x204)
scancode 0x800f0405 = KEY_NUMERIC_5 (0x205)
scancode 0x800f0406 = KEY_NUMERIC_6 (0x206)
scancode 0x800f0407 = KEY_NUMERIC_7 (0x207)
scancode 0x800f0408 = KEY_NUMERIC_8 (0x208)
scancode 0x800f0409 = KEY_NUMERIC_9 (0x209)
scancode 0x800f040a = KEY_DELETE (0x6f)
scancode 0x800f040b = KEY_ENTER (0x1c)
scancode 0x800f040c = KEY_SLEEP (0x8e)
scancode 0x800f040d = KEY_MEDIA (0xe2)
scancode 0x800f040e = KEY_MUTE (0x71)
scancode 0x800f040b = KEY_OK (0x160)
… <truncated>


# X11 can’t recognize keycodes above decimal 255, so you need to re-map some keys.  Here are the remaps I made below based on my Harmony One remote. Remove the (hexcode) after the remap when you edit the /etc/rc_keymaps/myrc6_mce file
KEY_OK ⇒ KEY_ENTER
KEY_EXIT ⇒ KEY_ESC
KEY_DVD ⇒ KEY_M
KEY_INFO ⇒ KEY_I
KEY_RECORD ⇒ KEY_R
KEY_PLAY ⇒ KEY_P
KEY_PAUSE ⇒ KEY_P
KEY_EPG ⇒ KEY_G


# As an example, a section of the /etc/rc_keymaps/myrc6_mce file should look like this:
scancode 0x800f040f = KEY_I                     #changed from KEY_INFO
scancode 0x800f0410 = KEY_VOLUMEUP (0x73)
scancode 0x800f0411 = KEY_VOLUMEDOWN (0x72)
scancode 0x800f0412 = KEY_CHANNELUP (0x192)
scancode 0x800f0413 = KEY_CHANNELDOWN (0x193)
scancode 0x800f0414 = KEY_FASTFORWARD (0xd0)
scancode 0x800f0415 = KEY_REWIND (0xa8)
scancode 0x800f0416 = KEY_P                     #changed from KEY_PLAY
scancode 0x800f0417 = KEY_R                     #changed from KEY_RECORD
scancode 0x800f0418 = KEY_P                     #changed from KEY_PAUSE
scancode 0x800f0419 = KEY_STOP (0x80)
scancode 0x800f041a = KEY_NEXT (0x197)
scancode 0x800f041b = KEY_PREVIOUS (0x19c)
scancode 0x800f041c = KEY_NUMERIC_POUND (0x20b)
scancode 0x800f041d = KEY_NUMERIC_STAR (0x20a)
scancode 0x800f041e = KEY_UP (0x67)
scancode 0x800f041f = KEY_DOWN (0x6c)
scancode 0x800f0420 = KEY_LEFT (0x69)
scancode 0x800f0421 = KEY_RIGHT (0x6a)
scancode 0x800f0422 = KEY_ENTER                 #changed from KEY_OK
scancode 0x800f0423 = KEY_ESC                      #changed from KEY_EXIT
scancode 0x800f0424 = KEY_M                          #changed from KEY_DVD
scancode 0x800f0425 = KEY_TUNER (0x182)
scancode 0x800f0426 = KEY_G                          #changed from KEY_EPG


# Test the changes.  This can be done by updating the driver
% sudo ir-keytable --write /etc/rc_keymaps/myrc6_mce -d /dev/input/event10
You should see an output like this:
Wrote 63 keycode(s) to driver


% sudo ir-keytabe --test --device=/dev/input/event10
Press the “OK” key on the remote. You should get an output that looks like the one below.  
1416166547.998594: event type EV_MSC(0x04): scancode = 0x800f0422
1416166547.998594: event type EV_KEY(0x01) key_down: KEY_ENTER(0x0001)
1416166547.998594: event type EV_SYN(0x00).
1416166548.104565: event type EV_MSC(0x04): scancode = 0x800f0422
1416166548.104565: event type EV_SYN(0x00).
1416166548.353099: event type EV_KEY(0x01) key_up: KEY_ENTER(0x0001)
1416166548.353099: event type EV_SYN(0x00).


# Finally, to update on startup, just edit /etc/rc_maps.cfg as root and update the table to point to your new myrc6_mce keymap file:
#driver      table                    file
*                rc-rc6-mce          myrc6_mce


# Reboot and try your settings on mythfrontend.
% sudo reboot

Sunday, November 16, 2014

MythTV: Remove commercials and transcode to H.264

If you use MythTV and would like to automatically mark/remove commercials and transcode your recordings to H.264 (Mpeg4),  you should add this script as a "user job" in your mythtv-backend setup.

Pre-requisites:

  • A working MythTV backend setup
  • HandBrakeCLI installed.  
    • To install on Ubuntu, try:  sudo apt-get install handbrake-cli
  • Your MythTV database username and password:
    • You can find these under /etc/mythtv/config.xml
    • Add these to the DBUSER and DBPASSWD lines in the script.  Keep quotes.


Script functions:

0. Invoked as a user-job after a MythTV recording completes
1. Query database to check if invoked for a commercial-free channel
2. If commercial-free channel, go to step 7
3. Flag commercials using "All" methods, copy flagged commercials to cutlist
4. Lossless transcode to remove commercials from the file
5. Rebuild the seek table. Clear cutlist
6. Update database to point to recording with no commercials
7. Remove stale bookmarking and seek information from the database
8. Use Handbrake to transcode recording to H.264 (MPEG-4):
    -- preserve original audio track (5.1, dts etc.)
    -- preserve subtitles
    -- Use x264 encoder for HD-quality video with much reduced file-size
    -- deinterlace video if necessary
    -- create .mp4 file format compatible with Roku or Apple/Android devices
 9. Update database to point to the new .mp4 transcoded file
10. Rebuild seektable for new .mp4 transcoded file


Download and install:

0. You can download the script here.
1. Change the DBUSER and DBPASSWD lines in the script to match your settings.
2. Save the script (as root) to /usr/local/bin
3. Invoke mythtv-setup and navigate to the user-jobs section.
4. Provide the 4 arguments required for the script: %DIR% %FILE% %CHANID% %STARTTIMEUTC% as shown below:


5. For a new recording schedule via Mythweb, select the name of the user job you just added:


6. You should see the job launch after the recording with a "T" icon under "Recorded Programs" in Mythweb, indicating that the file has been transcoded: