def Scan(path, files, mediaList, subdirs, language=None, root=None): """Handle .cue files. Find any Cue files in the input list. Remove any files handled by the Cue file. Handle any remaining files normally. """ FLACCueParse(path, files, mediaList, subdirs, language=language, root=root) # Scan for other audio files, including those that failed to process correctly. AudioFiles.Scan(path, files, mediaList, subdirs, root=root) # Read tags, etc. and build up the mediaList AudioFiles.Process(path, files, mediaList, subdirs, language=language, root=root)
def Scan(path, files, mediaList, subdirs, language=None, root=None): """Handle FLAC with Cue. Find any Cue files in the input list. """ cues = [] for f in files: if(os.path.splitext(f)[1] == '.cue'): cues.append(f) for cue in cues: log(cue) try: # Get the Cue sheet info. info = read_cue(cue) log(info) # Get all files mentioned in the Cue sheet. cuefiles = info['Files'] # Get the directory the files should be in. This should be # the same as the Cue sheet directory. folder = os.path.dirname(cue) for file in cuefiles: # Get the full FLAC file path. full_file = os.path.join(folder, file) # My FLAC files include "Disc 1", "Disc 2", and similar as the # final part of the title for multi-disk sets. Something like: # "Artist - Album Title Disc 3.flac" # The scanner is designed to pull disc information from this # and to group albums together. # Get the name of the FLAC file without the extension. # Split it for white space. try: flac_details = os.path.splitext(file)[-2].split() # Check for disc numbering. if(flac_details[-2] == 'Disc'): disc = int(flac_details[-1]) else: disc = 1 except IndexError: disc = 1 try: # Add the full FLAC file. # Number it as track -1. track = -1 # Get the file info for the full FLAC file. flac_info = mutagen.flac.FLAC(full_file) # Add the additional disc number information. flac_info['discnumber'] = ['{0:0d}'.format(disc)] # Make this track -1. flac_info['tracknumber'] = ['{0:02d}'.format(track)] # Call this "Album Name - Full Album" in the track listing # Ensure the info is added cleanly. if('artist' not in flac_info or flac_info['artist'][0] == ''): flac_info['artist'] = [info['PERFORMER']] if('album' not in flac_info or flac_info['album'][0] == ''): flac_info['album'] = [info['TITLE']] if('albumartist' not in flac_info or flac_info['albumartist'][0] == ''): flac_info['albumartist'] = [info['PERFORMER']] flac_info['title'] = ['{0} - Full album'.format(flac_info['album'][0])] artist = AudioFiles.cleanPass(flac_info['artist'][0]) album = AudioFiles.cleanPass(flac_info['album'][0]) title = AudioFiles.cleanPass(flac_info['title'][0]) album_artist = AudioFiles.cleanPass(flac_info['albumartist'][0]) # Create the track object. track_object = Media.Track(artist, album, title, track, disc=disc, album_artist=album_artist, guid=None, album_guid=None) # Use the FLAC file for playback. track_object.parts.append(full_file) log(track_object) # Add the track object to the output list. mediaList.append(track_object) # Split into tracks. track_info = cuefiles[file]['Tracks'] start_time = '00:00:00' end_time = '00:00:00' # Handle each track. for track in track_info: title = AudioFiles.cleanPass(track_info[track]['TITLE']) try: # Get the start time of the track. start_time = track_info[track]['INDEX'][1] except KeyError: # If none is listed, use the previous end time. start_time = end_time try: # Get the start time of the following track. # Use this as the end time for the current track. end_time = track_info[track+1]['INDEX'][1] except (IndexError, KeyError): # For the last track, we just take the end time of the # FLAC file as the end of the song. file_end = flac_info.info.length minutes = int(file_end / 60) seconds = int(file_end % 60) # 75 frames per second. frames = int(round(75 * (file_end % 1))) end_time = '{0:02d}:{1:02d}:{2:02d}'.format(minutes, seconds, frames) # Create the track object. track_object = Media.Track(artist, album, title, track, disc=disc, album_artist=album_artist, guid=None, album_guid=None) # Use a file format for compatibility with the FLACCue # FUSE (Filesystem in Userspace) code. # This will be something like: # /flaccue/Music/Artist/Album/Artist - Album Disc 1.flac.10:25:17.12:55:20 track_object.parts.append('/flaccue'+full_file+'.{0}.{1}'.format(start_time, end_time)) log(track_object) # Add the track to the returned object list. mediaList.append(track_object) log('') # Remove the FLAC file from the list to parse. files.remove(full_file) except: log(traceback.format_exc()) except: log(traceback.format_exc()) finally: files.remove(cue) # Scan for other audio files, including those that failed to process correctly. AudioFiles.Scan(path, files, mediaList, subdirs, root=root) # Read tags, etc. and build up the mediaList AudioFiles.Process(path, files, mediaList, subdirs, language=language, root=root)
def Scan(path, files, media_list, subdirs, language=None, root=None, respect_tags=False): # Scan for audio files. AudioFiles.Scan(path, files, media_list, subdirs, root) root_str = root or '' loc_str = os.path.join(root_str, path) Log('Scanning: ' + loc_str) Log('Files: ' + str(files)) Log('Subdirs: ' + str(subdirs)) # Look at the files and determine whether we can do a quick match (minimal tag parsing). do_quick_match = True mixed = False # Make sure we're looking at a leaf directory (no audio files below here). if len(subdirs) > 0: Log('Found directories below this one; won\'t attempt quick matching.') do_quick_match = False if files: # Make sure we're not sitting in the section root. parent_path = os.path.split(files[0])[0] if parent_path == root: Log('File(s) are in section root; doing expensive matching with mixed content.' ) do_quick_match = False mixed = True # Make sure we have reliable track indices for all files and there are no dupes. tracks = {} for f in files: try: index = re.search(r'^([0-9]{1,2})[^0-9].*', os.path.split(f)[-1]).groups(0)[0] except: do_quick_match = False Log('Couldn\'t find track indices in all filenames; doing expensive matching.' ) break if tracks.get(index): do_quick_match = False mixed = True Log('Found duplicate track index: %s; doing expensive matching with mixed content.' % index) break else: tracks[index] = True # Read the first track's tags to check for milti-disc and VA. if do_quick_match: disc = album_artist = None try: (artist, album, title, track, disc, album_artist, compil) = AudioFiles.getInfoFromTag(files[0], language) except: Log('Exception reading tags from first file; doing expensive matching.' ) do_quick_match = False # Make sure we are on the first disc. if disc is not None and disc > 1: Log('Skipping quick match because of non-first disc.') do_quick_match = False # We want to read all the tags for VA albums to pick up track artists. if album_artist is not None and album_artist == 'Various Artists': Log('Skipping quick match for Various Artists album.') do_quick_match = False artist = None album = None if do_quick_match: Log('Doing quick match') # See if we have some consensus on artist/album by reading a few tags. for i in range(3): if i < len(files): this_artist = this_album = tags = None try: tags = mutagen.File(files[i], easy=True) except: Log('There was an exception thrown reading tags.') if tags: # See if there's an album artist tag. album_artist_tags = [ t for t in ['albumartist', 'TPE2', 'performer'] if t in tags ] album_artist_tag = album_artist_tags[0] if len( album_artist_tags) else None this_artist = tags[album_artist_tag][ 0] if album_artist_tag else tags['artist'][ 0] if 'artist' in tags else None this_album = tags['album'][ 0] if 'album' in tags else None if artist and artist != this_artist: Log('Found different artists in tags (%s vs. %s); doing expensive matching.' % (artist, this_artist)) do_quick_match = False break if album and album != this_album: Log('Found different albums in tags (%s vs. %s); doing expensive matching.' % (album, this_album)) do_quick_match = False break artist = this_artist album = this_album if not artist or not album: Log('Couldn\'t determine unique artist or album from tags; doing expensive matching.' ) do_quick_match = False query_list = [] result_list = [] fingerprint = False # Directory looks clean, let's build a query list directly from info gleaned from file names. if do_quick_match: Log('Building query list for quickmatch with artist: %s, album: %s' % (artist, album)) # Determine if the artist and/or album appears in all filenames, since we'll want to strip these out for clean titles. strip_artist = True if len([ f for f in files if artist.lower() in Unicodize( os.path.basename(f), language).lower() ]) == len(files) else False strip_album = True if len([ f for f in files if album.lower() in Unicodize( os.path.basename(f), language).lower() ]) == len(files) else False for f in files: try: filename = os.path.splitext(os.path.split(f)[1])[0] (head, index, title) = re.split(r'^([0-9]{1,2})', filename) # Replace underscores and dots with spaces. title = re.sub(r'[_\. ]+', ' ', title) # Things in parens seem to confuse Gracenote, so let's strip them out. title = re.sub(r' ?\(.*\)', '', title) # Remove artist name from title if it appears in all of them. if strip_artist and len(files) > 2: title = re.sub(r'(?i)' + artist, '', title) # Remove album title from title if it appears in all of them. if strip_album and len(files) > 2: title = re.sub(r'(?i)' + album, '', title) # Remove any remaining index-, artist-, and album-related cruft from the head of the track title. title = re.sub(r'^[\W\-]+', '', title).strip() # Last chance for artist or album prefix. if not strip_artist and Unicodize( title, language).lower().find(artist.lower()) == 0: title = title[len(artist):] if not strip_album and Unicodize( title, language).lower().find(album.lower()) == 0: title = title[len(album):] t = Media.Track(artist=toBytes(artist), album=toBytes(album), title=toBytes(title), index=int(index)) t.parts.append(f) Log(' - Adding: %s - %s' % (index, title)) query_list.append(t) except Exception as e: Log('Error preparing tracks for quick matching: ' + str(e)) # Otherwise, let's do old school directory crawling and tag reading. else: AudioFiles.Process(path, files, media_list, subdirs, root) query_list = list(media_list) # Try as-is first (ask for everything at once). discs = [query_list] final_match = run_queries(discs, result_list, language, fingerprint, mixed, do_quick_match) # If the match was still shitty, and it looks like we have multiple discs, try splitting. if final_match < 75: discs = group_tracks_by_disc(query_list) if len(discs) > 1: Log('Result still looked bad, we will try splitting into separate per-disc queries.' ) other_result_list = [] other_match = run_queries(discs, other_result_list, language, fingerprint, mixed, do_quick_match) if other_match > final_match: Log('The split result was best, we will use it.') result_list = other_result_list final_match = other_match # If we have a crappy match, don't use it. if final_match < 50.0: Log('That was terrible, let us not use it.') result_list = [] # Finalize the results. used_tags = False del media_list[:] if len(result_list) > 0: # Gracenote results. for result in result_list: media_list.append(result) else: # We bailed during the GN lookup, fall back to tags. used_tags = True AudioFiles.Process(path, files, media_list, subdirs, root) # If we wanted to respect tags, then make sure we used tags. if not used_tags and respect_tags: # Let's grab tag results, and then set GUIDs we found. tag_media_list = [] AudioFiles.Process(path, files, tag_media_list, subdirs, root) # Now suck GN data out. path_map = {} for track in media_list: path_map[track.parts[0]] = track for track in tag_media_list: if track.parts[0] in path_map: gn_track = path_map[track.parts[0]] track.guid = gn_track.guid track.album_guid = gn_track.album_guid track.artist_guid = gn_track.artist_guid track.album_thumb_url = gn_track.album_thumb_url track.artist_thumb_url = gn_track.artist_thumb_url # If the tags failed, fill in key data from Gracenote. if track.album == '[Unknown Album]': track.album = gn_track.album if track.artist == '[Unknown Artist]': track.artist = gn_track.artist media_list[:] = tag_media_list
def Scan(path, files, mediaList, subdirs, language=None, root=None): # Scan for audio files. AudioFiles.Scan(path, files, mediaList, subdirs, root) # Read tags, etc. and build up the mediaList AudioFiles.Process(path, files, mediaList, subdirs, root)