Skip to content

Instantly share code, notes, and snippets.

@simon987
Created October 28, 2018 17:30
Show Gist options
  • Save simon987/2a1dd3090a2ad0574c00e171670b1e0d to your computer and use it in GitHub Desktop.
Save simon987/2a1dd3090a2ad0574c00e171670b1e0d to your computer and use it in GitHub Desktop.
Multi-threaded python script to convert all flac files to mp3 V0 while keeping the directory structure
import os
from shutil import copyfile
import pathlib
from multiprocessing import Pool, Value
SRC = "/mnt/Data1/Music"
DST = "/mnt/Data1/Music [V0]"
to_convert = []
counter = Value("d")
def convert(path):
global counter
destination = DST + path[len(SRC):]
destination = os.path.splitext(destination)[0] + ".mp3"
pathlib.Path(os.path.split(destination)[0]).mkdir(parents=True, exist_ok=True)
if not os.path.exists(destination):
os.system("ffmpeg -i \"" + path + "\" -c:a libmp3lame -q:a 0 -y \"" + destination + "\" -loglevel panic")
print("[%.2f%%] - " % (counter.value / len(to_convert) * 100) + destination)
counter.value += 1
def recursive_convert(path):
for item in os.listdir(path):
full_path = path + os.sep + item
if os.path.isdir(full_path):
recursive_convert(full_path)
else:
if os.path.splitext(item)[1] in [".mp3", ".flac", ".m4a", ".ape"]:
to_convert.append(full_path)
recursive_convert(SRC)
print("Converting " + str(len(to_convert)) + " files...")
pool = Pool(processes=4)
pool.map(convert, to_convert)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment