Video thumbnails for MythTV

My MythTV box has lots of TV episodes on it, as well as films. The MythTV people have got films all sorted nicely; there’s a thing which looks up the film on IMDB, grabs the plot and the cover art, and makes your list of films look all pretty. However, you can’t do that for episodes of TV shows, or random bits of video; they don’t have DVD cover art, unsurprisingly. Lots of people have come up with the idea of using a thumbnail taken from the video as artwork, which is fine, but most people using MythTV are using mplayer to do the thumbnailing (and play videos). Mplayer doesn’t work on my MythTV box (it crashes X), so I use VLC. VLC can theoretically create thumbnails, but I can’t get it to work properly, so I’ve written a script which does it with totem-video-thumbnailer. (myth-thumbnailer.py — download)

 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/python
import MySQLdb, os, re

if not os.path.exists("/usr/bin/totem-video-thumbnailer"):
  raise "This script requires totem-video-thumbnailer"

# read DB connection parameters
fp = open("/etc/mythtv/mysql.txt")
DB = {}
for line in fp:
  if line.find("=") != -1:
    k,v = line.strip().split("=",1)
    DB[k] = v

required_keys = ["DBHostName", "DBUserName", "DBName", "DBPassword"]
for k in required_keys:
  if not DB.has_key(k):
    raise "Missing MySQL connection detail: %s" % k

# connect to database
con = MySQLdb.connect(DB["DBHostName"], DB["DBUserName"], DB["DBPassword"], DB["DBName"])
crs = con.cursor()

# get location where thumbnails are stored
sql = "select data from settings where value = 'VideoArtworkDir';"
crs.execute(sql)
VIDEO_DIR = crs.fetchone()[0]
# get all files without covers
cmd = 'totem-video-thumbnailer -j -s 600 "%s" "%s" >/dev/null 2>&1'
sql = "select intid, filename from videometadata where coverfile = 'No Cover';"
crs.execute(sql)
VIDEOS = []
while 1:
  data = crs.fetchone()
  if not data: break
  intid, filename = data
  parts = filename.split(os.sep)
  output = re.sub("[^a-z0-9_]","_","_".join(parts[-2:]).lower()) + ".jpg"
  full_output = os.path.join(VIDEO_DIR, output)
  if os.path.exists(full_output):
    pass
  else:
    cmd_exec = cmd % (filename, full_output)
    print "Thumbnailing", os.path.split(filename)[1]
    os.system(cmd_exec)
    VIDEOS.append((intid,full_output))

sql = "update videometadata set coverfile = %s where intid = %s limit 1;"
for intid, thumbnail in VIDEOS:
  crs.execute(sql, (thumbnail, intid))
  print "Added %s to database" % thumbnail

More in the discussion (powered by webmentions)

  • (no mentions, yet.)