| 18 | | class StreamInfo(object): |
| 19 | | def __init__(self, filename, txt_filter, parameter=[]): |
| 20 | | self.filename = filename |
| 21 | | self.txt_filter = txt_filter |
| 22 | | self.parameter = parameter |
| | 22 | VIDEO_EXT = ".mkv" # ToDo: Should go into the config |
| | 23 | |
| | 24 | STREAMINFOS = [ |
| | 25 | { |
| | 26 | "txt_filter": ("Chapters",), |
| | 27 | "ext": ".txt", |
| | 28 | }, |
| | 29 | { |
| | 30 | "txt_filter": ("h264", "VC-1", "MPEG2",), |
| | 31 | "ext": VIDEO_EXT |
| | 32 | }, |
| | 33 | { |
| | 34 | "txt_filter": ("DTS Hi-Res",), |
| | 35 | "ext": ".dtshr", |
| | 36 | }, |
| | 37 | { |
| | 38 | "txt_filter": ("DTS",), |
| | 39 | "ext": ".dts", |
| | 40 | }, |
| | 41 | { |
| | 42 | "txt_filter": ("Subtitle",), |
| | 43 | "ext": ".sup", |
| | 44 | }, |
| | 45 | ] |
| | 46 | |
| | 47 | |
| | 48 | |
| | 49 | class Drive(dict): |
| | 50 | def __init__(self, driveletter, cfg): |
| | 51 | self["letter"] = driveletter |
| | 52 | self.cfg = cfg |
| | 53 | |
| | 54 | self["vol_info"] = None |
| | 55 | self["lable"] = None |
| | 56 | |
| | 57 | self["stream_dir"] = os.path.join(driveletter, cfg["stream_dir"]) |
| | 58 | |
| | 59 | def set_vol_info(self, vol_info): |
| | 60 | self["vol_info"] = vol_info |
| | 61 | self["lable"] = vol_info[0] |
| | 62 | |
| | 63 | def debug(self): |
| | 64 | print "_"*79 |
| | 65 | print "Drive debug:" |
| | 66 | pprint(self) |
| | 67 | print "-"*79 |
| 25 | | return "<StreamInfo %s (%s)>" % ( |
| 26 | | self.filename, self.parameter |
| | 70 | return "<Drive '%s'>" % self["letter"] |
| | 71 | # def __repr__(self): |
| | 72 | # return self.__str__() |
| | 73 | |
| | 74 | |
| | 75 | |
| | 76 | |
| | 77 | |
| | 78 | |
| | 79 | |
| | 80 | |
| | 81 | class VideoFile(dict): |
| | 82 | def __init__(self, name, drive, abs_path, stat, cfg): |
| | 83 | self["name"] = name # Filename |
| | 84 | |
| | 85 | self["fn"], self.ext = os.path.splitext(self["name"]) |
| | 86 | |
| | 87 | self["drive"] = drive # class "Drive" |
| | 88 | self["abs_path"] = abs_path # File path + filename |
| | 89 | self["stat"] = stat # os.stat |
| | 90 | |
| | 91 | self.cfg = cfg |
| | 92 | |
| | 93 | self["out_path"] = os.path.join(cfg["out_dir"], drive["lable"]) |
| | 94 | |
| | 95 | self["name_prefix"] = self["name"].replace(".", "_") |
| | 96 | |
| | 97 | self["eac3to_txt_path"] = os.path.join( |
| | 98 | self["out_path"], |
| | 99 | "%s_eac3to.txt" % self["name_prefix"] |
| 28 | | def __repr__(self): |
| 29 | | return self.__str__() |
| 30 | | |
| 31 | | |
| 32 | | VIDEO_EXT = "mkv" # ToDo: Should go into the config |
| 33 | | |
| 34 | | STREAMINFOS = [ |
| 35 | | StreamInfo( |
| 36 | | filename = "chapters.txt", |
| 37 | | txt_filter = ("Chapters",), |
| 38 | | ), |
| 39 | | StreamInfo( |
| 40 | | filename = "h264."+VIDEO_EXT, |
| 41 | | txt_filter = ("h264",), |
| 42 | | ), |
| 43 | | StreamInfo( |
| 44 | | filename = "VC1."+VIDEO_EXT, |
| 45 | | txt_filter = ("VC-1",), |
| 46 | | ), |
| 47 | | StreamInfo( |
| 48 | | filename = "MPEG2."+VIDEO_EXT, |
| 49 | | txt_filter = ("MPEG2",), |
| 50 | | ), |
| 51 | | StreamInfo( |
| 52 | | filename = "ger 384KBits.ac3", |
| 53 | | txt_filter = ("German",), |
| 54 | | parameter = ["-384", "-down6", "-quality=4"], |
| 55 | | ), |
| 56 | | ] |
| 57 | | |
| 58 | | |
| 59 | | |
| 60 | | class Drive(object): |
| 61 | | def __init__(self, driveletter, cfg): |
| 62 | | self.letter = driveletter |
| 63 | | self.cfg = cfg |
| 64 | | |
| 65 | | self.vol_info = None |
| 66 | | self.lable = None |
| 67 | | |
| 68 | | self.stream_dir = os.path.join(driveletter, cfg["stream_dir"]) |
| 69 | | |
| 70 | | def set_vol_info(self, vol_info): |
| 71 | | self.vol_info = vol_info |
| 72 | | self.lable = vol_info[0] |
| 73 | | |
| 74 | | def __str__(self): |
| 75 | | return "<Drive '%s'>" % self.letter |
| 76 | | def __repr__(self): |
| 77 | | return self.__str__() |
| 78 | | |
| 79 | | |
| 80 | | class Files(list): |
| 81 | | def print_info(self): |
| 82 | | for file in self: |
| 83 | | print file.abs_path, |
| 84 | | size = file.stat.st_size/1024.0/1024.0/1024.0 |
| 85 | | print "%.1f GB" % size |
| 86 | | print "output path....:", file.output_path |
| 87 | | print "log file path..:", file.log_file_path |
| 88 | | print |
| 89 | | |
| 90 | | |
| 91 | | |
| 92 | | |
| 93 | | |
| 94 | | |
| 95 | | |
| 96 | | |
| 97 | | |
| 98 | | |
| 99 | | class VideoFile(object): |
| 100 | | def __init__(self, name, drive, abs_path, stat, cfg): |
| 101 | | self.name = name # Filename |
| 102 | | |
| 103 | | self.fn, self.ext = os.path.splitext(self.name) |
| 104 | | |
| 105 | | self.drive = drive # class "Drive" |
| 106 | | self.abs_path = abs_path # File path + filename |
| 107 | | self.stat = stat # os.stat |
| 108 | | |
| 109 | | self.cfg = cfg |
| 110 | | |
| 111 | | self.output_path = os.path.join(cfg["out_dir"], drive.lable) |
| 112 | | |
| 113 | | log_fn = "%s.log" % self.name.replace(".", "_") |
| 114 | | self.log_file_path = os.path.join(self.output_path, log_fn) |
| 115 | | |
| 116 | | self.streams = None # set in self.parse_streaminfo() |
| | 101 | |
| | 102 | self["log_file_path"] = os.path.join( |
| | 103 | self["out_path"], |
| | 104 | "%s.log" % self["name_prefix"] |
| | 105 | ) |
| | 106 | self.log_file = file(self["log_file_path"], "a") |
| | 107 | self.log("Start logging") |
| | 108 | |
| | 109 | self["streams"] = None # set in self.parse_streaminfo() |
| 142 | | def get_command(self): |
| 143 | | cmds = [self.cfg["eac3to"], self.abs_path] |
| 144 | | for no, stream in self.streams: |
| 145 | | #~ print stream |
| 146 | | out_name = "%s_%s" % (self.fn, stream.filename) |
| 147 | | out_fn = os.path.join(self.output_path, out_name) |
| 148 | | cmds.append("%s: %s" % (no, out_fn)) |
| 149 | | |
| 150 | | if stream.parameter: |
| 151 | | cmds += stream.parameter |
| 152 | | |
| 153 | | return " ".join(cmds) |
| 154 | | |
| 155 | | def parse_line(self, no, info): |
| 156 | | def in_list(txt, l): |
| 157 | | for item in l: |
| 158 | | if item in txt: |
| 159 | | return True |
| 160 | | return False |
| 161 | | |
| 162 | | for stream_info in STREAMINFOS: |
| 163 | | if in_list(info, stream_info.txt_filter): |
| 164 | | self.streams.append([no, stream_info]) |
| | 128 | def get_command(self, stream_selection): |
| | 129 | cmd = [self.cfg["eac3to"], self["abs_path"]] |
| | 130 | |
| | 131 | value_dict = {} |
| | 132 | for k,v in self["streams"].iteritems(): |
| | 133 | # print k,v |
| | 134 | if v not in value_dict: |
| | 135 | value_dict[v] = [] |
| | 136 | |
| | 137 | value_dict[v].append(k) |
| | 138 | |
| | 139 | # pprint(value_dict) |
| | 140 | |
| | 141 | def get_file_ext(stream_txt): |
| | 142 | def in_list(txt, l): |
| | 143 | for item in l: |
| | 144 | if item in txt: |
| | 145 | return True |
| | 146 | return False |
| | 147 | |
| | 148 | for stream_info in STREAMINFOS: |
| | 149 | if in_list(stream_txt, stream_info["txt_filter"]): |
| | 150 | return stream_info["ext"] |
| | 151 | |
| | 152 | raise RuntimeError( |
| | 153 | "No STREAMINFOS entry matched for '%s'" % stream_txt |
| | 154 | ) |
| | 155 | |
| | 156 | selected_streams = {} |
| | 157 | for stream_txt in stream_selection: |
| | 158 | try: |
| | 159 | ids = value_dict[stream_txt] |
| | 160 | except KeyError: |
| | 161 | print "Stream '%s' doesn't exist. Skip file." % stream_txt |
| 185 | | cmd = [self.cfg["eac3to"], self.abs_path] |
| 186 | | print "run '%s'..." % " ".join(cmd) |
| 187 | | process, output = subprocess2(cmd) |
| 188 | | return process, output |
| | 201 | """ |
| | 202 | Run eac3to and cache the output data. |
| | 203 | """ |
| | 204 | if os.path.isfile(self["eac3to_txt_path"]): |
| | 205 | print "Use existing eac3to output from cache file '%s'..." % ( |
| | 206 | self["eac3to_txt_path"] |
| | 207 | ) |
| | 208 | f = file(self["eac3to_txt_path"], "r") |
| | 209 | output = f.read() |
| | 210 | f.close() |
| | 211 | else: |
| | 212 | # run eac3to and save the output data into self["eac3to_txt_path"] |
| | 213 | cmd = [self.cfg["eac3to"], self["abs_path"]] |
| | 214 | print "run '%s'..." % " ".join(cmd) |
| | 215 | process, output = subprocess2(cmd) |
| | 216 | |
| | 217 | f = file(self["eac3to_txt_path"], "w") |
| | 218 | f.write(output) |
| | 219 | f.close() |
| | 220 | |
| | 221 | return output |
| | 264 | def choose_drive(cfg): |
| | 265 | """ |
| | 266 | Choose a stream dir manuely. |
| | 267 | """ |
| | 268 | path = askopenfilename2( |
| | 269 | title = "Choose the stream dir:", |
| | 270 | initialdir = cfg["last sourcedir"], |
| | 271 | filetypes = [('M2TS File','*.m2ts')], |
| | 272 | ) |
| | 273 | |
| | 274 | vol_info = path.replace("\\", "_").replace(":","_") # Fallback |
| | 275 | for part in reversed(path.split(os.sep)[:-1]): |
| | 276 | # Use the last part of the path as a vol_info |
| | 277 | if part.upper() not in ("BDMV", "STREAM"): |
| | 278 | vol_info = part |
| | 279 | break |
| | 280 | |
| | 281 | drive = Drive(os.path.splitdrive(path)[0], cfg) |
| | 282 | drive["stream_dir"] = os.path.split(path)[0] |
| | 283 | drive.set_vol_info((vol_info,)) |
| | 284 | |
| | 285 | drive.debug() |
| | 286 | |
| | 287 | drives = [drive,] |
| | 288 | |
| | 289 | cfg["last sourcedir"] = path |
| | 290 | |
| | 291 | return drives |
| | 292 | |
| | 293 | |
| | 294 | #------------------------------------------------------------------------------ |
| | 295 | |
| 238 | | for fn in sorted(os.listdir(path)): |
| 239 | | abs_path = os.path.join(path, fn) |
| | 307 | glob_path = os.path.join(path, cfg["glob"]) |
| | 308 | print "Looking in", glob_path |
| | 309 | file_list = glob.glob(glob_path) |
| | 310 | print "Files found:", file_list |
| | 311 | for abs_path in sorted(file_list): |
| | 312 | filename = os.path.basename(abs_path) |
| | 313 | |
| 241 | | size = stat.st_size |
| 242 | | if size<cfg["skip_size"]: |
| 243 | | # Skip small files |
| 244 | | continue |
| 245 | | |
| 246 | | f = VideoFile(fn, drive, abs_path, stat, cfg) |
| | 315 | |
| | 316 | # print (filename, drive, abs_path, stat, cfg) |
| | 317 | f = VideoFile(filename, drive, abs_path, stat, cfg) |
| | 336 | def select_videofiles(videofiles): |
| | 337 | """ |
| | 338 | Select witch founded files should be recordnized. |
| | 339 | """ |
| | 340 | activated = [] |
| | 341 | item_list = [] |
| | 342 | for index, videofile in enumerate(videofiles): |
| | 343 | filesize = videofile["stat"].st_size |
| | 344 | |
| | 345 | line = "%s - %s" % (videofile["abs_path"], human_filesize(filesize)) |
| | 346 | item_list.append(line) |
| | 347 | |
| | 348 | if filesize>cfg["skip_size"]: |
| | 349 | activated.append(index) |
| | 350 | |
| | 351 | # size = stat.st_size |
| | 352 | # if size<cfg["skip_size"]: |
| | 353 | # # Skip small files |
| | 354 | # continue |
| | 355 | |
| | 356 | lb = TkListbox( |
| | 357 | title = "Please select", |
| | 358 | lable = "Please select files witch should be converted:", |
| | 359 | items = item_list, |
| | 360 | activated = activated |
| | 361 | ) |
| | 362 | print "selected items:" |
| | 363 | pprint(lb.selection) # list of selected items. |
| | 364 | |
| | 365 | curselection = lb.curselection # tuple containing index of selected items |
| | 366 | print "curselection:", curselection |
| | 367 | |
| | 368 | new_list = [ |
| | 369 | item |
| | 370 | for index, item in enumerate(videofiles) |
| | 371 | if index in curselection |
| | 372 | ] |
| | 373 | |
| | 374 | return new_list |
| | 375 | |
| | 376 | |
| | 377 | |
| | 378 | |
| 296 | | |
| | 414 | |
| | 415 | def select_streams(videofiles): |
| | 416 | """ |
| | 417 | select witch streams should be convert. |
| | 418 | """ |
| | 419 | print "*"*79 |
| | 420 | streams_txt = [] |
| | 421 | for videofile in videofiles: |
| | 422 | # videofile.debug() |
| | 423 | for stream_txt in videofile["streams"].values(): |
| | 424 | if not stream_txt in streams_txt: |
| | 425 | streams_txt.append(stream_txt) |
| | 426 | # |
| | 427 | # lb = TkListbox( |
| | 428 | # title = "Please select", |
| | 429 | # lable = "Please select streams:", |
| | 430 | # items = streams_txt |
| | 431 | # ) |
| | 432 | # print lb.selection |
| | 433 | |
| | 434 | selection = TkListbox( |
| | 435 | title = "Please select", |
| | 436 | lable = "Please select streams:", |
| | 437 | items = streams_txt |
| | 438 | ).selection |
| | 439 | |
| | 440 | return selection |
| | 441 | |
| | 442 | def convert_streams(videofile, stream_selection): |
| | 443 | print "convert_streams():", videofile, stream_selection |
| | 444 | videofile.debug() |
| | 445 | |
| | 446 | |
| 315 | | print "No drives with stream files found -> abort, ok." |
| 316 | | sys.exit() |
| 317 | | |
| 318 | | files = get_stream_files(drives, cfg) |
| 319 | | files.print_info() |
| 320 | | |
| 321 | | if not files: |
| | 465 | print "No drives with stream files found -> choose manuel" |
| | 466 | drives = choose_drive(cfg) |
| | 467 | |
| | 468 | # Get from all path all m2ts files |
| | 469 | videofiles = get_stream_files(drives, cfg) |
| | 470 | |
| | 471 | # Select via Tk the files witch realy convert |
| | 472 | videofiles = select_videofiles(videofiles) |
| | 473 | |
| | 474 | if not videofiles: |