Kaynağa Gözat

Add inplace flag to handle renaming and backing up the srt file before changes

Breandan Dezendorf 2 yıl önce
ebeveyn
işleme
d73b8714bf

+ 18 - 3
dezendorf/applications/offset-fixer/offset-fixer.py

@@ -50,12 +50,19 @@ def main():
 
   parser = argparse.ArgumentParser()
 
-  parser.add_argument("--seconds", help="Decimal formatted seconds to adjust offsets by", type=float)
-  parser.add_argument("--file", help="Subtitle file to shift", type=str)
+  parser.add_argument("--seconds", help="Decimal formatted seconds to adjust offsets by", type=float, required=True)
+  parser.add_argument("--file", help="Subtitle file to shift", type=str, required=True)
+  parser.add_argument("--inplace", required=False, help="Shift file in place, creating a backup", action='store_true')
   parser.add_argument("--outfile", required=False, help="Subtitle file to shift", type=str)
 
   args = parser.parse_args()
 
+  if args.inplace is True:
+      if args.outfile == None:
+        args.outfile = str(args.file + ".backup")
+      else:
+        sys.exit(1)
+
   if args.outfile == None:
       args.outfile = str(args.file + ".new")
 
@@ -90,7 +97,15 @@ def main():
         #print("Added text:" + line)
         text += line
 
-  print("Writing to file: {}".format(args.outfile))
+  if args.inplace == True:
+    args.outfile = args.file
+    args.file = args.file + ".backup"
+    with open(args.file, 'w') as o:
+      print("Writing backup to " + args.file)
+      for entry in Subtitles:
+        print(entry.formatEntry(), file=o)
+
+  print("Updating subtitle file: " + args.outfile)
   with open(args.outfile, 'w') as o:
     print("Shifting all entries by {} seconds".format(args.seconds))
     for entry in Subtitles: