Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. # Copyright (c) 2016 Jakub Kukiełka
  2.  
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  4. # documentation files (the "Software"), to deal in the Software without restriction, including without
  5. # limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
  6. # Software, and to permit persons to whom the Software is furnished to do so, subject to the following
  7. # conditions:
  8.  
  9. # The above copyright notice and this permission notice shall be included in all copies or substantial portions
  10. # of the Software.
  11.  
  12. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  13. # TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  14. # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  15. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  16. # DEALINGS IN THE SOFTWARE.
  17.  
  18. # Only supports .ogg! (this is due to my own purposes)
  19.  
  20. from mutagen.oggvorbis import OggVorbis
  21. from mutagen.flac import Picture, error as FLACError
  22. import os
  23. import glob
  24. import base64
  25.  
  26. print("""This program will segregate all music files in the current directory,
  27. into their own album directories.""")
  28.  
  29. proceed = input("Proceed (Y/n)?: ")
  30.  
  31. if proceed != "" and proceed != "y" and proceed != "Y":
  32. exit()
  33.  
  34. def makedir(directory):
  35. if not os.path.exists(directory):
  36. print("creating directory: ", directory)
  37. os.makedirs(directory)
  38.  
  39. # Read all .ogg files in directory
  40. lst_files = glob.glob("*.ogg")
  41. for i in lst_files:
  42. audio = OggVorbis(i)
  43. if not hasattr(audio, "album"):
  44. continue
  45.  
  46. cover_file = glob.glob(audio["album"][0] + "/cover*")
  47. if len(cover_file) == 0:
  48. for b64_data in audio.get("metadata_block_picture", []):
  49. try:
  50. data = base64.b64decode(b64_data)
  51. except (TypeError, ValueError):
  52. continue
  53.  
  54. try:
  55. picture = Picture(data)
  56. except FLACError:
  57. continue
  58.  
  59. extensions = {
  60. "image/jpeg": "jpg",
  61. "image/png": "png",
  62. "image/gif": "gif",
  63. }
  64. ext = extensions.get(picture.mime, "jpg")
  65.  
  66. os.chdir(audio["album"][0])
  67. with open("cover.%s" % ext, "wb") as h:
  68. h.write(picture.data)
  69.  
  70. os.chdir("..")
  71.  
  72. # Move file to appropriate album directory
  73. os.rename(i, audio["album"][0] + "/" + i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement