Last active
May 18, 2017 15:35
-
-
Save fritschy/144adb01e763880f52c623e7f97ba863 to your computer and use it in GitHub Desktop.
Recode a number of FLACs to mp3, python, multiprocess, needs mpv, flac, metaflac and lame
This file contains hidden or 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
#!/usr/bin/env python3 | |
# coding=UTF-8 | |
from os import system, cpu_count, getpid | |
from os.path import isfile, split, splitext, join | |
from sys import argv, exit, stderr | |
from re import sub | |
from subprocess import call | |
from multiprocessing import Pool | |
from traceback import print_exc | |
def recode_(f): | |
assert isfile(f) | |
fh, ft = split(f) | |
r=splitext(ft)[0] | |
rout = join(fh, sub("[<>+#'´`\"%$!=~;|^°(),*:\\\[\]\{\}]", "_", r)) | |
t = rout + ".tags" | |
w = rout + ".wav" | |
mp3 = rout + ".mp3" | |
pid = getpid() | |
def call_(argv): | |
print("%5d> %s" % (pid, " ".join(argv))) | |
return call(argv) | |
def C(*argv): | |
a = [] | |
for i in argv: | |
if type(i) == type([]): | |
a += i | |
else: | |
a.append(i) | |
return call_(a) | |
if not isfile(mp3): | |
assert C("metaflac", "--export-tags-to=" + t, f) == 0 | |
assert C("mpv", "--quiet", "--no-terminal", "--no-video", "--ao", "pcm", "--ao-pcm-file=" + w, f) == 0 | |
comments={} | |
with open(t) as tags: | |
for l in tags: | |
if not l.startswith("REPLAYGAIN"): | |
k, v = l.strip().split('=', 2) | |
comments[k] = v | |
def get(n, x): | |
return comments.get(n, comments.get(n.lower(), x)) | |
if C("lame", "--quiet", "-q", "0", "-v", "-V", "0", # "--strictly-enforce-ISO", | |
"--tt", get("TITLE", "notitle"), | |
"--ta", get("ARTIST", "noartist"), | |
"--tl", get("ALBUM", "noalbum"), | |
"--tn", get("TRACKNUMBER", "0"), | |
w, mp3) != 0: | |
return | |
C("rm", "-f", t, w) | |
def recode(f): | |
try: | |
recode_(f) | |
except: | |
print_exc() | |
def main(): | |
with Pool(cpu_count() * 3 // 2) as p: | |
p.map(recode, argv[1:]) | |
__name__ == '__main__' and main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment