# HG changeset patch # User mdd # Date 1513038323 -3600 # Node ID a2951f7c435eeadb9a40a696547876739a2c8e73 # Parent 361f76cd60eeb80df02134732854c093d20f47fc added dryrun for testing, option to move processed files to another location diff -r 361f76cd60ee -r a2951f7c435e ts2mkv.py --- a/ts2mkv.py Mon Dec 11 22:45:18 2017 +0100 +++ b/ts2mkv.py Tue Dec 12 01:25:23 2017 +0100 @@ -93,6 +93,8 @@ unknown_line = thread.match.group(0) sys.stdout.write(unknown_line) sys.stdout.flush() + thread.close() + return thread.exitstatus def ffmpeg_filename(filename): """ @@ -458,18 +460,22 @@ return None if not self.info["msg_eit"]: self.info["msg_eit"] = "No EIT file found, sorry - no description" - fd = open(self.outfilebase + ".txt", "wb") - fd.write(self.info["msg_eit"]) - fd.write("\n\n# ---DEBUG---\n\n") - fd.write(self.info["msg_prepare"]) - fd.write(self.info["msg_ffmpeg"]) - fd.close() + if not self.config["dryrun"]: + fd = open(self.outfilebase + ".txt", "wb") + fd.write(self.info["msg_eit"]) + fd.write("\n\n# ---DEBUG---\n\n") + fd.write(self.info["msg_prepare"]) + fd.write(self.info["msg_ffmpeg"]) + fd.close() #print self.info["msg_ffmpeg"] for cmd in self.command: print "Executing ffmpeg:\n%s\n" % cmd #return run_command(cmd, self.total_frames) - return run_ffmpeg_watch(cmd, frames_total=self.info["frames_total"]) + if not self.config["dryrun"]: + return run_ffmpeg_watch(cmd, frames_total=self.info["frames_total"]) + else: + return 0 @@ -484,14 +490,18 @@ help='ffmpeg tune preset [film, animation] (default is film)') parser.add_argument('--ns', action='store_true', default=False, \ help='no rescaling (default is scale to 720p)') - parser.add_argument('--rename', action='store_true', default=False, \ - help='rename file basename to name and genre from EIT file if present') - parser.add_argument('input', metavar='input', nargs='+', \ - help='one or more files, glob style syntax') parser.add_argument('-f', action='store_true', default=False, \ help='force overwrite of existing file') parser.add_argument('--fa', action='store_true', default=False, \ - help='use first audio stream found') + help='force use first audio stream') + parser.add_argument('--rename', action='store_true', default=False, \ + help='rename file basename to name and genre from EIT file if present') + parser.add_argument('--moveto', default='', \ + help='specify base directory to move processed files to') + parser.add_argument('--dryrun', action='store_true', default=False, \ + help='Dry-run, dont touch anything') + parser.add_argument('input', metavar='input', nargs='+', \ + help='one or more files, glob style syntax') args = parser.parse_args() processor = ts2mkv(crf=args.crf, tune=args.tune) @@ -499,6 +509,7 @@ processor.config["rename"] = args.rename processor.config["overwrite"] = args.f processor.config["firstaudio"] = args.fa + processor.config["dryrun"] = args.dryrun src = [] for srcstr in args.input: @@ -507,7 +518,30 @@ for srcfile in src: print "\nProcessing file %i/%i: %s" % (idx, len(src), srcfile) processor.load(srcfile) - processor.convert() + exitcode = processor.convert() + if exitcode == 0: + print "Successful conversion." + if args.moveto: + mvlist = glob.glob(os.path.splitext(srcfile)[0] + ".*") + mvtarget = os.path.dirname(srcfile).replace('../', '') + mvtarget = os.path.join( + args.moveto, mvtarget) + mvsource = os.path.dirname(srcfile) + print "Moving processed files from %s to %s" % ( + mvsource, mvtarget) + if not args.dryrun: + os.makedirs(mvtarget) + for mvsrc in mvlist: + mvfn = os.path.basename(mvsrc) + if os.path.splitext(mvfn)[1] in ['.txt', '.mkv', '.nfo']: + continue + print mvfn + if not args.dryrun: + os.rename( + os.path.join(mvsource, mvfn), + os.path.join(mvtarget, mvfn)) + else: + print "ERROR while executing ffmpeg!" idx += 1