def parse_dir_update_tags(self, path, ref_path, b_initial=True): # Reset container for failed files if path == ref_path: self.failed = [] for elem in os.listdir(path): full_path_in = os.path.join(path, elem) if os.path.isdir(full_path_in): self.parse_dir_update_tags(full_path_in, ref_path, b_initial=False) elif full_path_in.endswith(".flac"): tags = self._extract_tags(full_path_in, ref_path) try: song = FLAC(full_path_in) song.clear() song.update(tags) song.save() except Exception as e: print(e.__class__.__name__) print(e) self.failed.append(full_path_in) if b_initial: print() if not self.failed: print("All files converted successfully.") else: for e in self.failed: print(f"Failed to process: [{e}]")
def main(): print("start") tracks = [] root = tkinter.Tk() root.withdraw() root.update() folder = askdirectory(title = "choose the dir u want to delete on tags in",initialdir = r"C:\Users\User\Desktop", mustexist=True ) root.destroy() for root, dirs, files, in os.walk(folder): for name in files: if name.endswith((".mp3", ".flac")): print("the name= "+os.path.splitext(name)[0]) print("the type= "+os.path.splitext(name)[1]) if(name.endswith(".flac")): print("its .flac") audio=FLAC(root + "\\" + name) audio.delete() audio.clear_pictures() audio.save() if(name.endswith(".mp3")): print("its .mp3") audio=MP3(root + "\\" + name) audio.delete() audio.clear() audio.save()
def tag_flac(path_audio,path_pic,lrc,title,artist,album,disc,track,**kw): ''' ref: https://www.xiph.org/vorbis/doc/v-comment.html FLAC tags also call Vorbis comment. It doesn't support lyrics. So use ID3 to tag FLAC instead FLAC tags. ''' tags=FLAC(path_audio) tags.clear() tags.clear_pictures() tags.save() tag_mp3(path_audio,path_pic,lrc,title,artist,album,disc,track,**kw)
def standardize(self): """ """ # 1. Is the album already standardized? scanner = scan.Scanner() if scanner.scan_album(self.abs_album_dir): print 'Valid Album Detected' return # 2. Categories all files within the album subdir self.find_files() if not self.cue_file: sys.stderr.write('Cue file not found\n') return # 3. Parse the cue file so we have enough info cue = CueParser().parse(self.cue_file) # 4. Do we have all split tracks present? track_count = len(self.track_files) if track_count != len(cue): sys.stderr.write('Wrong number of tracks: {0}\n'.format(track_count)) return # 5. Fix track metadata and rename them # TODO: Deal with multi-CD releases (but this also on top-level) track_data = dict([(v, getattr(cue,k)) for k,v in const.CUE_TO_TRACK.items()]) track_data['tracktotal'] = '{0:0>2}'.format(len(cue)) for i, track in enumerate(cue): track_number = '{0:0>2}'.format(i+1) track_data['tracknumber'] = track_number track_data['title'] = track['TITLE'] track_file = self.track_files[i] audio = FLAC(track_file) audio.clear() audio.update(track_data) audio.save() # Build proper track name track_name = const.TRACK_FMT.format(track_number, self.sanitize_title(track_data['title']), ) os.rename(track_file, os.path.join(self.abs_album_dir, track_name)) # 6. Clean up other files map(os.remove, self.rm_files+[self.cue_file]) map(shutil.rmtree, self.subdirs) # 7. Rename album folder if necessary album_parent = os.path.dirname(self.abs_album_dir) abs_album_dir = os.path.join(os.path.dirname(self.abs_album_dir), const.ALBUM_FMT.format(cue.REM_DATE, self.sanitize_title(cue.TITLE))) if self.abs_album_dir != abs_album_dir: os.rename(self.abs_album_dir, abs_album_dir)
def prepare_track(self, album, track): temp_path = os.path.join(self.temp_dir, track.temp_filename) track_path = os.path.join(self.temp_dir, track.filename) meta_file = FLAC(q_enc(temp_path)) meta_file.clear() meta_file['album'] = album.title meta_file['albumartist'] = album.joined_artists meta_file['artist'] = track.joined_artists meta_file['title'] = track.title meta_file['date'] = str(album.year) meta_file['genre'] = album.genre meta_file['tracknumber'] = str(track.track_number) meta_file['totaltracks'] = str(track.track_total) meta_file['discnumber'] = str(track.media_number) meta_file['totaldiscs'] = str(album.media_total) meta_file.save() conditional_rename(temp_path, track_path)
def add(self, filename, artist, title, extension, clear): ''' Adds metadata to a file :param file: The already-title-cased file in its new destination :param artist: The artist to be set in the metadata :param title: The title to be set in the metadata :param extension: The extension of the file :param clear: boolean if the tags should be cleared beforehand or not ''' if extension == 'mp3': audio = MP3(filename, ID3=EasyID3) try: audio.add_tags(ID3=EasyID3) except mutagen.id3.error: pass if clear: audio.clear() audio['title'] = title audio['artist'] = artist audio.save() audio.save(v1=2, v2_version=3) elif extension == 'flac': try: audio = FLAC(filename) except FLACNoHeaderError: audio = FLAC() if clear: audio.clear() audio['TITLE'] = title audio['ARTIST'] = artist audio.save(filename) elif extension == 'ogg': try: audio = OggVorbis(filename) except OggVorbisHeaderError: audio = OggVorbis() if clear: audio.clear() audio['title'] = artist audio['artist'] = artist audio['ALBUMARTIST'] = artist audio.save(filename)
def tag(file_path: Path, track: Track) -> None: """ Tag the music file at the given file path using the specified [Track][deethon.types.Track] instance. Args: file_path (Path): The music file to be tagged track: The [Track][deethon.types.Track] instance to be used for tagging. """ ext = file_path.suffix if ext == ".mp3": tags = ID3() tags.clear() tags.add(Frames["TALB"](encoding=3, text=track.album.title)) tags.add(Frames["TBPM"](encoding=3, text=str(track.bpm))) tags.add(Frames["TCON"](encoding=3, text=track.album.genres)) tags.add(Frames["TCOP"](encoding=3, text=track.copyright)) tags.add(Frames["TDAT"](encoding=3, text=track.release_date.strftime("%d%m"))) tags.add(Frames["TIT2"](encoding=3, text=track.title)) tags.add(Frames["TPE1"](encoding=3, text=track.artist)) tags.add(Frames["TPE2"](encoding=3, text=track.album.artist)) tags.add(Frames["TPOS"](encoding=3, text=str(track.disk_number))) tags.add(Frames["TPUB"](encoding=3, text=track.album.label)) tags.add(Frames["TRCK"]( encoding=3, text=f"{track.number}/{track.album.total_tracks}")) tags.add(Frames["TSRC"](encoding=3, text=track.isrc)) tags.add(Frames["TYER"](encoding=3, text=str(track.release_date.year))) tags.add(Frames["TXXX"](encoding=3, desc="replaygain_track_gain", text=str(track.replaygain_track_gain))) if track.lyrics: tags.add(Frames["USLT"](encoding=3, text=track.lyrics)) tags.add(Frames["APIC"](encoding=3, mime="image/jpeg", type=3, desc="Cover", data=track.album.cover_xl)) tags.save(file_path, v2_version=3) else: tags = FLAC(file_path) tags.clear() tags["album"] = track.album.title tags["albumartist"] = track.album.artist tags["artist"] = track.artist tags["bpm"] = str(track.bpm) tags["copyright"] = track.copyright tags["date"] = track.release_date.strftime("%Y-%m-%d") tags["genre"] = track.album.genres tags["isrc"] = track.isrc if track.lyrics: tags["lyrics"] = track.lyrics tags["replaygain_track_gain"] = str(track.replaygain_track_gain) tags["title"] = track.title tags["tracknumber"] = str(track.number) tags["year"] = str(track.release_date.year) cover = Picture() cover.type = 3 cover.data = track.album.cover_xl cover.width = 1000 cover.height = 1000 tags.clear_pictures() tags.add_picture(cover) tags.save(deleteid3=True)
cli_args = [ 'ffmpeg', '-i', tag_path[i], '-ac', '2', '-acodec', 'flac', 'tag.flac' ] try: subprocess.check_output(cli_args) except: print("convert tag flac error") # read tag info tag_info = FLAC('tag.flac') key_list = tag_info.keys() # write tag info audio_flac = FLAC(flac_path[i]) audio_flac.clear() audio_flac.clear_pictures() for key in key_list: audio_flac[key] = tag_info[key] # write cover image info if os.path.exists('./cover.jpg'): img = fimg() img.type = 3 img.mime = 'image/jpg' img.desc = 'front cover' img.colors = 0 img.data = open('cover.jpg', mode='rb').read() audio_flac.add_picture(img) # save music info