Skip to content

Instantly share code, notes, and snippets.

@joshbarrass
Last active July 20, 2022 02:23
Show Gist options
  • Save joshbarrass/e44226cbcc49636d18eaecb3131bf6f8 to your computer and use it in GitHub Desktop.
Save joshbarrass/e44226cbcc49636d18eaecb3131bf6f8 to your computer and use it in GitHub Desktop.
Python code to generate cue sheets (.cue files) from YouTube timestamps
import re
def pad_number(number,length=2,padding="0"):
str_number = str(number)
if len(str_number) < length:
str_number = padding+str_number
return str_number
def make_cue_tracks(inp,pattern="((\\d{1,2}):)?(\\d{1,2}):(\\d{1,2}) - (.*)",
hr=1,m=2,s=3,title=4,artist=None):
matcher = re.compile(pattern)
output = ""
lines = inp.strip("\n").split("\n")
if len(lines) > 99:
raise ValueError("A cue sheet cannot contain more than 99 tracks!")
for line in range(len(lines)):
lines[line] = lines[line].strip()
str_track = pad_number(line+1)
match = matcher.match(lines[line])
groups = list(match.groups())
if groups[hr] == None: groups[hr] = "00"
output += "\n TRACK {n} AUDIO\n".format(n=str_track)
output += " TITLE \"{title}\"\n".format(title=groups[title])
if isinstance(artist,int):
output += " PERFORMER {artist}\n".format(artist=groups[artist])
output += " INDEX 01 {m}:{s}:00".format(m=pad_number(int(groups[hr])*60+int(groups[m])),s=pad_number(groups[s]))
return output
def make_cue(inp,performer,album,filename,form,rems={},*args,**kwargs):
"""Takes input text, artist, and album name, filename and type to produce a cue sheet. Optionally takes 'pattern' argument with 'hr', 'm', 's', and 'title' arguments to specify new regex patterns and group indices. 'artist' argument specifies artist regex index for tracks that have different artists."""
output = "PERFORMER \"{performer}\"\nTITLE \"{album}\"\n".format(performer=performer,album=album)
for key,item in rems.iteritems():
output+="REM {k} {i}\n".format(k=key,i=item)
output += "FILE \"{f}\" {t}".format(t=form.upper(),f=filename)
output += make_cue_tracks(inp,*args,**kwargs)
return output
@r-a-y
Copy link

r-a-y commented May 25, 2022

Thanks Josh. Your python script is really handy!

I've incorporated changes that TheScienceOtter made here: https://gitlab.com/TheScienceOtter/cuemaker. And have made further changes in my gist fork. Thanks again!

@joshbarrass
Copy link
Author

I'm glad it could be of use to you @r-a-y. Thanks for helping to extend this into something a little more usable than my collection of functions :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment