From 9af85d3d879904c0f19e1d20ccb1523c89e342a7 Mon Sep 17 00:00:00 2001 From: John O'Neil Date: Sun, 19 Oct 2025 00:04:48 -0700 Subject: [PATCH 1/2] Fixes #89: Incorrect file not written message when error encountered in .ts file --- arib/ts2srt.py | 50 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/arib/ts2srt.py b/arib/ts2srt.py index df080ab..9d85e2f 100755 --- a/arib/ts2srt.py +++ b/arib/ts2srt.py @@ -130,8 +130,11 @@ def on_es_packet(self, current_pid, packet, header_size): # ---- driver ---- def run(self) -> int: - if not self.cfg.infile.exists() and not self.cfg.quiet: - print(f"Input filename :{self.cfg.infile} does not exist.") + not_quiet = not self.cfg.quiet + + if not self.cfg.infile.exists(): + if not_quiet: + print(f"Input filename :{self.cfg.infile} does not exist.") return -1 ts = TS(str(self.cfg.infile)) @@ -140,26 +143,49 @@ def run(self) -> int: ts.OnESPacket = self.on_es_packet try: - ts.Parse() + try: + ts.Parse() + except KeyboardInterrupt: + if not_quiet: + print("Interrupted by user.") + return 130 # conventional SIGINT exit finally: # Ensure buffered subtitle data is written to disk even if parsing # encounters errors partway through the file if self.srt: self.srt.finalize() - if self.pid < 0 and not self.cfg.quiet: - print(f"*** Sorry. No ARIB subtitle content was found in file: {self.cfg.infile} ***") + # If we never found a usable PID, report "no ARIB subtitle content". + if self.pid < 0: + if not_quiet: + print( + "*** Sorry. No ARIB subtitle content" + f" was found in file: {self.cfg.infile} ***" + ) return -1 - if self.srt and not self.srt.file_written() and not self.cfg.quiet: + # Success conditions: + # - stdout mode (no file to check) + # - non-empty outfile on disk + # - formatter believes it wrote (fallback) + if self.cfg.output_to_stdout: + return 0 + + try: + if self.cfg.outfile.exists() and self.cfg.outfile.stat().st_size > 0: + return 0 + except OSError: + pass # fall back to formatter flag + + if self.srt and getattr(self.srt, "file_written", None) and self.srt.file_written(): + return 0 + + if not_quiet: print( - "*** Sorry. No nonempty ARIB closed caption content found in file " - + str(self.cfg.infile) - + " ***" + f"*** Sorry. No nonempty ARIB closed caption content found in file " + f"{self.cfg.infile} ***" ) - return -1 - - return 0 + return -1 def parse_args(argv=None) -> Config: From d4b49bdffce6b70b910a747e13e8198ef0ea89a2 Mon Sep 17 00:00:00 2001 From: John O'Neil Date: Sun, 19 Oct 2025 13:12:10 -0700 Subject: [PATCH 2/2] Fixed double try block in ts2srt --- arib/ts2srt.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/arib/ts2srt.py b/arib/ts2srt.py index 9d85e2f..d2cb7ef 100755 --- a/arib/ts2srt.py +++ b/arib/ts2srt.py @@ -143,12 +143,11 @@ def run(self) -> int: ts.OnESPacket = self.on_es_packet try: - try: - ts.Parse() - except KeyboardInterrupt: - if not_quiet: - print("Interrupted by user.") - return 130 # conventional SIGINT exit + ts.Parse() + except KeyboardInterrupt: + if not_quiet: + print("Interrupted by user.") + return 130 # conventional SIGINT exit finally: # Ensure buffered subtitle data is written to disk even if parsing # encounters errors partway through the file