Guest User

Untitled

a guest
Apr 21st, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. """
  4. audiofiles.py
  5.  
  6. Copyright (c) 2009 marcc (Marc Crovic).
  7.  
  8. Permission is hereby granted, free of charge, to any person
  9. obtaining a copy of this software and associated documentation
  10. files (the "Software"), to deal in the Software without
  11. restriction, including without limitation the rights to use,
  12. copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. copies of the Software, and to permit persons to whom the
  14. Software is furnished to do so, subject to the following
  15. conditions:
  16.  
  17. The above copyright notice and this permission notice shall be
  18. included in all copies or substantial portions of the Software.
  19.  
  20. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  22. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  24. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  25. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  26. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  27. OTHER DEALINGS IN THE SOFTWARE
  28. """
  29.  
  30. import sys
  31. import os
  32.  
  33.  
  34. def get_audio(rootdir, summary=False):
  35. """Finds audio files by recursively walking the filesystem from the path
  36. 'rootdir', looking for audio files of several formats.
  37.  
  38. Builds a list of full file paths. Setting the optional summary argument
  39. to true skips list building and returns a count of audio files.
  40. """
  41. file_list = []
  42. # feel free to edit these
  43. # TODO read in from a text file
  44. ext = ['.aa3', '.aac', '.aif', '.aiff', '.ape', '.au' , '.flac', '.m3u',
  45. '.m4a', '.m4b', '.m4p', '.m4r', '.mid', '.midi', '.mp2', '.mp3',
  46. '.mpa', '.ogg', '.ra', '.ram', '.shn', '.wav', '.wma']
  47. res_msg = '%d audio files found in %s'
  48. # don't get burned by upper case file extensions.
  49. # after all, who knows where your audio files came from...
  50. audio_ext = tuple(ext + [e.upper() for e in ext])
  51. if summary is True:
  52. file_count = 0
  53. for root, dirs, files in os.walk(rootdir):
  54. for filename in files:
  55. if filename.endswith(audio_ext):
  56. if summary is True:
  57. file_count += 1
  58. else:
  59. file_list.append(os.path.join(root, filename))
  60. if summary is True:
  61. print res_msg % (file_count, rootdir)
  62. return file_count
  63. else:
  64. for filepath in file_list:
  65. print filepath
  66. print res_msg % (len(file_list), rootdir)
  67. return file_list
  68. #end get_audio
  69. if __name__ == '__main__':
  70. if len(sys.argv) == 1: # if no arguments
  71. msg = ['\nThis script takes one required argument, the path from ',
  72. 'which to recursively build a list of audio files.\n\nIt',
  73. ' also takes an optional switch of "-c" if you only want to ',
  74. 'count (and not list) audio files.\n\nUsage:\npython ',
  75. 'audiofiles.py /tmp/bt/\npython audiofiles.py ~/Music/ -c\n']
  76. usage = ''.join(msg)
  77. sys.exit(usage)
  78. else:
  79. rootdir = sys.argv[1]
  80. summary = False
  81. if len(sys.argv) > 2:
  82. if '-c' in sys.argv[2:]:
  83. summary = True
  84. get_audio(rootdir, summary)
Add Comment
Please, Sign In to add comment