MythTVの録画データをPodcastにする

サーバの中身が消し飛んだので、前々から使っていたMythTVの録画データをPodcastにするためのスクリプトを一から作り直すはめに… 同じミスを繰り返した場合でも復活できるように、スクリプトをメモ。

#!/usr/bin/ruby require 'dbi' require 'rss' expdir = "/home/foo/export" baseurl = "http://example/~foo/" opturl = "opt/" author = { '1001' => 'NHK', '1003' => 'Eテレ', '1004' => '日本テレビ', '1006' => 'TBS', '1008' => 'フジテレビ', '1010' => 'テレビ朝日', '1012' => 'テレビ東京' } recfiles = Dir.glob(expdir + "/*.m4v") dbh = DBI.connect("dbi:Mysql:mythconverg:localhost","mythtv","Password") dbh.do("SET CHARACTER SET utf8") rss = RSS::Maker.make("2.0") {|maker| maker.channel.title = "MythTV Recorded" maker.channel.description = "MythTV Recorded Videos" maker.channel.link = baseurl maker.channel.itunes_author = "example.com" maker.channel.itunes_categories.new_category {|category| category.text = "TV & Film" } maker.items.do_sort = true recfiles.each {|recfile| filename = File::basename(recfile) chanid, starttime, dummy = filename.split(/[_.]/) query = "select * from recorded where chanid='#{chanid}' and starttime='#{starttime}'" sth = dbh.execute(query) sth.fetch_hash {|row| item = maker.items.new_item item.title = row['title'] item.date = row['starttime'] item.enclosure.url = baseurl + opturl + filename item.enclosure.length = File::size(recfile) item.enclosure.type = "video/mp4" item.description = row['description'] item.itunes_subtitle = row['subtitle'] item.itunes_author = author[chanid] item.itunes_keywords = row['category'] } } } dbh.disconnect if dbh print rss.to_s

実行すると標準出力にrdfを吐くので、吐き出したrdfをWebサーバ上のどこかに置いて、iTunesとかにつっこめばPodcastとして録画データを視聴可能。 Podcastにしとくと、iPod/iPhoneにも入れられるし、AppleTVからも見られるのでなかなか便利っすよ。