Commit b9dbd12e authored by jvoisin's avatar jvoisin

Implement recursive metadata for FLAC files

Since FLAC files can contain covers, it makes sense
to parse their metadata
parent b2e153b6
Pipeline #19562 failed with stages
in 50 seconds
import mimetypes
import os
import shutil
import tempfile
import mutagen
from . import abstract
from . import abstract, parser_factory
class MutagenParser(abstract.AbstractParser):
......@@ -55,6 +58,14 @@ class FLACParser(MutagenParser):
def get_meta(self):
meta = super().get_meta()
if mutagen.File(self.filename).pictures:
meta['Picture'] = 'Cover'
for num, picture in enumerate(mutagen.File(self.filename).pictures):
name = picture.desc if picture.desc else 'Cover %d' % num
_, fname = tempfile.mkstemp()
with open(fname, 'wb') as f:
extension = mimetypes.guess_extension(picture.mime)
shutil.move(fname, fname + extension)
p, _ = parser_factory.get_parser(fname+extension)
meta[name] = p.get_meta() if p else 'harmful data'
os.remove(fname + extension)
return meta
......@@ -65,25 +65,24 @@ def show_meta(filename: str):
if p is None:
print("[-] %s's format (%s) is not supported" % (filename, mtype))
print("[+] Metadata for %s:" % filename)
metadata = p.get_meta().items() # type: dict
__print_meta(filename, p.get_meta())
def __print_meta(metadata: dict):
def __print_meta(filename:str, metadata: dict, depth:int=1):
padding = " " * depth*2
if not metadata:
print(" No metadata found")
print(padding + "No metadata found")
for k, v in metadata:
print("[%s] Metadata for %s:" % ('+'*depth, filename))
for k, v in metadata.items():
if isinstance(v, dict):
try: # FIXME this is ugly.
print(" %s: %s" % (k, v))
except UnicodeEncodeError:
print(" %s: harmful content" % k)
return __print_meta(k, v, depth+1)
try: # FIXME this is ugly.
print(padding + " %s: %s" % (k, v))
except UnicodeEncodeError:
print(padding + " %s: harmful content" % k)
def clean_meta(filename: str, is_lightweight: bool, policy: UnknownMemberPolicy) -> bool:
......@@ -96,7 +96,7 @@ class TestGetMeta(unittest.TestCase):
p = audio.FLACParser('./tests/data/dirty.flac')
meta = p.get_meta()
self.assertEqual(meta['title'], 'I am so')
self.assertEqual(meta['Picture'], 'Cover')
self.assertEqual(meta['Cover 0'], {'Comment': 'Created with GIMP'})
def test_docx(self):
p = office.MSOfficeParser('./tests/data/dirty.docx')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment