def _audio_tbpm(atuple): audio, atag, advanced, _, _ = atuple if advanced: param = ast.literal_eval(atag) audio.add(TBPM(3, param[1])) else: audio.add(TBPM(3, atag))
def add_mp3_tags(fileobj, tags, cover=None, lyrics=None, image_mimetype='image/png'): handle = MP3(fileobj=fileobj) if 'artist' in tags: handle['TPE1'] = TPE1(text=tags['artist']) if 'title' in tags: handle['TIT2'] = TIT2(text=tags['title']) if 'album' in tags: handle['TALB'] = TALB(text=tags['album']) if 'albumartist' in tags: handle['TPE2'] = TPE2(text=tags['albumartist']) if 'genre' in tags: handle['TCON'] = TCON(genres=[tags['genre']]) if 'tracknumber' in tags: handle['TRCK'] = TRCK(text=tags['tracknumber']) if 'year' in tags: handle['TYER'] = TYER(text=tags['year']) if 'date' in tags: handle['TDAT'] = TDAT(text=tags['date']) if 'bpm' in tags: handle['TBPM'] = TBPM(text=tags['bpm']) if 'isrc' in tags: handle['TSRC'] = TSRC(text=tags['isrc']) if 'explicit' in tags: handle['TXXX'] = TXXX(text=tags['explicit']) if lyrics: handle['USLT'] = USLT(text=lyrics) if cover: handle['APIC'] = APIC(data=cover, mime=image_mimetype) handle.save(fileobj) fileobj.seek(0)
def saveFiles(): #Saves everything you have done playlistPath = os.path.abspath("playlists/") currentListSongIds = [] libraryDict = {} for song in playlistLibrary[0].songObjectList: libraryDict[song.songID] = song.filePath #Go through one playlist at the time for playlist in playlistLibrary: songsToCopy = [] songObjectIds = [] currentListSongIds = [] writeWithBPM = playlist.writeWithBPM if playlist.name != "library": #Creates list with all files in the playlist folder try: songFileList = os.listdir(playlistPath + "/" + playlist.name) except FileNotFoundError: os.mkdir(playlistPath + "/" + playlist.name) time.sleep(2) songFileList = os.listdir(playlistPath + "/" + playlist.name) #Create a list of all IDs of the songs in the playlist folder for songFile in songFileList: if songFile != ".DS_Store": songID3 = ID3(playlistPath + "/" + playlist.name + "/" + songFile) currentListSongIds.append(songID3['TRCK'].text[0]) #Check what songs are in objectList. If not in playlist folder, add to copy array for song in playlist.songObjectList: songObjectIds.append(song.songID) if song.songID not in currentListSongIds: songsToCopy.append(song) for song in songsToCopy: copyfile(libraryDict.get(song.songID), playlistPath + "/" + playlist.name + "/" + song.title + " - " + song.artist + ".mp3") #Write with or without bpm and change names - Rewerite ID3 finalSongFileList = os.listdir(playlistPath + "/" + playlist.name) for songFile in finalSongFileList: if songFile != ".DS_Store": songID3 = ID3(playlistPath + "/" + playlist.name + "/" + songFile) songID3["TIT2"] = TIT2(encoding=3, text=findID3ObjMatch(songID3["TRCK"].text[0], playlist).title) songID3["TPE1"] = TPE1(encoding=3, text=findID3ObjMatch(songID3["TRCK"].text[0], playlist).artist) songID3["TBPM"] = TBPM(encoding=3, text=findID3ObjMatch(songID3["TRCK"].text[0], playlist).bpm) songID3.save() if writeWithBPM: os.rename(playlistPath + "/" + playlist.name + "/" + songFile, playlistPath + "/" + playlist.name + "/" + "(" + songID3['TBPM'].text[0] + ")" + songID3['TIT2'].text[0] + " - " + songID3['TPE1'].text[0] + ".mp3") else: os.rename(playlistPath + "/" + playlist.name + "/" + songFile, playlistPath + "/" + playlist.name + "/" + songID3['TIT2'].text[0] + " - " + songID3['TPE1'].text[0] + ".mp3") print("Everything is saved!")
def mp3_tag(mp3_dirname, mp3_filename, artist, album, track, tracks, title, year, genre, bpms, compilation): # Delete existing tags try: id3 = ID3(mp3_filename) id3.delete() except ID3NoHeaderError: id3 = ID3() # Artist id3.add(TPE1(encoding=3, text=artist)) # Artistsort id3.add(TSOP(encoding=3, text=artist)) # Band id3.add(TPE2(encoding=3, text=artist)) # Composer id3.add(TCOM(encoding=3, text=artist)) # Album id3.add(TALB(encoding=3, text=album)) # Albumsort id3.add(TSOA(encoding=3, text=album)) # Track id3.add(TRCK(encoding=3, text=tracks)) # Title id3.add(TIT2(encoding=3, text=title)) # Year id3.add(TDRC(encoding=3, text=year)) # Genre id3.add(TCON(encoding=3, text=genre)) # BPMs id3.add(TBPM(encoding=3, text=bpms)) # Compilation if (compilation): id3.add(TCMP(encoding=3, text='1')) else: id3.add(TCMP(encoding=3, text='0')) # Cover image = str(mp3_dirname + '/Cover.jpg') try: imagefile = open(image, 'rb').read() id3.add(APIC(3, 'image/jpeg', 3, 'Cover', imagefile)) except: print("Warning. No Cover.jpg in directory " + mp3_dirname + ".") # Save tags to file id3.save(mp3_filename, v2_version=4, v1=2)
def stretch(self): try: self.dlg.actionStretch.setChecked(True) filename=self.list[int(self.dlg.listWidget.currentRow())] base, ext = os.path.splitext(filename) try: track=self.read_id3v2(os.path.join(os.getcwd(),filename)) print(track) except: if ext!=".mp3": self.message("Make shure you selected \nan *.mp3 file") else: self.message("Analyse track in Traktor:\n"+ filename) if ext==".mp3": if not os.path.exists("stretch"): os.makedirs("stretch") i, ok = QInputDialog.getInteger(self,"QInputDialog.getInteger()", "Percentage:", 160, 120, 300) i=str(i) if ok: Work.v_wav(self.work,os.path.join(os.getcwd(),filename),os.path.join(os.getcwd(),"stretch",base+".wav")) self.progress(1,4) Work.spremeni_hitrost(self.work,os.path.join(os.getcwd(),"stretch",base+".wav"), os.path.join(os.getcwd(),"stretch",base+".stretch.wav"),track[3],i) os.remove(os.path.join(os.getcwd(),"stretch",base+".wav")) self.progress(2,4) Work.v_mp3(self.work,os.path.join(os.getcwd(),"stretch",base+".stretch.wav"),os.path.join(os.getcwd(),"stretch",base+"."+i+".mp3")) os.remove(os.path.join(os.getcwd(),"stretch",base+".stretch.wav")) self.progress(3,4) try: audio= ID3(os.path.join(os.getcwd(),"stretch",base+"."+i+".mp3")) audio["TPE1"] = TPE1(encoding=3, text=track[0]) audio["TIT2"] = TIT2(encoding=3, text=track[1]) audio["TKEY"] = TKEY(encoding=3, text=track[2]) audio["TBPM"] = TBPM(encoding=3, text=i) audio["COMM"] = COMM(encoding=3, lang=u'eng', desc='desc', text='Stretched with MP3EDJ') audio.save(os.path.join(os.getcwd(),"stretch",base+"."+i+".mp3"), v1=2) except: pass self.list.append(os.path.join("stretch",base+"."+i+".mp3")) self.dlg.listWidget.addItem(os.path.join("stretch",base+"."+i+".mp3")) self.progress(4,4) self.dlg.actionStretch.setChecked(False) self.dlg.progressBar.setValue(0) except: pass
def write_tag(self, tag, value, save=True): text = [normalize_tag_text(value)] if tag in self.id3.keys(): self.id3[tag].text = text elif tag in TRACK_MD_ID3_TAGS: if tag == ID3Tag.TITLE.value: self.id3[tag] = TIT2(text=text) elif tag == ID3Tag.GENRE.value: self.id3[tag] = TCON(text=text) elif tag == ID3Tag.BPM.value: self.id3[tag] = TBPM(text=text) elif tag == ID3Tag.KEY.value: self.id3[tag] = TKEY(text=text) elif tag == ID3Tag.LABEL.value: self.id3[tag] = TPUB(text=text) elif tag == ID3Tag.COMMENT.value or tag == ID3Tag.COMMENT_ENG.value or tag == ID3Tag.COMMENT_XXX.value: self.id3[ID3Tag.COMMENT.value] = COMM(text=text) self.id3[ID3Tag.COMMENT_ENG.value] = COMM(text=text) self.id3[ID3Tag.COMMENT_XXX.value] = COMM(text=text) if save: self.id3.save()
def update_file(filename, language, clear_genre, append_genre): """ Update the audio file with the new found dances (in the genre field) :param filename: The filename of the audio file :param language: Language to get the dance names in :param clear_genre: If we need to clear the genre in the file if no data is found :param append_genre:If we need to append the new genre (dance) to the existing list of genres :return: track, found, dances_found: track: The track with data from the file and dances from the database found: If the track was known by the online database dances_found: If any dance data was found for the track """ found = False dances_found = False track = extract_info_from_file(filename) if track: found = find_dances_online(track, language) if len(track.dances) > 0: dances_found = True dances_str = track.dances[0].name for i in range(1, len(track.dances)): dances_str += ", " + track.dances[i].name print("Found '{:} by {:}' : '{:}'".format(track.title, track.band.name, dances_str)) file = mutagen.File(filename) if "title" in file.keys(): if not append_genre: file["genre"] = [] for dance in track.dances: if not "genre" in file.keys(): file["genre"] = [dance.name] elif not dance.name in file["genre"]: file["genre"] += [dance.name] if track.bpm > 0: file["bpm"] = str(track.bpm) file.save() else: file = ID3(filename) if not append_genre: file.delall("TCON") for dance in track.dances: if not "TCON" in file.keys() or not dance in file["TCON"]: file.add(TCON(encoding=3, text=dance.name)) if track.bpm > 0: file.delall("TBPM") file.add(TBPM(encoding=3, text=[track.bpm])) file.save() else: if clear_genre: # We might want to purge unknown genres file = mutagen.File(filename) if "title" in file.keys(): file["genre"] = [] file.save() else: file = ID3(filename) file.delall("TCON") file.save() print("No data found for {:} by {:}".format( track.title, track.band.name)) return track, found, dances_found
def update_id3(self, path: str, track: beatport.Track): #AIFF Check aiff = None if path.endswith('.aiff') or path.endswith('.aif'): aiff = AIFF(path) f = aiff.tags else: f = ID3() f.load(path, v2_version=3, translate=True) #Update tags if UpdatableTags.title in self.config.update_tags and self.config.overwrite: f.setall('TIT2', [TIT2(text=track.title)]) if UpdatableTags.artist in self.config.update_tags and self.config.overwrite: f.setall('TPE1', [ TPE1(text=self.config.artist_separator.join( [a.name for a in track.artists])) ]) if UpdatableTags.album in self.config.update_tags and ( self.config.overwrite or len(f.getall('TALB')) == 0): f.setall('TALB', [TALB(text=track.album.name)]) if UpdatableTags.label in self.config.update_tags and ( self.config.overwrite or len(f.getall('TPUB')) == 0): f.setall('TPUB', [TPUB(text=track.label.name)]) if UpdatableTags.bpm in self.config.update_tags and ( self.config.overwrite or len(f.getall('TBPM')) == 0): f.setall('TBPM', [TBPM(text=str(track.bpm))]) if UpdatableTags.genre in self.config.update_tags and ( self.config.overwrite or len(f.getall('TCON')) == 0): f.setall('TCON', [TCON(text=', '.join([g.name for g in track.genres]))]) #Dates if UpdatableTags.date in self.config.update_tags: #ID3 v2.3 if self.config.id3v23 and (self.config.overwrite or (len(f.getall('TYER')) == 0 and len(f.getall('TDAT')) == 0)): date = track.release_date.strftime('%d%m') f.setall('TDRC', []) f.setall('TDAT', [TDAT(text=date)]) f.setall('TYER', [TYER(text=str(track.release_date.year))]) #ID3 v2.4 if not self.config.id3v23 and (self.config.overwrite or len(f.getall('TDRC')) == 0): date = track.release_date.strftime('%Y-%m-%d') f.setall('TDAT', []) f.setall('TYER', []) f.setall('TDRC', [TDRC(text=date)]) if UpdatableTags.key in self.config.update_tags and ( self.config.overwrite or len(f.getall('TKEY')) == 0): f.setall('TKEY', [TKEY(text=track.id3key())]) if UpdatableTags.publishdate in self.config.update_tags and ( self.config.overwrite or len(f.getall('TDRL')) == 0): # f.setall('TORY', [TORY(text=str(track.publish_date.year))]) if not self.config.id3v23: date = track.publish_date.strftime('%Y-%m-%d') f.setall('TDRL', [TDRL(text=date)]) #Other keys if UpdatableTags.other in self.config.update_tags: f.add(TXXX(desc='WWWAUDIOFILE', text=track.url())) f.add(TXXX(desc='WWWPUBLISHER', text=track.label.url('label'))) #Redownlaod cover if self.config.replace_art: try: url = track.art(self.config.art_resolution) r = requests.get(url) data = APIC(encoding=3, mime='image/jpeg', type=3, desc=u'Cover', data=r.content) f.delall('APIC') f['APIC:cover.jpg'] = data except Exception: logging.warning('Error downloading cover for file: ' + path) if aiff == None: if self.config.id3v23: f.save(path, v2_version=3, v1=0) else: f.save(path, v2_version=4, v1=0) else: aiff.save()