Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Copyright (c) 2016 Jakub Kukiełka
- # 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.
- # Only supports .ogg! (this is due to my own purposes)
- from mutagen.oggvorbis import OggVorbis
- from mutagen.flac import Picture, error as FLACError
- import os
- import glob
- import base64
- print("""This program will segregate all music files in the current directory,
- into their own album directories.""")
- proceed = input("Proceed (Y/n)?: ")
- if proceed != "" and proceed != "y" and proceed != "Y":
- exit()
- def makedir(directory):
- if not os.path.exists(directory):
- print("creating directory: ", directory)
- os.makedirs(directory)
- # Read all .ogg files in directory
- lst_files = glob.glob("*.ogg")
- for i in lst_files:
- audio = OggVorbis(i)
- if not hasattr(audio, "album"):
- continue
- cover_file = glob.glob(audio["album"][0] + "/cover*")
- if len(cover_file) == 0:
- for b64_data in audio.get("metadata_block_picture", []):
- try:
- data = base64.b64decode(b64_data)
- except (TypeError, ValueError):
- continue
- try:
- picture = Picture(data)
- except FLACError:
- continue
- extensions = {
- "image/jpeg": "jpg",
- "image/png": "png",
- "image/gif": "gif",
- }
- ext = extensions.get(picture.mime, "jpg")
- os.chdir(audio["album"][0])
- with open("cover.%s" % ext, "wb") as h:
- h.write(picture.data)
- os.chdir("..")
- # Move file to appropriate album directory
- os.rename(i, audio["album"][0] + "/" + i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement