# HG changeset patch # User mdd # Date 1511536610 -3600 # Node ID 92409c985e0d71312b8ef1b06f46fb83ecbc7049 # Parent 0d021d47eca5023c806c6953a1c146f638364028 added autocrop and 720p rescaler diff -r 0d021d47eca5 -r 92409c985e0d ts2mkv.py --- a/ts2mkv.py Fri Nov 24 02:12:25 2017 +0100 +++ b/ts2mkv.py Fri Nov 24 16:16:50 2017 +0100 @@ -30,16 +30,18 @@ self.msg_ffmpeg = "" self.command = None + self.scaleto_720p = True + self.video_options = [ "-c:v libx264", - "-preset slow", - "-crf 21" + "-preset faster", # slow + "-tune film", # film / animation + "-crf 19" # 21, better 19 ] self.audio_options = [ "-c:a copy", ] - def get_stream_index(self, data): idx = data.find("Stream #") if idx == -1: @@ -55,15 +57,40 @@ # http://www.omdbapi.com/apikey.aspx self.msg_eit = readeit(os.path.splitext(filename)[0] + ".eit") + def get_crop_option(self): + lines = filter_lines(self.msg_ffmpeg, "[Parsed_cropdetect").split("\n") + option = None + for line in lines: + tmp = line[line.find(" crop="):].strip() + #print "DEBUG: " + tmp + if not option: + option = tmp + else: + if option != tmp: + self.msg_prepare += "WARNING: cropdetect inconsistent over scan time, disabling autocrop\n" + return None + self.msg_prepare += "Crop detected: %s\n" % option + return option + def get_ffmpeg_command(self, filename): commands = [] fn = "\\'".join(p for p in filename.split("'")) - p = subprocess.Popen(["ffmpeg", "-i", fn], - stdout=subprocess.PIPE, stderr=subprocess.PIPE) + # ffmpeg -ss 00:05:00 -t 2 -i testfiles/chappie.ts -vf "cropdetect=24:16:0" -f null - + + cmd = ["ffmpeg", + "-ss 00:05:00", "-t 1", # search to 5 minutes, analyze 1 second + "-i %s" % fn, + "-vf \"cropdetect=24:2:0\"", # detect black bar crop on top and bottom + "-f null", "-" # no output file + ] + print " ".join(cmd) + p = subprocess.Popen(shlex.split(" ".join(cmd)), stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate() + print "Command return code: ", p.poll() self.msg_ffmpeg = out + "\n" + err self.msg_ffmpeg = self.msg_ffmpeg[self.msg_ffmpeg.find("Input #0"):] + # find "Stream #0:" lines info = filter_lines(self.msg_ffmpeg, "Stream #0:") @@ -107,12 +134,23 @@ audiomap.append(a) + self.msg_prepare += "Video Stream selected: Stream #%s\n" % v cmd = [ "ffmpeg", "-i %s" % fn, "-map %s" % v, ] + flt = [] + crop = self.get_crop_option() + if crop: + flt.append(crop) + if self.scaleto_720p: + flt.append("scale='min(1280,iw)':-2'") # -2 ensures division by two for codec + self.msg_prepare += "Scaling cropped output stream to 720p\n" + if len(flt) > 0: + # append video filters + cmd.append('-filter:v "%s"' % ",".join(flt)) for a in audiomap: self.msg_prepare += "Audio Stream selected: Stream #%s\n" % a cmd.append("-map %s" % a) @@ -133,7 +171,8 @@ os.system('cls' if os.name == 'nt' else 'clear') # TODO: get file from commandline - filename = "testfiles/chappie.ts" + #filename = "testfiles/chappie.ts" + filename = "/srv/storage0/DREAMBOX/Science Fiction/THE_ISLAND.ts" mkv = ts2mkv() mkv.load(filename) @@ -147,5 +186,5 @@ print mkv.msg_ffmpeg for cmd in mkv.command: - print "Executing ffmpeg:\n%s\n" % " ".join(cmd) + print "Executing ffmpeg:\n%s\n" % cmd run_command(cmd)