check against tag format type instead of file extension

sorta like, "if tag_format == 'id3'" rather than "if song_format == ['mp3', 'wav', 'aiff']"
This commit is contained in:
Lynne Megido 2020-10-28 22:32:20 +10:00
parent 3e0eb75999
commit d27e1481c3
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90
2 changed files with 42 additions and 30 deletions

View file

@ -104,7 +104,7 @@
<workItem from="1602850978698" duration="7902000" />
<workItem from="1602908398925" duration="34104000" />
<workItem from="1603714609431" duration="5637000" />
<workItem from="1603720261881" duration="4847000" />
<workItem from="1603720261881" duration="6236000" />
</task>
<task id="LOCAL-00001" summary="mp3 support! more helpful interface! better code! yahoo!!">
<created>1602927759343</created>
@ -176,7 +176,14 @@
<option name="project" value="LOCAL" />
<updated>1603723480563</updated>
</task>
<option name="localTasksCounter" value="11" />
<task id="LOCAL-00011" summary="remove unneeded file extension">
<created>1603814270091</created>
<option name="number" value="00011" />
<option name="presentableId" value="LOCAL-00011" />
<option name="project" value="LOCAL" />
<updated>1603814270092</updated>
</task>
<option name="localTasksCounter" value="12" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -205,7 +212,8 @@
<MESSAGE value="my py dot ini" />
<MESSAGE value="added project files, aiff support" />
<MESSAGE value="turns out i didn't need to do anything to add alac support - they work the same as aac m4a files do. although i did find and fix a bug in the m4a handling so that's good at least 0uo" />
<option name="LAST_COMMIT_MESSAGE" value="turns out i didn't need to do anything to add alac support - they work the same as aac m4a files do. although i did find and fix a bug in the m4a handling so that's good at least 0uo" />
<MESSAGE value="remove unneeded file extension" />
<option name="LAST_COMMIT_MESSAGE" value="remove unneeded file extension" />
</component>
<component name="WindowStateProjectService">
<state x="555" y="188" width="800" height="672" key="#Deployment" timestamp="1602927147820">

58
bcao.py
View file

@ -39,15 +39,6 @@ MutagenTags = Union[mutagen.id3.ID3Tags, mutagen.mp4.Tags, mutagen.oggvorbis.Ogg
args: argparse.Namespace
tmp_dir: tempfile.TemporaryDirectory # type: ignore
class SongInfo:
tag_lookup: Dict[str, Dict[str, str]] = {
"track": {"id3": "TRCK", "m4a": "trkn", "vorbis": "tracknumber"},
"artist": {"id3": "TPE1", "m4a": "©ART", "vorbis": "artist"},
"title": {"id3": "TIT2", "m4a": "©nam", "vorbis": "title"},
"album": {"id3": "TALB", "m4a": "©alb", "vorbis": "album"},
"album_artist": {"id3": "TPE2", "m4a": "aART", "vorbis": "albumartist"}
}
format_lookup: Dict[str, str] = {
"mp3": "id3",
"m4a": "m4a",
@ -57,6 +48,15 @@ class SongInfo:
"aiff": "id3"
}
class SongInfo:
tag_lookup: Dict[str, Dict[str, str]] = {
"track": {"id3": "TRCK", "m4a": "trkn", "vorbis": "tracknumber"},
"artist": {"id3": "TPE1", "m4a": "©ART", "vorbis": "artist"},
"title": {"id3": "TIT2", "m4a": "©nam", "vorbis": "title"},
"album": {"id3": "TALB", "m4a": "©alb", "vorbis": "album"},
"album_artist": {"id3": "TPE2", "m4a": "aART", "vorbis": "albumartist"}
}
def __init__(self, file_name: Path):
self.m_file: MutagenFile = mutagen.File(file_name)
self.m_tags: MutagenTags = self.m_file.tags
@ -65,6 +65,9 @@ class SongInfo:
self.format = path.splitext(file_name)[1][1:]
self.fallback = False
if self.format not in format_lookup:
raise ValueError(f"Unsupported file type: {self.format}")
fallbacks = re.match(
r"^(?P<artist>.+) - (?P<album>.+) - (?P<track>\d{2,}) (?P<title>.+)\.(?:ogg|flac|aiff|wav|mp3|m4a)$",
self.file_name
@ -95,14 +98,14 @@ class SongInfo:
# write fallback tags to file
for standard_name, tag_set in self.tag_lookup.items():
tag = tag_set[self.format_lookup[self.format]]
tag = tag_set[format_lookup[self.format]]
self.m_tags[tag] = self.new_id3_tag(standard_name, self.tags[standard_name])
self.m_file.save()
else:
for standard_name, tag_set in self.tag_lookup.items():
tag = tag_set[self.format_lookup[self.format]]
tag = tag_set[format_lookup[self.format]]
if tag not in self.m_tags:
print(f"{tag} not in self.m_tags")
@ -150,34 +153,35 @@ class SongInfo:
return f"{self.tags['track'].zfill(zeroes)} {self.tags['title']}.{self.format}"
def has_cover(self) -> bool:
if self.format == "ogg":
return "metadata_block_picture" in self.m_tags and len(self.m_tags["metadata_block_picture"]) != 0
if self.format == "flac":
# needs to be handled separately from ogg, as it doesn't use the vorbis tags for cover art for whatever reason
return len(self.m_file.pictures) != 0
if self.format in ["mp3", "wav", "aiff"]:
if format_lookup[self.format] == "vorbis":
return "metadata_block_picture" in self.m_tags and len(self.m_tags["metadata_block_picture"]) != 0
if format_lookup[self.format] == "id3":
apics: List[APIC] = self.m_tags.getall("APIC")
for apic in apics:
if apic.type == PictureType.COVER_FRONT:
return True
return False
if self.format == "m4a":
if format_lookup[self.format] == "m4a":
return 'covr' in self.m_tags and len(self.m_tags['covr']) != 0
raise NotImplementedError("Song format not yet implemented.")
def set_cover(self, to_embed: Union[Picture, APIC, MP4Cover]) -> None:
# embed cover art
if self.format == "ogg":
self.m_tags["metadata_block_picture"] = [b64encode(to_embed.write()).decode("ascii")]
elif self.format == "flac":
if self.format == "flac":
self.m_file.clear_pictures()
self.m_file.add_picture(to_embed)
elif self.format in ["mp3", "wav", "aiff"]:
elif format_lookup[self.format] == "vorbis":
self.m_tags["metadata_block_picture"] = [b64encode(to_embed.write()).decode("ascii")]
elif format_lookup[self.format] == "id3":
self.m_tags.add(to_embed)
elif self.format == "m4a":
elif format_lookup[self.format] == "m4a":
self.m_tags['covr'] = [to_embed]
self.m_file.save()
@ -295,17 +299,17 @@ def main() -> None:
# it's really strange that the more annoying the file's metadata is, the *less* annoying it is to create cover art
# for it in mutagen.
# vorbis: open standard, so easy to use that mutagen supplies a bunch of "easy" wrappers around other formats to
# make them work more like mutagen.
# vorbis: open standard, so easy to use that mutagen supplies a bunch of "easy" wrappers around other tag formats to
# make them work more like vorbis comments.
# cover-annoy-o-meter: high. mutagen requires you to specify the width, height, colour depth, etc etc
# id3: well documented, but rather cryptic (which is more understandable, "album_artist" or "TPE2").
# id3: well documented, but rather cryptic (which is more understandable, "album_artist" or "TPE2"?).
# cover-annoy-o-meter: not bad at all - at least you get a constructor this time - although it is kinda annoying
# that you have to specify the file encoding, and how you need both a type and a desc.
# m4a: scarce documentation, closed format, half reverse engineered from whatever itunes is doing, exists pretty
# much exclusively in the realm of apple stuff.
# cover-annoy-o-meter: all you need is the file data and the format type.
if song_format in ["ogg", "flac"]:
if format_lookup[song_format] == "vorbis":
# i hate this
with Image.open(io.BytesIO(data)) as image:
embed_cover = Picture()
@ -316,7 +320,7 @@ def main() -> None:
embed_cover.height = image.size[1]
embed_cover.depth = image.bits
elif song_format in ["mp3", "wav", "aiff"]:
elif format_lookup[song_format] == "id3":
# apparently APIC files get compressed on save if they are "large":
# https://mutagen.readthedocs.io/en/latest/api/id3_frames.html#mutagen.id3.APIC
# i don't know what that means (lossless text compression? automatic JPEG conversion?) and i don't know if or how
@ -341,7 +345,7 @@ def main() -> None:
data=data
)
elif song_format == "m4a":
elif format_lookup[song_format] == "m4a":
embed_cover = MP4Cover(
data=data,
imageformat=MP4Cover.FORMAT_JPEG