1
0
mirror of synced 2024-11-22 17:24:23 +01:00
mat2/libmat2/audio.py

78 lines
2.3 KiB
Python
Raw Normal View History

import mimetypes
import os
2018-03-25 16:17:41 +02:00
import shutil
import tempfile
2018-10-12 11:58:01 +02:00
from typing import Dict, Union
2018-03-25 16:17:41 +02:00
import mutagen
from . import abstract, parser_factory
2018-03-25 16:17:41 +02:00
2018-03-25 16:17:41 +02:00
class MutagenParser(abstract.AbstractParser):
def __init__(self, filename):
super().__init__(filename)
try:
mutagen.File(self.filename)
except mutagen.MutagenError:
raise ValueError
2018-10-12 11:58:01 +02:00
def get_meta(self) -> Dict[str, Union[str, dict]]:
2018-03-25 16:17:41 +02:00
f = mutagen.File(self.filename)
if f.tags:
2018-05-16 22:36:59 +02:00
return {k:', '.join(v) for k, v in f.tags.items()}
2018-03-25 16:17:41 +02:00
return {}
2018-10-12 11:58:01 +02:00
def remove_all(self) -> bool:
2018-03-25 16:17:41 +02:00
shutil.copy(self.filename, self.output_filename)
f = mutagen.File(self.output_filename)
f.delete()
f.save()
return True
2018-03-25 16:17:41 +02:00
class MP3Parser(MutagenParser):
mimetypes = {'audio/mpeg', }
2018-10-12 11:58:01 +02:00
def get_meta(self) -> Dict[str, Union[str, dict]]:
metadata = {} # type: Dict[str, Union[str, dict]]
meta = mutagen.File(self.filename).tags
2018-03-25 16:31:21 +02:00
for key in meta:
metadata[key.rstrip(' \t\r\n\0')] = ', '.join(map(str, meta[key].text))
2018-03-25 16:17:41 +02:00
return metadata
2018-03-25 16:17:41 +02:00
class OGGParser(MutagenParser):
mimetypes = {'audio/ogg', }
2018-03-25 16:20:45 +02:00
2018-03-25 16:20:45 +02:00
class FLACParser(MutagenParser):
mimetypes = {'audio/flac', 'audio/x-flac'}
2018-10-11 18:15:11 +02:00
2018-10-12 11:58:01 +02:00
def remove_all(self) -> bool:
2018-10-11 18:15:11 +02:00
shutil.copy(self.filename, self.output_filename)
f = mutagen.File(self.output_filename)
f.clear_pictures()
f.delete()
f.save(deleteid3=True)
return True
2018-10-12 11:58:01 +02:00
def get_meta(self) -> Dict[str, Union[str, dict]]:
2018-10-11 18:15:11 +02:00
meta = super().get_meta()
for num, picture in enumerate(mutagen.File(self.filename).pictures):
name = picture.desc if picture.desc else 'Cover %d' % num
2018-10-12 11:58:01 +02:00
extension = mimetypes.guess_extension(picture.mime)
if extension is None: # pragma: no cover
meta[name] = 'harmful data'
continue
_, fname = tempfile.mkstemp()
2018-10-12 11:58:01 +02:00
fname = fname + extension
with open(fname, 'wb') as f:
f.write(picture.data)
2018-10-12 11:58:01 +02:00
p, _ = parser_factory.get_parser(fname) # type: ignore
# Mypy chokes on ternaries :/
meta[name] = p.get_meta() if p else 'harmful data' # type: ignore
os.remove(fname)
2018-10-11 18:15:11 +02:00
return meta