Created
January 6, 2011 22:47
-
-
Save kmr/768759 to your computer and use it in GitHub Desktop.
Plex MP4 media file scanner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re, os, os.path | |
import tempfile | |
import pickle | |
import Media, VideoFiles, Stack | |
import dircache | |
import commands | |
import UnicodeHelper | |
import hashlib | |
from mp4file import mp4file, atomsearch | |
mp4Info='/opt/local/bin/mp4info' | |
nameRegex='^ Name: ' | |
artistRegex='^ Artist: ' | |
albumRegex='^ Album: ' | |
trackRegex='^ Track: ' | |
dumpfileName='Mpeg4TagScannerDump.dat' | |
indexfileName='Mpeg4TagScannerIndex.dat' | |
matcher = re.compile('^\.') | |
nameMatcher = re.compile(nameRegex) | |
artistMatcher = re.compile(artistRegex) | |
albumMatcher = re.compile(albumRegex) | |
trackMatcher = re.compile(trackRegex) | |
# get mp4 tags | |
def getTags(file): | |
info = commands.getoutput(mp4Info + ' "' + file + '"') | |
title = artist = album = track = None | |
for line in info.splitlines(): | |
if nameMatcher.match(line): | |
title = re.sub(nameRegex, '', line) | |
elif artistMatcher.match(line): | |
artist = re.sub(artistRegex, '', line) | |
elif albumMatcher.match(line): | |
album = re.sub(albumRegex, '', line) | |
elif trackMatcher.match(line): | |
track = re.search(r'(\d+) of \d+', re.sub(trackRegex, '', line)).group(1) | |
if title == None or title == '': | |
title = os.path.splitext(os.path.basename(file))[0] | |
if artist == None or artist == '': | |
artist = 'その他のアーチスト' | |
if album == None or album == '': | |
album = 'その他の作品' | |
if track == None or track == '': | |
track = 0 | |
return Media.Episode(UnicodeHelper.fixEncoding(album), 0, track, UnicodeHelper.fixEncoding(title), None) | |
# Look for episodes. | |
def Scan(path, files, mediaList, subdirs): | |
# Scan for video files. | |
VideoFiles.Scan(path, files, mediaList, subdirs) | |
list = dict() | |
dumpfile = tempfile.gettempdir() + '/' + dumpfileName | |
if os.path.exists(dumpfile): | |
fi = None | |
try: | |
fi = open(dumpfile) | |
list = pickle.load(fi) | |
finally: | |
if (fi): | |
fi.close() | |
indexlist = dict() | |
indexfile = tempfile.gettempdir() + '/' + indexfileName | |
if os.path.exists(indexfile): | |
fix = None | |
try: | |
fix = open(indexfile) | |
indexlist = pickle.load(fix) | |
finally: | |
if (fix): | |
fix.close() | |
change = False | |
for file in files: | |
if not matcher.match(file): | |
key = hashlib.md5(file).hexdigest() | |
size = os.path.getsize(file) | |
if list.has_key(key) and list[key][0] == size: | |
media = list[key][1] | |
else: | |
change = True | |
media = getTags(file) | |
if media.episode == 0: | |
index = 0 | |
if indexlist.has_key(media.show): | |
index = indexlist[media.show] + 1 | |
else: | |
index = 101 | |
media.episode = index | |
indexlist[media.show] = index | |
list[key] = [size, media] | |
media.parts.append(file) | |
mediaList.append(media) | |
if (change): | |
fo = None | |
try: | |
fo = open(dumpfile, 'w') | |
pickle.dump(list, fo) | |
finally: | |
if (fo): | |
fo.close() | |
fox = None | |
try: | |
fox = open(indexfile, 'w') | |
pickle.dump(indexlist, fox) | |
finally: | |
if (fox): | |
fox.close() | |
# Stack the results. | |
Stack.Scan(path, files, mediaList, subdirs) | |
def find_data(atom, name): | |
child = atomsearch.find_path(atom, name) | |
data_atom = child.find('data') | |
if data_atom and 'data' in data_atom.attrs: | |
return data_atom.attrs['data'] | |
import sys | |
if __name__ == '__main__': | |
print "Hello, world!" | |
path = sys.argv[1] | |
files = [os.path.join(path, file) for file in os.listdir(path)] | |
media = [] | |
Scan(path[1:], files, media, []) | |
print "Media:", media |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment