Created
July 18, 2014 07:20
-
-
Save kennyballou/7eee124b942f1f7b10a3 to your computer and use it in GitHub Desktop.
A flac to mp3 directory converter
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 python | |
| # flac2mp3 -- FLAC to MP3 file converter | |
| # Copyright (C) 2014 Kenny Ballou | |
| # This program is free software: you can redistribute it and/or modify | |
| # it under the terms of the GNU General Public License as published by | |
| # the Free Software Foundation, either version 3 of the License, or | |
| # (at your option) any later version. | |
| # This program is distributed in the hope that it will be useful, | |
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| # GNU General Public License for more details. | |
| # You should have received a copy of the GNU General Public License | |
| # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| import os | |
| import sys | |
| from os.path import abspath | |
| from os.path import join | |
| import subprocess | |
| import Queue | |
| import threading | |
| def basename(file): | |
| return os.path.splitext(file)[0] | |
| class Converter(threading.Thread): | |
| """Threaded Flac to mp3 converter""" | |
| def __init__(self, queue): | |
| threading.Thread.__init__(self) | |
| self.queue = queue | |
| def run(self): | |
| while True: | |
| toConvert = self.queue.get() | |
| self.convertFile(infile=toConvert[0],outfile=toConvert[1]) | |
| self.queue.task_done() | |
| def convertFile(self, infile="",outfile="",bitcode=320): | |
| tmpFile = basename(infile) + ".wav" | |
| print("Processing file: " + abspath(infile)) | |
| flac = ["flac", "-df", abspath(infile), "-o", abspath(tmpFile)] | |
| with open(os.devnull, "w") as nul: | |
| flacCode = subprocess.call(flac, stdout=nul, stderr=nul) | |
| if flacCode != 0: | |
| raise Exception("Flac command failed") | |
| lame = ["lame", "-b", str(bitcode), abspath(tmpFile), outfile] | |
| with open(os.devnull, "w") as nul: | |
| lameCode = subprocess.call(lame, stdout=nul, stderr=nul) | |
| if lameCode != 0: | |
| raise Exception("Lame command failed") | |
| os.remove(abspath(tmpFile)) | |
| def isFlacFile(test): | |
| if os.path.isfile(test) == False: | |
| return False | |
| fileExt = os.path.splitext(test)[1] | |
| if len(fileExt) == 0 or fileExt != '.flac': | |
| return False | |
| return True | |
| def convertDir(dir="",outdir=""): | |
| cwd = os.getcwd() | |
| dir = abspath(dir) | |
| outdir = abspath(outdir) | |
| if not os.path.exists(outdir): | |
| os.mkdir(outdir) | |
| queue = Queue.Queue() | |
| os.chdir(dir) | |
| ext = ".mp3" | |
| for file in os.listdir(dir): | |
| if isFlacFile(file): | |
| queue.put((abspath(file), | |
| abspath(join(outdir, basename(file) + ext)))) | |
| for i in range(4): | |
| t = Converter(queue) | |
| t.setDaemon(True) | |
| t.start() | |
| os.chdir(cwd) | |
| queue.join() | |
| if __name__ == "__main__": | |
| convertDir(sys.argv[1], sys.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment