rclaudio: let mutagen create the right object type. Extract more fields. Use the setfield() method instead of html meta tags. Needs the recent increase in max field count in mh_execm

This commit is contained in:
Jean-Francois Dockes 2017-02-02 18:05:35 +01:00
parent 148e7eec0d
commit 28bf7ff93c

View File

@ -7,29 +7,11 @@ import os
import rclexecm import rclexecm
try: try:
from mutagen.mp3 import MP3 from mutagen import File
from mutagen.easyid3 import EasyID3
from mutagen.flac import FLAC
from mutagen.oggvorbis import OggVorbis
except: except:
print("RECFILTERROR HELPERNOTFOUND python:mutagen") print("RECFILTERROR HELPERNOTFOUND python:mutagen")
sys.exit(1); sys.exit(1);
# prototype for the html document we're returning
htmltemplate = '''
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="album" content="%s">
<meta name="author" content="%s">
<meta name="title" content="%s">
</head>
<body>
%s
</body>
</html>
'''
# mp3: album, title, artist, genre, date, tracknumber # mp3: album, title, artist, genre, date, tracknumber
# flac: album, title, artist, genre, xxx, tracknumber # flac: album, title, artist, genre, xxx, tracknumber
# oggvorbis:album, title, artist, genre, date, tracknumber # oggvorbis:album, title, artist, genre, date, tracknumber
@ -48,41 +30,49 @@ class AudioTagExtractor:
filename = params["filename:"] filename = params["filename:"]
mimetype = params["mimetype:"] mimetype = params["mimetype:"]
try: try:
if mimetype == b'audio/mpeg': mutf = File(filename, easy=True)
tags = MP3(filename, ID3=EasyID3)
elif mimetype == b'application/ogg' or \
mimetype == b'audio/x-vorbis+ogg':
tags = OggVorbis(filename)
elif mimetype == b'application/x-flac' or \
mimetype == 'audio/x-flac' or \
mimetype == b'audio/flac':
tags = FLAC(filename)
else:
raise Exception("Bad mime type %s" % mimetype)
except Exception as err: except Exception as err:
self.em.rclog("extractone: extract failed: [%s]" % err) self.em.rclog("extractone: extract failed: [%s]" % err)
return (ok, docdata, "", rclexecm.RclExecM.eofnow) return (ok, docdata, "", rclexecm.RclExecM.eofnow)
album = "" minf = {}
artist = "" for prop,dflt in [('sample_rate', 44100), ('channels', 2),
title = "" ('bits_per_sample', 16), ('length', 0),
try: ('bitrate', 0)]:
album = self.em.htmlescape(tags["album"][0]) try:
except: minf[prop] = getattr(mutf.info, prop)
pass except Exception as e:
try: #self.em.rclog("NO %s prop: %s" % (prop, e))
artist = self.em.htmlescape(tags["artist"][0]) minf[prop] = dflt
except:
pass if minf['bitrate'] == 0 and minf['length'] > 0:
try: br = int(os.path.getsize(filename)* 8 / minf['length'])
title = self.em.htmlescape(tags["title"][0]) minf['bitrate'] = str(br)
except:
pass minf['duration'] = minf['length']
self.em.setmimetype("text/html") del minf['length']
alldata = self.em.htmlescape(tags.pprint()) for tag,val in minf.iteritems():
alldata = alldata.replace("\n", "<br>") minf[tag] = str(val)
docdata = (htmltemplate % (album, artist, title, alldata))\
.encode('UTF-8') for tag,val in mutf.iteritems():
try:
val0 = val[0]
if val0:
minf[tag.lower()] = val0
except:
pass
self.em.setmimetype("text/plain")
self.em.setfield("charset", 'utf-8')
for tag,val in minf.iteritems():
#self.em.rclog("%s -> %s" % (tag, val))
self.em.setfield(tag, val)
# Compat with old version
if tag == 'artist':
self.em.setfield('author', val)
docdata = mutf.pprint().encode('utf-8')
ok = True ok = True
return (ok, docdata, "", rclexecm.RclExecM.eofnext) return (ok, docdata, "", rclexecm.RclExecM.eofnext)