Created
February 15, 2013 13:47
-
-
Save simon-weber/4960475 to your computer and use it in GitHub Desktop.
old gmusicapi code to do a generic metadata copy between audio files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import mutagen | |
log = logging.getLogger(__name__) | |
def copy_md_tags(from_fname, to_fname): | |
"""Copy all metadata from *from_fname* to *to_fname* and write. | |
Return True on success, False if not all keys were copied/saved.""" | |
from_tags = mutagen.File(from_fname, easy=True) | |
to_tags = mutagen.File(to_fname, easy=True) | |
if from_tags is None or to_tags is None: | |
log.debug("couldn't find an appropriate handler for tag files: '%s' '%s'", | |
from_fname, to_fname) | |
return False | |
success = True | |
for k, v in from_tags.iteritems(): | |
try: | |
#Some tags don't store values in strings, but in special container types. | |
#Those should be converted to strings so we can safely save them. | |
#Also, the value might be a list of tags or a single tag. | |
if not isinstance(v, basestring): | |
safe = [str(e) for e in v] | |
else: | |
safe = str(e) | |
to_tags[k] = safe | |
except mutagen.easyid3.EasyID3KeyError: | |
#Raised because we're copying in an unsupported in easy-mode key. | |
log.debug("skipping non easy key", exc_info=True) | |
except: | |
#lots of things can go wrong, just skip the key | |
log.warning("problem when copying keys from '%s' to '%s'", | |
from_fname, to_fname, exc_info=True) | |
success = False | |
try: | |
to_tags.save() | |
except: | |
log.warning("could not save tag file %s", to_fname, exc_info=True) | |
success = False | |
return success |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment