#!/usr/bin/python # # mp34t - Massage large sets of mp3 files into sane sizes/quantities # (name derived from 'MP3 for Tevion' - my player's brand) # # Copyright (C) 2007 David Symons # # 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 2 # 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, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # # Dependencies: python, v2strip, mpgtx import os import sys version = '0.1' usage = '\nUsage: mp34t\n(no parameters required)\n' if len(sys.argv) > 1: if sys.argv[1] == "--help": print usage print """Processes large sets of MP3 files that are part of a single recording (eg. an Audio Book). It makes sure there are a sane number of files by merging them if required and also strips any tags and (re)names the files so that players that play in filename order (like mine) will do so. It is designed to be run in the parent directory of a set of directories each containing a CDs worth of files ripped to MP3s. """ elif sys.argv[1] == "--version": print """ Version: %s Copyright: 2007 David Symons Licence: GPL (see the file COPYING included in the archive) """ % version else: print usage sys.exit() # Prefix to be put at the start of each filename prefix = raw_input('File Prefix (eg. h-2-): ') fileseq = 1 # Get the subdirectories into a sorted list root, subdirs, filenames = os.walk( os.getcwd() ).next() subdirs.sort() for dir in subdirs: filelist = os.listdir( dir ) filelist.sort() filecount = len( filelist ) os.chdir( dir ) print "-------- In subdirectory %s --------" % dir # If there are more than 15 files we will do some merging. # Otherwise just rename if filecount > 15: i=0 while i < filecount: newfile = '%s%04d.mp3' % ( prefix, fileseq ) # Using mpgjoin even though it seems to give a lot of nasty messages. # I guess it's really made for video files, but it's much faster at # this than some of the other utilities tried. cmd = 'mpgjoin -o %s' % newfile fileseq+=1 for j in range( filecount/8 ): if i < filecount: cmd += ' "%s"' % filelist[ i ] i += 1 os.system( cmd ) os.system( 'mv %s ..' % newfile ) else: # Rename only if there is only a small number of files. for filename in filelist: newfile = '%s%04d.mp3' % ( prefix, fileseq ) fileseq+=1 os.copy( filename, newfile ) os.system( 'mv %s ..' % newfile ) os.chdir( '..' ) # Strip the mp3 tags (if applicable) because they can compromise the file # ordering on some players. os.system( 'v2strip *.mp3' )