Skip to content

Instantly share code, notes, and snippets.

@simon-weber
Created February 15, 2013 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simon-weber/4960475 to your computer and use it in GitHub Desktop.
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
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