mirror of
https://github.com/fergalmoran/picard.git
synced 2026-02-19 06:03:59 +00:00
The MP3 plugin is now almost complete.
This commit is contained in:
@@ -62,7 +62,19 @@ class File(QtCore.QObject):
|
||||
id = property(getId)
|
||||
|
||||
def save(self):
|
||||
raise NotImplementedError()
|
||||
"""Save the file."""
|
||||
locker = QtCore.QMutexLocker(self.mutex)
|
||||
try:
|
||||
self._save()
|
||||
except Exception, e:
|
||||
raise
|
||||
else:
|
||||
self.orig_metadata.copy(self.metadata)
|
||||
self.metadata.changed = False
|
||||
|
||||
def _save(self):
|
||||
"""Save metadata to the file."""
|
||||
raise NotImplementedError
|
||||
|
||||
def remove_from_cluster(self):
|
||||
locker = QtCore.QMutexLocker(self.mutex)
|
||||
|
||||
@@ -20,13 +20,19 @@
|
||||
from mutagen import id3
|
||||
from picard.plugins.picardmutagen.mutagenext import compatid3
|
||||
|
||||
|
||||
def read_id3_tags(tags, metadata):
|
||||
"""Read tags from an ID3 object to Picard's metadata."""
|
||||
|
||||
|
||||
def read_text_frame(frame_id, name):
|
||||
if frame_id in tags:
|
||||
metadata[name] = unicode(tags[frame_id])
|
||||
|
||||
def read_free_text_frame(desc, name):
|
||||
frames = tags.getall("TXXX:%s" % desc)
|
||||
if frames:
|
||||
metadata[name] = unicode(frames[0])
|
||||
|
||||
read_text_frame("TPE1", "artist")
|
||||
read_text_frame("TPE2", "ensemble")
|
||||
read_text_frame("TPE3", "conductor")
|
||||
@@ -46,15 +52,35 @@ def read_id3_tags(tags, metadata):
|
||||
else:
|
||||
metadata["tracknumber"] = text
|
||||
|
||||
def write_id3_tags(tags, metadata):
|
||||
if "TPOS" in tags:
|
||||
text = unicode(tags["TPOS"])
|
||||
if "/" in text:
|
||||
disc, total = text.split("/")
|
||||
metadata["discnumber"] = disc
|
||||
metadata["totaldiscs"] = total
|
||||
else:
|
||||
metadata["discnumber"] = text
|
||||
|
||||
frames = tags.getall("UFID:http://musicbrainz.org")
|
||||
if frames:
|
||||
metadata["musicbrainz_trackid"] = unicode(frames[0])
|
||||
read_free_text_frame("MusicBrainz Artist Id", "musicbrainz_trackid")
|
||||
read_free_text_frame("MusicBrainz Album Id", "musicbrainz_albumid")
|
||||
read_free_text_frame("MusicBrainz Album Artist Id",
|
||||
"musicbrainz_albumartistid")
|
||||
|
||||
read_free_text_frame("ALBUMARTIST", "albumartist")
|
||||
|
||||
|
||||
def write_id3_tags(tags, metadata, encoding, v23=False):
|
||||
"""Write tags from Picard's metadata to an ID3 object."""
|
||||
|
||||
encoding = 3
|
||||
|
||||
def add_text_frame(frame_class, name):
|
||||
print name, name in metadata, metadata[name]
|
||||
if name in metadata:
|
||||
tags.add(frame_class(encoding=encoding,
|
||||
text=metadata[name]))
|
||||
print tags.pprint()
|
||||
|
||||
def add_free_text_frame(desc, name):
|
||||
if name in metadata:
|
||||
@@ -101,3 +127,4 @@ def write_id3_tags(tags, metadata):
|
||||
"musicbrainz_albumartistid")
|
||||
|
||||
add_free_text_frame("ALBUMARTIST", "albumartist")
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
"""Mutagen-based MP3 metadata reader."""
|
||||
"""Mutagen-based MP3 metadata reader/tagger."""
|
||||
|
||||
from picard.file import File
|
||||
from picard.util import encode_filename
|
||||
@@ -29,7 +29,7 @@ from picard.plugins.picardmutagen._id3 import read_id3_tags, write_id3_tags
|
||||
class MutagenMP3File(File):
|
||||
|
||||
def read(self):
|
||||
|
||||
|
||||
mp3file = MP3(encode_filename(self.filename), ID3=CompatID3)
|
||||
read_id3_tags(mp3file.tags, self.orig_metadata)
|
||||
|
||||
@@ -39,11 +39,32 @@ class MutagenMP3File(File):
|
||||
|
||||
self.metadata.copy(self.orig_metadata)
|
||||
|
||||
def save(self):
|
||||
def _save(self):
|
||||
"""Save ID3 tags to the file."""
|
||||
|
||||
id3file = CompatID3(encode_filename(self.filename), translate=False)
|
||||
write_id3_tags(id3file, self.metadata)
|
||||
id3file.update_to_v23()
|
||||
id3file.save(v2=3)
|
||||
|
||||
tags = CompatID3(encode_filename(self.filename), translate=False)
|
||||
|
||||
if self.config.setting.clear_existing_tags:
|
||||
tags.clear()
|
||||
|
||||
if self.config.setting.write_id3v1:
|
||||
v1 = 2
|
||||
else:
|
||||
v1 = 0
|
||||
|
||||
if self.config.setting.id3v2_encoding == "utf-8":
|
||||
encoding = 3
|
||||
elif self.config.setting.id3v2_encoding == "utf-16":
|
||||
encoding = 1
|
||||
else:
|
||||
encoding = 0
|
||||
|
||||
if self.config.setting.write_id3v23:
|
||||
write_id3_tags(tags, self.metadata, encoding, True)
|
||||
tags.update_to_v23()
|
||||
tags.save(v2=3, v1=v1)
|
||||
else:
|
||||
write_id3_tags(tags, self.metadata, encoding, False)
|
||||
tags.update_to_v24()
|
||||
tags.save(v2=4, v1=v1)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user