From 56d5726a6a07d97213975173e4ab5adf8da8ec7c Mon Sep 17 00:00:00 2001 From: yomguy Date: Fri, 1 May 2009 18:38:21 +0000 Subject: [PATCH] add windows version git-svn-id: http://svn.parisson.org/svn/telemaster/trunk@15 353fd7da-fb10-4236-9bec-1a49139083f2 --- flac2mp3mix_win.py | 135 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 flac2mp3mix_win.py diff --git a/flac2mp3mix_win.py b/flac2mp3mix_win.py new file mode 100644 index 0000000..a6060cd --- /dev/null +++ b/flac2mp3mix_win.py @@ -0,0 +1,135 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# +# Copyright Guillaume Pellerin (2006-2009) + +# + +# This software is a computer program whose purpose is to convert flac files +# into mixed MP3 file, managing metadata. + +# This software is governed by the CeCILL license under French law and +# abiding by the rules of distribution of free software. You can use, +# modify and/ or redistribute the software under the terms of the CeCILL +# license as circulated by CEA, CNRS and INRIA at the following URL +# "http://www.cecill.info". + +# As a counterpart to the access to the source code and rights to copy, +# modify and redistribute granted by the license, users are provided only +# with a limited warranty and the software's author, the holder of the +# economic rights, and the successive licensors have only limited +# liability. + +# In this respect, the user's attention is drawn to the risks associated +# with loading, using, modifying and/or developing or reproducing the +# software by the user in light of its specific status of free software, +# that may mean that it is complicated to manipulate, and that also +# therefore means that it is reserved for developers and experienced +# professionals having in-depth computer knowledge. Users are therefore +# encouraged to load and test the software's suitability as regards their +# requirements in conditions enabling the security of their systems and/or +# data to be ensured and, more generally, to use and operate it in the +# same conditions as regards security. + +# The fact that you are presently reading this means that you have had +# knowledge of the CeCILL license and that you accept its terms. + +# Author: Guillaume Pellerin + +import os +import sys +import mutagen +import subprocess +import Queue +from tools import * + + +class SubProcessPipe: + + def __init__(self, command, stdin=None): + """Read media and stream data through a generator. + Taken from Telemeta (see http://telemeta.org)""" + + self.buffer_size = 32768 + if not stdin: + stdin = subprocess.PIPE + + self.proc = subprocess.Popen(command, + shell = True, + bufsize = self.buffer_size, + stdin = stdin, + stdout = subprocess.PIPE, + close_fds = False) + + self.stdin = self.proc.stdin + self.stdout = self.proc.stdout + +class Flac2Mp3Mix: + + def __init__(self, argv): + self.root_dir = argv[1] + self.dest_dir = argv[2] + self.sox = os.sep.join(['D:','sox-14.2.0','sox.exe']) + self.lame = os.sep.join(['D:','lame3.98.2','lame.exe']) + + def flac_player(self, media_list, stdin): + files = '' + for media in media_list: + files += '"' + media + '" ' + command = self.sox + ' -t flac %s -q -b 16 -r 44100 -t wav - ' % files + return SubProcessPipe(command, stdin) + + + def mp3_encode(self, stdin, file): + command = self.lame + ' -b 256 -h --tt "xxx" - "' + file + '"' + return SubProcessPipe(command, stdin) + + def get_media_lists(self): + full_media_list = [] + for root, dirs, files in os.walk(self.root_dir): + media_list = [] + if root: + for file in files: + ext = file.split('.')[-1] + if ext == 'oga' or ext == 'flac': + media_list.append(root+os.sep+file) + full_media_list.append(media_list) + return full_media_list + + def main(self): + media_lists = self.get_media_lists() + for media_list in media_lists: + if media_list: + media_list.sort() + #root_dir = os.sep.join(media_list[0].split(os.sep)[:-1]) + media_0 = media_list[0] + print 'Mixing : ' + print media_list + flac = Flac(media_0) + metadata = flac.get_tags() + album_name = metadata['album'] + mp3_file = self.dest_dir + os.sep + album_name + '.mp3' + if not os.path.exists(mp3_file): + f = self.flac_player(media_list, None) + m = self.mp3_encode(f.stdout, mp3_file) + print 'Encoding ' + mp3_file + m.proc.wait() + print 'Writing metadata...' + mp3 = Mp3(mp3_file) + mp3.metadata = metadata + mp3.metadata['title'] = album_name + mp3.metadata['comment'] = 'Cellar_playlist' + mp3.write_tags() + print 'Done !' + else: + print 'File already exits !' + print mp3_file + + +if __name__ == '__main__': + if len(sys.argv) <= 2: + print "Usage : flac2mp3mix.py FLAC_DIR MIX_DIR" + else: + f = Flac2Mp3Mix(sys.argv) + f.main() + -- 2.39.5