• [^] # Mon script en python pour ceux qui veulent

    Posté par (site web personnel) . En réponse au journal Des "basheries". Évalué à 2.

    J'utilise la librairie mutagen pour récupérer les tags du fichier flac, ainsi que l'encodeur lame. Le reste c'est du python3 de base.
    Il s'utilise comme suit:

    ./flac2mp3.py source_dir destination_dir

    Le script parcours récursivement l'ensemble du répertoire source_dir, convertit les fichiers flac trouvés en mp3 en reprenant les tags et les stocke dans le dossier destination_dir en reproduisant l'arborescence du dossier d'origine.

    #!/usr/bin/env python3
    from os.path import isdir, isfile, splitext, join, normpath
    from os import walk, mknod, makedirs
    import argparse
    import subprocess
    from mutagen.flac import FLAC
    SUPPORTED_FILES = ["flac"]
    LAME_OPTS = "-b 128 --resample 44.1"
    def main():
     args = parser()
     path = args.directory[0]
     path = normpath(path) + "/"
     dest = args.destination[0]
     dest = normpath(dest) + "/"
     if not isdir(path):
     return False
     for root, dirs, files in walk(path):
     if files is None:
     continue
     for file in files:
     filename, extension = splitext(file)
     extension = extension[1:]
     if extension not in SUPPORTED_FILES:
     continue
     origfile = join(root, file)
     destpath = root.replace(path, dest, 1)
     destfile = join(destpath, ".".join((filename, "mp3")))
     if not isdir(destpath):
     makedirs(destpath)
     if isfile(destfile):
     continue
     convert(origfile, destfile)
    def parser():
     parser = argparse.ArgumentParser(description="FLAC to MP3 batch converter.")
     parser.add_argument("directory",
     nargs=1,
     help="Directory containing FLAC files to convert.")
     parser.add_argument("destination",
     nargs=1,
     help="Target directory to store MP3 files to.")
     return parser.parse_args()
    def convert(src, dest):
     tag = FLAC(src)
     title = ""
     index = ""
     year = ""
     artist = ""
     album = ""
     if tag.get('title') is not None:
     title = tag.get('title')[0]
     title = title.replace('"', '\'')
     if tag.get('tracknumber') is not None:
     index = tag.get('tracknumber')[0]
     if tag.get('date') is not None:
     year = tag.get('date')[0]
     if tag.get('artist') is not None:
     artist = tag.get('artist')[0]
     artist = artist.replace('"', '\'')
     if tag.get('album') is not None:
     album = tag.get('album')[0]
     album = album.replace('"', '\'')
     cmd = "flac -dc \"{flac}\" | lame {opts} \
     --tt \"{title}\"\
     --tn \"{index}\"\
     --ty \"{year}\"\
     --ta \"{artist}\"\
     --tl \"{album}\"\
     --add-id3v2 - \"{mp3}\"".format(flac=src,
     mp3=dest,
     opts=LAME_OPTS,
     title=title,
     index=index,
     year=year,
     artist=artist,
     album=album)
     try:
     subprocess.check_call(cmd, shell=True)
     except subprocess.CalledProcessError as e:
     print(e)
    if __name__ == "__main__":
     main()

    There is no spoon...