miyouzi / aniGamerPlus

巴哈姆特動畫瘋自動下載工具,支援命令行
GNU General Public License v3.0
677 stars 92 forks source link

简体弹幕 #274

Open fangyuan99 opened 4 months ago

fangyuan99 commented 4 months ago

这个Python脚本将会遍历当前目录及其所有子目录下的.ass字幕文件,将其中的繁体中文转换为简体中文,并覆盖源文件。请确保在执行这个脚本之前备份重要数据,以防不测。

import os
from opencc import OpenCC

def convert_to_simplified(file_path):
    cc = OpenCC('t2s')  # 繁体转简体
    with open(file_path, 'r', encoding='utf-8') as file:
        content = file.read()
    simplified_content = cc.convert(content)
    with open(file_path, 'w', encoding='utf-8') as file:
        file.write(simplified_content)
    print(f"Converted: {file_path}")

def traverse_and_convert(root_dir):
    for root, dirs, files in os.walk(root_dir):
        for file in files:
            if file.endswith('.ass'):
                file_path = os.path.join(root, file)
                convert_to_simplified(file_path)

if __name__ == "__main__":
    traverse_and_convert('.')  # 从当前目录开始

使用说明:

  1. 安装所需库:脚本使用 OpenCC 来进行繁简转换,你需要先安装这个库。可以通过 pip 安装:

    pip install opencc-python-reimplemented
  2. 备份文件:由于该脚本会覆盖原文件,请确保你已经备份了所有重要的 .ass 文件。

  3. 运行脚本:将上述代码保存为 Python 文件(例如 convert_ass.py),在包含 .ass 文件的目录中运行该脚本。确保你的工作目录是正确的,或者修改脚本中的 traverse_and_convert('.') 调用,使其指向正确的目录路径。

脚本运行时将打印出被转换的文件路径,以便跟踪进度。