Created
September 30, 2021 04:42
-
-
Save WhiteBoardDev/646d866ff142508f6a646b8efc8422a0 to your computer and use it in GitHub Desktop.
Music Conversion
This file contains 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
import os | |
from multiprocessing import Pool | |
rootDir = '/starting_dir' | |
output = rootDir + '/downsided' | |
fileTypes = ['wav','mp3', 'flac'] | |
if not os.path.exists(output): | |
os.mkdir(output) | |
def doConvert(input, output): | |
os.system("ffmpeg -i \"%s\" -codec:a libmp3lame -b:a 128k \"%s\"" % (input, output)) | |
def downsize(parentPath, pool, invocations): | |
entries = os.scandir(parentPath) | |
for entry in entries: | |
if entry.is_dir(): | |
downsize(entry.path, pool, invocations) | |
else: | |
fileParts = entry.name.split('.') | |
extension = fileParts[-1] | |
if extension in fileTypes and not fileParts[-2] == 'convert': | |
destinationFolder = "%s/%s/" % (output, entry.path.split('/')[-2]) | |
if not os.path.exists(destinationFolder): | |
os.mkdir(destinationFolder) | |
destination = "%s/%s.convert.mp3" %(destinationFolder, entry.name) | |
# infinite loop | |
if not os.path.exists(destination): | |
invocations.append(pool.apply_async(doConvert, (entry.path, destination))) | |
if __name__ == '__main__': | |
with Pool(processes=4) as pool: | |
invocations = [] | |
downsize(rootDir, pool, invocations) | |
[invocation.wait() for invocation in invocations] | |
print("done!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment