def test_WXXX(self): from mutagen.id3 import WXXX self.assert_(isinstance(WXXX(url='durl'), WXXX)) frame = WXXX(encoding=0, desc="d", url="u") self.assertEqual(frame.HashKey, "WXXX:d") frame._pprint()
def test_hash(self): self.assert_(isinstance(WXXX(url='durl'), WXXX)) frame = WXXX(encoding=0, desc="d", url="u") self.assertEqual(frame.HashKey, "WXXX:d") frame._pprint() self.assertEquals(WXXX(text="a").HashKey, WXXX(text="b").HashKey) self.assertNotEquals(WXXX(desc="a").HashKey, WXXX(desc="b").HashKey)
class ID3Tags: title = ID3TagItem('TIT2', lambda i: i.text[0], lambda i: TIT2(encoding=Encoding.UTF8, text=i)) album = ID3TagItem('TALB', lambda i: i.text[0], lambda i: TALB(encoding=Encoding.UTF8, text=i)) artist = ID3TagItem('TPE1', lambda i: i.text[0], lambda i: TPE1(encoding=Encoding.UTF8, text=i)) sync_lrc = ID3TagItem( 'SYLT', lambda i: i.text, lambda i: SYLT( encoding=Encoding.UTF8, lang='eng', format=2, type=1, text=i)) unsync_lrc = ID3TagItem( 'USLT', lambda i: i.text, lambda i: USLT(encoding=Encoding.UTF8, lang='eng', text=i)) url = ID3TagItem('WXXX', lambda i: i.url, lambda i: WXXX(encoding=Encoding.UTF8, url=i)) cover = ID3TagItem( 'APIC', lambda i: i.data, lambda i: APIC( encoding=Encoding. LATIN1, # if other apple music/itunes can't display img mime='image/jpeg', # image/jpeg or image/png type=PictureType.COVER_FRONT, data=i)) def __init__(self, mp3path): self.file_path = mp3path try: self.tag = ID3(mp3path) except ID3NoHeaderError: self.tag = ID3() def save(self, file_path=None): file_path = file_path or self.file_path self.tag.save(file_path or self.file_path, v2_version=3)
def tag_file(filename, artist, title, year=None, genre=None, artwork_url=None, album=None, track_number=None, url=None, comment=None): """ Attempt to put ID3 tags on a file. Args: artist (str): title (str): year (int): genre (str): artwork_url (str): album (str): track_number (str): filename (str): url (str): """ try: audio = EasyMP3(filename) audio.tags = None audio["artist"] = artist audio["title"] = title if year: audio["date"] = str(year) if album: audio["album"] = album if track_number: audio["tracknumber"] = track_number if genre: audio["genre"] = genre if url: audio["website"] = url if artwork_url: artwork_url = artwork_url.replace('https', 'http') audio["website"] = artwork_url audio.save() # because there is software that doesn't seem to use WOAR we save url tag again as WXXX if url: audio = MP3(filename, ID3=OldID3) audio.tags.add(WXXX(encoding=3, url=url)) audio.save() return True except Exception as e: puts( colored.red("Problem tagging file: ") + colored.white("Is this file a WAV?")) return False
def save(self, *, file_path=None, title=None, album=None, artist=None, sync_lrc=None, unsync_lrc=None, img=None, url=None): """保存歌曲的ID3信息""" if img: self.tag.setall( 'APIC', [ APIC( encoding=Encoding. LATIN1, # if other apple music/itunes can't display img mime='image/jpeg', # image/jpeg or image/png type=PictureType. COVER_FRONT, # 3 is for the cover image data=img) ]) if sync_lrc: # Sample: format=2, type=1, text=[("Do you know what's worth fighting for'", 17640), ...]) # 不知道 format=2, type=1 的含义,这是使用ID3读取现有mp3逆向得到的 self.tag.setall("SYLT", [ SYLT(encoding=Encoding.UTF8, lang='eng', format=2, type=1, text=sync_lrc) ]) if unsync_lrc: self.tag.setall( "USLT", [USLT(encoding=Encoding.UTF8, lang='eng', text=unsync_lrc)]) if title: self.tag.setall("TIT2", [TIT2(encoding=Encoding.UTF8, text=title)]) if album: self.tag.setall("TALB", [TALB(encoding=Encoding.UTF8, text=album)]) if artist: self.tag.setall("TPE1", [TPE1(encoding=Encoding.UTF8, text=artist)]) if url: self.tag.setall("WXXX", [WXXX(encoding=Encoding.UTF8, url=url)]) self.tag.save(file_path or self.file_path, v2_version=3)
def writeTag(self, podData): try: self.audiofile.clear() self.audiofile.add_tags() except: pass self.audiofile.tags.add(TIT2(encoding=3, text=podData.Header)) self.audiofile.tags.add(TPE1(encoding=3, text=podData.Title)) self.audiofile.tags.add(TALB(encoding=3, text=podData.Title)) self.audiofile.tags.add(WXXX(encoding=3, text=podData.DownloadUrl)) self.audiofile.tags.add(COMM(encoding=3, text=podData.Description)) self.date = datetime.strptime(podData.DatePosted, "%a, %d %B %Y") self.dateinput = u"" + str(self.date.year) + "-" + str( self.date.month) + "-" + str(self.date.day) self.audiofile.tags.add(TDRC(encoding=3, text=[self.dateinput])) self.audiofile.save(v1=2)
def tagMP3(conf, mediafile): # http://id3.org/id3v2.3.0#Attached_picture # http://id3.org/id3v2.4.0-frames (section 4.14) # https://stackoverflow.com/questions/7275710/mutagen-how-to-detect-and-embed-album-art-in-mp3-flac-and-mp4 # https://stackoverflow.com/questions/409949/how-do-you-embed-album-art-into-an-mp3-using-python img_stream, img_meta = imgConv(conf['tags']['img']) print('{0}: Now adding tags to {1}...'.format(datetime.datetime.now(), mediafile)) tag = ID3(mediafile) tag.add(TALB(encoding = 3, text = [conf['tags']['album']])) tag.add(APIC(encoding = 3, mime = img_meta['mime'], type = 3, desc = '{0} ({1})'.format(conf['tags']['artist'], conf['tags']['comment']), data = img_stream)) tag.add(TDRC(encoding = 3, text = ['{0}.{1}.{2}'.format(conf['tags']['year'], conf['episode']['month'], conf['episode']['day'])])) tag.add(TENC(encoding = 3, text = [conf['tags']['encoded']])) tag.add(TRCK(encoding = 3, text = [conf['tags']['track']])) tag.add(COMM(encoding = 3, #lang = '\x00\x00\x00', # I'm not sure why we're sending three NULLs, but best to be explicit. lang = 'eng', desc = 'Description provided by Podloader. https://git.square-r00t.net/Podloader', text = [conf['tags']['comment']])) tag.add(WXXX(encoding = 3, desc = conf['tags']['artist'], url = conf['tags']['url'])) tag.add(TCON(encoding = 3, text = [conf['tags']['genre']])) tag.add(TIT2(encoding = 3, text = [conf['episode']['pretty_title']])) tag.add(TPE1(encoding = 3, text = [conf['tags']['artist']])) tag.add(TCOP(encoding = 3, text = [conf['tags']['copyright']])) tag.save()
def generate_with_kw(self, id3, kw, update): url = self.url.replace('KEYWORD', urllib.quote_plus(kw.encode('utf8'))) logger.debug('Crawling %(url)s' % locals()) soup = BeautifulSoup(requests.get(url).content, 'lxml') results = soup.select('table.mp3Tracks tr td.songTitle a') if not results: logger.info('Amazon, no results for %(kw)s' % locals()) return False result = results[0] url = result.get("href") logger.debug("Found specific url: %(url)s" % locals()) soup = BeautifulSoup(requests.get(url).content, 'lxml') id3.add(WXXX(encoding=3, desc=u"Amazon url", url=url)) album = soup.select("#fromAlbum a") if album and (update or "TALB" not in id3): id3.add( TALB(encoding=3, text=album[0].find(text=True).strip(" \n"))) images = soup.select("div#coverArt_feature_div img") + soup.select( '#prodImageContainer img') if images and (update or 'APIC:Cover' not in id3): data = requests.get(images[0].get("src")).content id3.add( APIC(encoding=3, mime="image/jpeg", type=3, desc=u"Cover", data=data)) details = [ filter(lambda x: x not in ("\n", "", " "), detail.find_all(text=True)) for detail in soup.select("div.content li") if detail.find("strong") ] for detail in details: if detail and detail[0] == "Genres:" and ( update or "TCON" not in id3) and len(detail) >= 2: id3.add(TCON(encoding=3, text=detail[1])) return True
def _audio_wxxx(atuple): audio, atag, _, _ = atuple param = ast.literal_eval(atag) audio.add(WXXX(param[0], param[1], param[2]))
def tag_file(filename, artist, title, year=None, genre=None, artwork_url=None, album=None, track_number=None, url=None): """ Attempt to put ID3 tags on a file. Args: artist (str): title (str): year (int): genre (str): artwork_url (str): album (str): track_number (str): filename (str): url (str): """ try: audio = EasyMP3(filename) audio.tags = None audio["artist"] = artist audio["title"] = title if year: audio["date"] = str(year) if album: audio["album"] = album if track_number: audio["tracknumber"] = track_number if genre: audio["genre"] = genre if url: # saves the tag as WOAR audio["website"] = url audio.save() if artwork_url: artwork_url = artwork_url.replace('https', 'http') mime = 'image/jpeg' if '.jpg' in artwork_url: mime = 'image/jpeg' if '.png' in artwork_url: mime = 'image/png' if '-large' in artwork_url: new_artwork_url = artwork_url.replace('-large', '-t500x500') try: image_data = requests.get(new_artwork_url).content except Exception as e: # No very large image available. image_data = requests.get(artwork_url).content else: image_data = requests.get(artwork_url).content audio = MP3(filename, ID3=OldID3) audio.tags.add( APIC( encoding=3, # 3 is for utf-8 mime=mime, type=3, # 3 is for the cover image desc='Cover', data=image_data)) audio.save() # because there is software that doesn't seem to use WOAR we save url tag again as WXXX if url: audio = MP3(filename, ID3=OldID3) audio.tags.add(WXXX(encoding=3, url=url)) audio.save() return True except Exception as e: puts( colored.red("Problem tagging file: ") + colored.white("Is this file a WAV?")) return False
def test_WXXX(self): from mutagen.id3 import WXXX self.assert_(isinstance(WXXX(url='durl'), WXXX))
def test_multi_WXXX(self): from mutagen.id3 import WXXX self.assertEquals(WXXX(encoding=0, text="a").HashKey, WXXX(encoding=0, text="b").HashKey) self.assertNotEquals(WXXX(encoding=0, desc="a").HashKey, WXXX(encoding=0, desc="b").HashKey)