@@ -105,9 +105,23 @@ def cuestamp_to_sectors(stamp):
105105 fields = int (m .group (3 ))
106106 return fields + (seconds * 75 ) + (minutes * 60 * 75 )
107107
108-
109- def gen_merged_cuesheet (bin_filename , files ):
110- cuesheet = 'FILE "%s" BINARY\n ' % bin_filename
108+ # Generates track filename based on redump naming convention
109+ # (Note: prefix may contain a fully qualified path)
110+ def track_filename (prefix , track_num , track_count ):
111+ # Redump is strangely inconsistent in their datfiles and cuesheets when it
112+ # comes to track numbers. The naming convention currently seems to be:
113+ # If there are less than 10 tracks: "Track 1", "Track 2", etc.
114+ # If there are more than 10 tracks: "Track 01", "Track 02", etc.
115+ #
116+ # It'd be nice if it were consistently %02d!
117+ #
118+ if track_count > 9 :
119+ return "%s (Track %02d).bin" % (prefix , track_num )
120+ return "%s (Track %d).bin" % (prefix , track_num )
121+
122+ # Generates a 'merged' cuesheet, that is, one bin file with tracks indexed within.
123+ def gen_merged_cuesheet (basename , files ):
124+ cuesheet = 'FILE "%s.bin" BINARY\n ' % basename
111125 # One sector is (BLOCKSIZE) bytes
112126 sector_pos = 0
113127 for f in files :
@@ -118,19 +132,21 @@ def gen_merged_cuesheet(bin_filename, files):
118132 sector_pos += f .size / Track .globalBlocksize
119133 return cuesheet
120134
121- def gen_split_cuesheet ( bin_filename , merged_file ):
122- # similar to merged, could have it do both, but separate arguably cleaner
135+ # Generates a 'split' cuesheet, that is, with one bin file for every track.
136+ def gen_split_cuesheet ( basename , merged_file ):
123137 cuesheet = ""
124138 for t in merged_file .tracks :
125- cuesheet += 'FILE "%s (Track %02d).bin" BINARY\n ' % (bin_filename , t .num )
139+ track_fn = track_filename (basename , t .num , len (merged_file .tracks ))
140+ cuesheet += 'FILE "%s" BINARY\n ' % track_fn
126141 cuesheet += ' TRACK %02d %s\n ' % (t .num , t .track_type )
127142 for i in t .indexes :
128143 sector_pos = i ['file_offset' ] - t .indexes [0 ]['file_offset' ]
129144 cuesheet += ' INDEX %02d %s\n ' % (i ['id' ], sectors_to_cuestamp (sector_pos ))
130145 return cuesheet
131146
147+ # Merges files together to new file `merged_filename`, in listed order.
132148def merge_files (merged_filename , files ):
133- # cat is actually faster, but I prefer multi-platform and no special-casing
149+ # cat is actually a bit faster, but this is multi-platform and no special-casing
134150 chunksize = 1024 * 1024
135151 with open (merged_filename , 'wb' ) as outfile :
136152 for f in files :
@@ -142,12 +158,12 @@ def merge_files(merged_filename, files):
142158 outfile .write (chunk )
143159 return True
144160
161+ # Writes each track in a File to a new file
145162def split_files (new_basename , merged_file ):
146- # use calculated sectors, read the same amount, start new file when equal
147163 with open (merged_file .filename , 'rb' ) as infile :
148164 for t in merged_file .tracks :
149165 chunksize = 1024 * 1024
150- out_name = '%s (Track %02d).bin' % ( new_basename , t .num )
166+ out_name = track_filename ( new_basename , t .num , len ( merged_file . tracks ) )
151167 tracksize = t .sectors * Track .globalBlocksize
152168 written = 0
153169 with open (out_name , 'wb' ) as outfile :
@@ -164,17 +180,17 @@ def split_files(new_basename, merged_file):
164180def main ():
165181 parser = argparse .ArgumentParser (description = "Using a cuesheet, merges numerous bin files into a single bin file and produces a new cuesheet with corrected offsets. Works great with Redump. Supports all block modes, but only binary track types. Should work on any python3 platform." )
166182 parser .add_argument ('cuefile' , help = 'path to source cuefile with multiple referenced bin tracks' )
167- parser .add_argument ('new_name ' , help = 'name (without extension) for your new bin/cue files' )
168- parser .add_argument ('--split' , help = 'Change mode from merging to splitting to allow reconstruction of the split format. ' , required = False , action = "store_true" )
183+ parser .add_argument ('basename ' , help = 'name (without extension) for your new bin/cue files' )
184+ parser .add_argument ('--split' , help = 'reverses operation, splitting merged files back to individual tracks ' , required = False , action = "store_true" )
169185 parser .add_argument ('-o' , dest = 'outdir' , required = False , default = False , help = 'output directory. defaults to the same directory as source cue' )
170186 args = parser .parse_args ()
171187
172188
173189 cue_map = read_cue_file (args .cuefile )
174190 if args .split :
175- cuesheet = gen_split_cuesheet (args .new_name , cue_map [0 ])
191+ cuesheet = gen_split_cuesheet (args .basename , cue_map [0 ])
176192 else :
177- cuesheet = gen_merged_cuesheet (args .new_name + '.bin' , cue_map )
193+ cuesheet = gen_merged_cuesheet (args .basename , cue_map )
178194
179195 outdir = os .path .dirname (args .cuefile )
180196 if args .outdir :
@@ -184,20 +200,20 @@ def main():
184200 print ("Output dir does not exist" )
185201 return False
186202
187- with open (os .path .join (outdir , args .new_name + '.cue' ), 'w' , newline = '\r \n ' ) as f :
203+ with open (os .path .join (outdir , args .basename + '.cue' ), 'w' , newline = '\r \n ' ) as f :
188204 f .write (cuesheet )
189- print ("Wrote %s" % args .new_name + '.cue' )
205+ print ("Wrote %s" % args .basename + '.cue' )
190206
191207 if args .split :
192208 print ("Splitting files..." )
193- if split_files (os .path .join (outdir , args .new_name ), cue_map [0 ]):
209+ if split_files (os .path .join (outdir , args .basename ), cue_map [0 ]):
194210 print ("Wrote %d bin files" % len (cue_map [0 ].tracks ))
195211 else :
196212 print ("Unable to split bin files" )
197213 else :
198214 print ("Merging files..." )
199- if merge_files (os .path .join (outdir , args .new_name + '.bin' ), cue_map ):
200- print ("Wrote %s" % args .new_name + '.bin' )
215+ if merge_files (os .path .join (outdir , args .basename + '.bin' ), cue_map ):
216+ print ("Wrote %s" % args .basename + '.bin' )
201217 else :
202218 print ("Unable to merge bin files" )
203219
0 commit comments