Skip to content

Commit

Permalink
optimize audio file matching algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
yinyanghu committed Aug 29, 2020
1 parent d8aee3a commit 1a3bdb4
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions cuefix/cuefix.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def convert_newline(self, byte_str, newline='unix'):
NEWLINE_CHAR[newline]
), True

def find_audio_file(self, directory, filename):
def find_audio_file(self, directory, audio_file, cue_file):
audio_files = []
for f in os.listdir(directory):
for ext in AUDIO_FILE_EXTENSION:
Expand All @@ -223,10 +223,11 @@ def find_audio_file(self, directory, filename):
return audio_files[0]

# TODO(yinyanghu): Design a smart matching algorithm.
name = filename.rsplit('.', 1)[0]
for f in audio_files:
if f.startswith(name):
return f
for hint_f in [audio_file, cue_file]:
name = hint_f.rsplit('.', 1)[0]
for f in audio_files:
if f.startswith(name):
return f

raise Exception(
'more than one audio file candidates to be used to fix the cue file: {}', audio_files)
Expand All @@ -245,7 +246,8 @@ def fix_audio_file(self, byte_str, encoding):
if self.verbose:
log.info('cannot find audio file {} in directory {}'.format(
audio_file, dir))
new_audio_file = self.find_audio_file(dir, audio_file)
new_audio_file = self.find_audio_file(
dir, audio_file, self.cue.filename)
if self.verbose:
log.info('found audio file {}'.format(new_audio_file))

Expand Down

0 comments on commit 1a3bdb4

Please sign in to comment.