#!/usr/bin/env python # # Script to recursively apply replaygain tags to mp3's with mp3gain # # Copyright (C) 2007, Michael T. Dean # # This file is released under the GPLv2 license. # # File created: June 23, 2007 # # 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 . # ###################################################################### import os mp3g="/usr/bin/mp3gain" def get_directory_list( Path ): dir_list = [] dir_contents = os.listdir( Path ) for file in dir_contents: if os.path.isdir( Path + file ): dir_list.append( file ) return dir_list def get_mp3_list( Path ): mp3_list = [] dir_contents = os.listdir( Path ) for file in dir_contents: if os.path.isfile( Path + file ) and file.endswith( ".mp3" ): mp3_list.append( file ) return mp3_list def get_recursive_dir_list( Path ): rdir_list = [] dir_list = [] dir_list = get_directory_list( Path ) for dir in dir_list: tlist = get_directory_list( Path + dir + "/" ) rdir_list.append( Path + dir + "/" ) for rdir in tlist: rdir_list.append( Path + dir + "/" + rdir + "/" ) rdirs = get_recursive_dir_list( Path + dir + "/" + rdir + "/" ) for de in rdirs: rdir_list.append( de ) return rdir_list def get_recursive_mp3_list( Path ): rmp3_list = [] dirs_list = get_recursive_dir_list( Path ) for dir in dirs_list: mp3s = get_mp3_list( dir ) for mp3 in mp3s: rmp3_list.append( dir + mp3 ) return rmp3_list ### test #myPath = "/home/mike/Shared/Music/" # #dlt = get_directory_list( myPath ) #print "Directory List:" #print dlt #print "" #mlt = get_mp3_list( myPath + "Metallica/" ) #print "MP3 List:" #print mlt #print "" ## #rdt = get_recursive_dir_list( myPath ) #print "Recursive Dir List:" #for dir in rdt: # print dir #print "" #rmt = get_recursive_mp3_list( myPath ) #print "Recursive MP3 List:" #mp3cnt = 0 #for mp3 in rmt: # print mp3 # mp3cnt += 1 #print "" #print "MP3 Count: " + str( mp3cnt ) # Run it import sys # Get and check the path to work on if len( sys.argv ) < 2: print "Error: you must specify a path" exit(-1) thePath = sys.argv[1] if not thePath.endswith( '/' ): thePath += "/" if thePath == ".": thePath = os.getcwd() if not os.path.exists( thePath ): print "Error: Invalid Path" # Ok, looks good, start the work print "Using " + thePath print "Generating MP3 file list..." reclist = get_recursive_mp3_list( thePath ) print "Found " + str( len( reclist ) ) + " MP3 files." if len( reclist ) > 0: print "Running mp3gain on MP3 list..." print "" print "" os.execv( mp3g, reclist ) else: print "Nothing to do. Exiting."