Created
October 1, 2015 15:15
-
-
Save brutus/69c51cd706a6cf5d1594 to your computer and use it in GitHub Desktop.
Revisions
-
brutus created this gist
Oct 1, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,60 @@ def add_image( filenames, image_path, description='Cover', img_type=3, encoding=3 ): """ Adds an image to MP3 from *filenames*. Args: filenames (lits): paths to the MP3 image_path (string): path to image file description (string): optional description img_type (integer): ID3 code for the iomage (3=cover) encoding (integer): encoding to use (3=UTF-8) Raises: ArgumentError: on *image_path* errors Image Type: https://mutagen.readthedocs.org/en/latest/api/id3.html#mutagen.id3.PictureType Encoding: https://mutagen.readthedocs.org/en/latest/api/id3.html#mutagen.id3.Encoding """ SUPPORTED_TYPES = ( '.png', '.jpg', '.jpeg', ) # check image if not os.path.exists(image_path): msg = "the image seems not to exists: '{}'" msg = msg.format(image_path) raise ArgumentError(msg) img_type = os.path.splitext(image_path)[1].lower() if img_type not in SUPPORTED_TYPES: msg = "image type '{}' is not supported, only: {}" msg = msg.format(img_type, ', '.join(SUPPORTED_TYPES)) raise ArgumentError(msg) if img_type == 'png': mime = 'image/png' else: mime = 'image/jpeg' # add image to files image = open(image_path, mode='rb').read() image = APIC( encoding=encoding, mime=mime, type=img_type, desc=description, data=image ) for filename in filenames: print("- {}".format(filename)) mp3 = MP3(filename, ID3=ID3) # add image tag mp3.tags.add(image) mp3.save() This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ Traceback (most recent call last): File "~/.local/bin/mp3tag", line 190, in <module> sys.exit(main()) File "~/.local/bin/mp3tag", line 183, in main add_image(args.filenames, args.image) File "~/.local/bin/mp3tag", line 124, in add_image data=image File "~/.local/lib/python3.4/site-packages/mutagen/id3/_frames.py", line 69, in __init__ self, kwargs.get(checker.name, None)) File "~/.local/lib/python3.4/site-packages/mutagen/id3/_specs.py", line 130, in validate chr_(value) File "~/.local/lib/python3.4/site-packages/mutagen/_compat.py", line 64, in <lambda> chr_ = lambda x: bytes([x]) TypeError: 'str' object cannot be interpreted as an integer