Eu encontrei um script python escrito por Louis-Philippe Véronneau.
O código atualizado pode ser encontrado em seu repositório git: link
Você precisa instalar o python-docopt (sudo apt-get install python-docopt) e executar este script depois de chmod + x rename-flac.py ele:
#!/usr/bin/python
"""
rename-flac takes the information from FLAC metadata to batch rename
the files according to a filenaming scheme.
Usage:
rename-flac.py [--verbose] <scheme> <directory>
rename-flac.py (-h | --help)
rename-flac.py --version
Arguments:
<scheme> The filenaming scheme. Has to be between quotation marks
<directory> The path to the directory containing the album
Options:
-h --help Shows the help screen
--version Outputs version information
--verbose Runs the program as verbose mode
These are the options you can use to define the filenaming scheme:
%a = Artist | %b = Album | %d = Date
%g = Genre | %n = Tracknumber | %t = Title
"""
import sys
try:
from docopt import docopt # Creating command-line interface
except ImportError:
sys.stderr.write("""
%s is not installed: this program won't run correctly.
To install %s, run: aptitude install %s
""" % ("python-docopt", "python-docopt", "python-docopt"))
import subprocess
import re
import os
TAGS = dict(a='artist', b='album', d='date',
g='genre', n='tracknumber', t='title')
# Defining the function that fetches metadata and formats it
def metadata(filepath):
args = ["--show-tag=%s" % tag for tag in TAGS.values()]
tags = ["%s=" % tag for tag in TAGS.values()]
formatter = dict()
pipe = subprocess.Popen(["metaflac"] + args + [filepath],
stdout=subprocess.PIPE)
output, error = pipe.communicate()
if pipe.returncode:
raise IOError("metaflac failed: %s" % error)
output = output.splitlines()
for tag in tags:
for item in output:
x = re.compile(re.escape(tag), re.IGNORECASE)
if bool(re.match(x, item)) == True:
tag = tag.replace("=", "")
if tag == "tracknumber":
formatter[tag] = x.sub("", item).zfill(2)
else:
formatter[tag] = x.sub("", item)
return formatter
# Defining function that renames the files
def rename(scheme, dirname, filename, args):
filepath = os.path.join(dirname, filename)
new_filename = scheme % metadata(filepath) + ".flac"
if new_filename == filename:
if args["--verbose"] == True:
print("%s is already named correctly") % (filename)
else:
new_filepath = os.path.join(dirname, new_filename)
os.rename(filepath, new_filepath)
if args["--verbose"] == True:
print("%s >> %s") % (filename, new_filename)
# Defining main function
def main():
args = docopt(__doc__, version="rename-flac 1.0")
scheme = args["<scheme>"]
if not re.search("%[tn]", scheme): # To have a unique filename
sys.stderr.write("%t or %n has to be present in <scheme>\n")
return
scheme = re.sub('%%([%s])' % ''.join(TAGS.keys()),
lambda m: '%%(%s)s' % TAGS[m.group(1)],
scheme)
for dirname, _, filenames in os.walk(
args["<directory>"],
topdown=False):
for filename in filenames:
if os.path.splitext(filename)[1] == ".flac":
try:
rename(scheme, dirname, filename, args)
except KeyboardInterrupt:
raise
except OSError:
print("Song title contains /. Please rename it")
# Calling main function
if __name__ == "__main__":
main()
Eu testei e tudo funciona como um encanto! Obrigada!