#!/usr/bin/env python # -*- coding: utf-8 -*- # # Find all *.{mp3,ogg,flac} files under given directory and add empty ID3 header # to raw MPEG Layer 3 streams. # # $LastChangedDate$ # # Copyright (C) 2007 by Timur Izhbulatov # # 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; version 2 only. # # 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. import sys import os def error(message): print >> sys.stderr, str(message) try: import mutagen import mutagen.id3 except ImportError: error('This script requires mutagen but it could not be found\n' 'Please run "easy_install mutagen"\n') sys.exit(1) def main(): if len(sys.argv) != 2: usage() dirname = sys.argv.pop() try: # Make directory tree generator tree_gen = os.walk(dirname) except OSError, e: error('Error opening directory "%s": %s' % (dirname, e)) for dirpath, dirnames, filenames in tree_gen: for filename in filenames: # Work only with *.{mp3,ogg,flac} files if os.path.splitext(filename)[1].lower() not in ('.mp3', '.ogg', '.flac'): continue try: path_to_file = os.path.join(dirpath, filename) # mutagen.File tries to detect file type automatically ftype = mutagen.File(path_to_file) try: ftype.add_tags() except NotImplementedError, e: error('Failed to detect file type of "%s". Skipping.' % filename) continue ftype.save() except (mutagen.id3.error, IOError, OSError), e: # Tags already exist, read error error('Error processing "%s": %s. Skipping.' % (filename, e)) def usage(): message = ( 'Find all *.{mp3,ogg,flac} files under given directory and add empty ID3 header\n' 'to raw MPEG Layer 3 streams.\n' 'Usage:\n' '%s \n' 'Author: Timur Izhbulatov \n' '$LastChangedDate: 2007-11-10 02:36:50 +0300 (Сбт, 10 Ноя 2007)$\n' ) % sys.argv[0] error(message) sys.exit(1) if __name__ == '__main__': main()