Skip to content

Commit

Permalink
Merge pull request #12 from thelastfantasy/v1.2.2-remove-debug-info
Browse files Browse the repository at this point in the history
V1.2.2 remove debug info, add more output to command line mode
  • Loading branch information
thelastfantasy authored Oct 20, 2024
2 parents 8c56c5a + e7cbbb9 commit 1d18a32
Showing 1 changed file with 44 additions and 8 deletions.
52 changes: 44 additions & 8 deletions sub_adjust.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import threading
import tkinter as tk
from tkinter import (messagebox, scrolledtext)
from typing import List, Optional
import webbrowser
from docopt import docopt
from loguru import logger
Expand Down Expand Up @@ -174,7 +175,43 @@ def process_ass_ssa_file(filepath, adjusted_shift_value, layers):
if is_cmd_mode:
logger.error(f"错误处理文件 {filepath}: {str(e)}")
return False, str(e)

def shift_times_in_filelist(filelist: Optional[List[str]], shift_value: float, layers, queue):
total_files = len(filelist)
success_count = 0
failure_count = 0
failure_reasons = []

for filepath in filelist:
if filepath.endswith(".ass") or filepath.endswith(".ssa"):
success, reason = process_ass_ssa_file(filepath, shift_value, layers)
elif filepath.endswith(".srt"):
success, reason = process_srt_file(filepath, shift_value)

if success:
success_count += 1
else:
failure_count += 1
failure_reasons.append(f"{filepath}: {reason}")

result_message = (
f"运行完毕。\n"
f"共处理 {total_files} 个文件。\n"
f"成功处理 {success_count} 个文件。\n"
f"失败处理 {failure_count} 个文件。\n"
)

if failure_count > 0:
result_message += "\n失败原因:\n" + "\n".join(failure_reasons)
queue.put(result_message)
else:
queue.put(result_message)

if is_cmd_mode:
log_result_fn = logger.info if success_count > 0 else logger.error
log_result_fn(f"\n\n{result_message}")


def shift_times_in_directory(directory, shift_value, shift_direction, layer_numbers, queue):
if shift_direction is None:
shift_direction = "delay"
Expand Down Expand Up @@ -205,6 +242,7 @@ def shift_times_in_directory(directory, shift_value, shift_direction, layer_numb
failure_reasons.append(f"{file}: {reason}")

result_message = (
f"运行完毕。\n"
f"共处理 {total_files} 个文件。\n"
f"成功处理 {success_count} 个文件。\n"
f"失败处理 {failure_count} 个文件。\n"
Expand All @@ -215,6 +253,10 @@ def shift_times_in_directory(directory, shift_value, shift_direction, layer_numb

queue.put(result_message)

if is_cmd_mode:
log_result_fn = logger.info if success_count > 0 else logger.error
log_result_fn(f"\n\n{result_message}")

def open_mail(event=None):
webbrowser.open("mailto:[email protected]")

Expand Down Expand Up @@ -324,9 +366,8 @@ def check_queue():
def main():
# 使用 options_first=True 确保没有参数时不会直接触发 Usage 输出
args = docopt(USAGE, version=__version__, options_first=True)
print(args)

input_files = args["INPUTS"]
input_files: Optional[List[str]] = args["INPUTS"]

# 如果用户请求版本信息,显示版本信息
if args["--version"]:
Expand All @@ -340,13 +381,8 @@ def main():
logger.info("进入命令行模式...")
shift_value = float(args["--offset"]) * 1000
layer_numbers = parse_layers(args["--layers"])
print(layer_numbers)
if input_files:
for filepath in input_files:
if filepath.endswith(".ass") or filepath.endswith(".ssa"):
process_ass_ssa_file(filepath, shift_value, layer_numbers)
elif filepath.endswith(".srt"):
process_srt_file(filepath, shift_value)
shift_times_in_filelist(input_files, shift_value, layer_numbers, queue.Queue())
else:
shift_times_in_directory(os.getcwd(), shift_value, None, layer_numbers, queue.Queue())

Expand Down

0 comments on commit 1d18a32

Please sign in to comment.