Skip to content

Instantly share code, notes, and snippets.

@ceres-c
Created January 6, 2019 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ceres-c/2cf18362ed7e654aa34d88e8935e05a4 to your computer and use it in GitHub Desktop.
Save ceres-c/2cf18362ed7e654aa34d88e8935e05a4 to your computer and use it in GitHub Desktop.
I needed a script to traverse my music collection and quickly update cuesheets with correct titles. Given all files in my collection are tagged with MusicBrainz archive, the best way to do so is reading flac files tags.
#!/usr/bin/python3
# Author: ceres-c 2019-01-06
# Updates recursively cue files to reflect track's title present in flac files.
import os
from mutagen.flac import FLAC
def cueupdater (path, cuefile):
title = None
newdata = ''
with open(os.path.join(path, cuefile), 'r', encoding="latin-1") as cuef:
for line in cuef:
if line.strip().startswith('FILE'):
audioTags = FLAC(os.path.join(path, line.strip()[6:-6])) # Yep, hacky... I'm removing FILE and WAVE cue strings
title = audioTags['title'][0] # Hacky again...
title = title.replace('"', '')
newdata += line
elif line.strip().startswith('TITLE') and title is not None: # To account for first TITLE occurrence in cue's header
newdata += '\tTITLE "' + title + '"\n'
else:
newdata += line
with open(os.path.join(path, cuefile), 'w') as cuef:
cuef.write(newdata)
for files in os.walk(os.getcwd()):
for file in files:
if '.cue' in file:
cueupdater (root, file_)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment