我有大量音乐收藏,只想备份其中的标签(这样以后如果丢失了,我可以再次找到它)。它由 .m4a 和 .mp3 文件组成。我该怎么做?
答案1
这实际上取决于您使用的系统,对于便携式解决方案 - 例如http://nedbatchelder.com/code/modules/id3reader.html
答案2
这是我的简单脚本,可以帮我解决这个问题。谢谢您的提示。
# coding=utf-8
"""Backs up tags from all m4a and mp3 files in a directory, recursively
and stores them as JSON file."""
import os
import json
import logging
from mutagen.easyid3 import EasyID3
from mutagen.easymp4 import EasyMP4
def backup_tags(directory, outfile):
"""Back up tags from all m4a & mp3 files from 'directory' into 'outfile'"""
logging.info('Saving started.')
filenames = []
for root, _, files in os.walk(directory):
filenames.extend([os.path.join(root, x) for x in files])
def tags_dict(filename):
"""Return tags from file 'filename' as python dictionary"""
_, extension = os.path.splitext(filename)
if extension == '.mp3':
return dict(EasyID3(filename))
elif extension == '.m4a':
return dict(EasyMP4(filename))
else:
raise Exception('Not supported file type: %s' % filename)
with open(outfile, 'w') as out:
json.dump([tags_dict(x) for x in filenames], out, indent=1)
logging.info('Tags from %d file(s) saved.', len(filenames))
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
backup_tags(u'F:\\Music', 'backup.json')