nick.recoil.org

⇐ Back to Code index

Ruby-MythTV

Description

A pure Ruby implementation of the MythTV Backend protocol, and a MySQL database wrapper to allow interaction with a MythTV server. Features include browsing and streaming of recordings, thumbnail generation, listing channels and programs, and recording schedule editing. Currently the most complicated use of the gem is from the tests in the test/ subdirectory.

See http://github.com/nickludlam/ruby-mythtv for more details.

Simple streaming example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

  require 'ruby-mythtv'
  
  # Connect to the server
  mythbackend = MythTV.connect_backend(:host => 'mythtv.localdomain')
  
  # Get an array of recordings
  recordings = mythbackend.query_recordings
  
  # Download a recording
  mythbackend.download(recordings[0])
  
  # Stream a recording into a block
  mythbackend.stream(recordings[0], :transfer_blocksize => 65535) do |chunk|
    ..do something with the 64k chunk..
  end
  
  # Generate a thumbnail of the most recent recording, at 60 seconds in from the start
  preview_thumbnail = mythbackend.preview_image(recordings[0], :secs_in => 60)
  File.open('preview_thumbnail.png', 'w') { |f| f.write(preview_thumbnail) }

Advanced scheduling example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

  require 'ruby-mythtv'

  # Connect to the server
  mythbackend, mythdb = MythTV.connect(:host => 'mythtv.localdomain',
                                       :database_password => 'password')

  # Find matches on our search term, and limit the results to 5 matches
  programs = mythdb.list_programs(:conditions => ['title LIKE ?', "%SEARCH TERM%"],
                                  :limit => 5)
  
  # Take the first program match, and convert it to a recording schedule
  new_schedule = MythTV::RecordingSchedule.new(programs[0], mythdb)
  new_schedule.save
  
  # Signal the backend of recording changes for our recording schedule entry
  mythbackend.reschedule_recordings(new_schedule.recordid)
  
  # Let the backend resolve matches
  sleep(5)

  # Enumerate the list of pending recordings, find ours, and check for any conflicts
  pending_recordings = mythbackend.query_pending
  conflicts = pending_recordings.find { |p| p.recordid == new_schedule.recordid &&
                                            p.recstatus_sym == :rsConflict }
  
  # If conflicts is empty, then all is good. If it is populated, then action needs
  # to be taken, such as bumping the priority, or removing the clashes....

Search

Sections

About Nick

I am a freelance technology consultant and developer working in London, with a particular interest in web development and video media.

This site contains my thoughts about technology, the universe and everything. If you would like to get in contact, have a look at the About me page.