Monday, May 4, 2009

a python script to generate cue file

Abstract

A python script to generate cue file from file names in a same folder is given.

Sometimes we have lessless music (ape, flac…) split in to different files, and a cue file may help orgnize them in some music jukebox application, i.e foobar2000. so I create a really silly script to help me…

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os
import sys
import glob
from string import Template
from optparse import OptionParser

cue_head='''REM GENRE $GENRE
REM DATE $DATE
REM DISCID $DISCID
PERFORMER "$PERFORMER"
TITLE "$ALBUMTITLE"'''

cue_file='''
FILE "$FILE" WAVE
TRACK $TRACK AUDIO
TITLE "$TRACKTITLE"
INDEX 01 00:00:00'''

def cue_gen():
"""
generate cue file
"""
parser = OptionParser()
parser.add_option('-d', dest='dir',
type='string')
parser.add_option('-s', dest='sfx',
type='string', default='ape')
options, args = parser.parse_args()

if options.dir is None:
directory = os.getcwd()
else:
directory = options.dir
suffix = options.sfx
print directory, suffix

files = glob.glob(os.path.join(directory, '*.'+suffix))
files = [ff[1] for ff in [os.path.split(f) for f in files]]
files.sort()
for file in files:
print file
begin = int(raw_input('Title offset? '))

genre = raw_input('Genre? ')
date = raw_input('Date? ')
discId = raw_input('DiscId? ')
performer = raw_input('Performer? ')
albumTitle = raw_input('Title? ')
d1 = dict(GENRE = genre,
DATE = date,
DISCID = discId,
PERFORMER = performer,
ALBUMTITLE = albumTitle)
head = Template(cue_head).substitute(d1)
cuepath = os.path.join(directory, (albumTitle+'.cue'))

f = open(cuepath, 'w')
f.write(head)

track = 0
for file in files:
trackTitle = file[begin:-(len(suffix)+1)]
track = track + 1
d2 = dict(FILE = file,
TRACK = track,
TRACKTITLE = trackTitle)
body = Template(cue_file).substitute(d2)
f.write(body)

f.close()

if __name__ == '__main__':
cue_gen()

I know it’s quite rough and a lot of enhancement can be made… maybe it should search for the right file extension automatically or even search the genre for the internet, and that’s just why I put it here, hopefully some of you would help to improve it XD

No comments:

Post a Comment