Roll your own Media Database
From techdaze
Contents |
Roll your own Media Database
Example: Brxlbravo
Example: Metadata parser
Example: File Uploader
DoingSomethingWithAllFilesInaDirectory
Metadata
Metadata is simply information about a particular (media) file that's considered secondary to its primary content. For instance, consider a JPEG file: in addition to the actual pixels of image data, the image has dimensions, and a particular color depth. Also, a JPEG file produced by a digital camera often has information about the time the photos was taken, and various camera settings.
Examples of command line tools that display (and possibly allow manipulation of) a file's metadata:
- identify (from ImageMagick)
- exiftool (EXIF data from images)
- + ffmpeg (by default displays information about a movie such as frame rate, duration, image size)
BashAndGrep
#!python numbers=off
#
import os
def bashandgrep (cmdtodo, patterns):
"""
executes the bash command CMDTODO
: and scans its output for patterns,
if they match, captures (groups /
: parentheses in the pattern) are stored
and returned in a new dictionary with
: names matching the keys of the patterns dict.
"""
ret = {}
(child_stdin, child_stdout) = os.popen4(cmdtodo)
output = child_stdout.read()
for (name, pattern) in patterns.items():
# print "scanning for %s" % name
m = pattern.search(output)
if m:
caps = m.groups()
if (len(caps) == 1):
ret[name] = caps[0]
else:
ret[name] = caps
return ret
Example using ffmpeg to grab some moviedata:
#!python numbers=off
from bashandgrep import *
import re
patterns = {
'fps': re.compile(r"([0-9.]*) fps"),
'size' : re.compile(r"([0-9]+)x([0-9]+)"),
'duration' : re.compile(r"Duration: ([0-9]+):([0-9]+):([0-9]+).([0-9]+)")
}
command = "ffmpeg -i ~/movies/anniehall.avi"
data = bashandgrep(command, patterns)
print data
#!python numbers=off def sqlinsert (dbconn, tablename, data): sql = "INSERT INTO " + tablename sql += " SET " assignpairs = [ x+"=%s" for x in data.keys() ] sql += ", ".join(assignpairs) # DEBUG # print "sqlinsert debug (dbconn is None)" # vlist = ["'"+str(x)+"'" for x in data.values()] # print "\t", sql % tuple(vlist) if dbconn: cursor = dbconn.cursor() cursor.execute(sql, data.values()) cursor.close()
Resources
Python process commands BR http://docs.python.org/lib/os-newstreams.html#os-newstreams

