Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # encoding: utf-8
- """
- audiofiles.py
- Copyright (c) 2009 marcc (Marc Crovic).
- Permission is hereby granted, free of charge, to any person
- obtaining a copy of this software and associated documentation
- files (the "Software"), to deal in the Software without
- restriction, including without limitation the rights to use,
- copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the
- Software is furnished to do so, subject to the following
- conditions:
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- OTHER DEALINGS IN THE SOFTWARE
- """
- import sys
- import os
- def get_audio(rootdir, summary=False):
- """Finds audio files by recursively walking the filesystem from the path
- 'rootdir', looking for audio files of several formats.
- Builds a list of full file paths. Setting the optional summary argument
- to true skips list building and returns a count of audio files.
- """
- file_list = []
- # feel free to edit these
- # TODO read in from a text file
- ext = ['.aa3', '.aac', '.aif', '.aiff', '.ape', '.au' , '.flac', '.m3u',
- '.m4a', '.m4b', '.m4p', '.m4r', '.mid', '.midi', '.mp2', '.mp3',
- '.mpa', '.ogg', '.ra', '.ram', '.shn', '.wav', '.wma']
- res_msg = '%d audio files found in %s'
- # don't get burned by upper case file extensions.
- # after all, who knows where your audio files came from...
- audio_ext = tuple(ext + [e.upper() for e in ext])
- if summary is True:
- file_count = 0
- for root, dirs, files in os.walk(rootdir):
- for filename in files:
- if filename.endswith(audio_ext):
- if summary is True:
- file_count += 1
- else:
- file_list.append(os.path.join(root, filename))
- if summary is True:
- print res_msg % (file_count, rootdir)
- return file_count
- else:
- for filepath in file_list:
- print filepath
- print res_msg % (len(file_list), rootdir)
- return file_list
- #end get_audio
- if __name__ == '__main__':
- if len(sys.argv) == 1: # if no arguments
- msg = ['\nThis script takes one required argument, the path from ',
- 'which to recursively build a list of audio files.\n\nIt',
- ' also takes an optional switch of "-c" if you only want to ',
- 'count (and not list) audio files.\n\nUsage:\npython ',
- 'audiofiles.py /tmp/bt/\npython audiofiles.py ~/Music/ -c\n']
- usage = ''.join(msg)
- sys.exit(usage)
- else:
- rootdir = sys.argv[1]
- summary = False
- if len(sys.argv) > 2:
- if '-c' in sys.argv[2:]:
- summary = True
- get_audio(rootdir, summary)
Add Comment
Please, Sign In to add comment