番剧批量重命名 (Emby 刮削)

发布于 2024-06-22  2927 次阅读


此脚本是 AI 写的,本人对此脚本并无信心,对此脚本造成的影响概不负责 QwQ

一些字幕组的命名有时候会有点奇怪,导致 Emby 无法正常刮削集和季节信息,故面向 AI 编程写了一个批量重命名的程序

代码

Version 1.2 2024_7-17 更新了正则匹配的规则

import os
import re
import shutil

# 匹配集数的正则表达式
episode_regex = r'\[(\d+)\]|(?:S\d+-)?(\d+)'
def rename_files(directory, season, modify_existing):
    # 遍历目录中的所有文件
    for root, _, files in os.walk(directory):
        for file_name in files:
            old_path = os.path.join(root, file_name)
            new_name = file_name

            # 检查文件是否已经是 SxExx 格式开头
            match = re.match(r'^(S\d+E\d+)', file_name)
            if match:
                printf(f"SxExx开头")
                if modify_existing.lower() in ['y', 'yes']:
                    episode = match.group(0)[3:]  # 提取 E 后面的数字
                    new_name = f"S{season}E{episode} {file_name[len(match.group(0)):].strip()}"
                else:
                    print(f"文件 '{file_name}' 已经符合命名规则,跳过重命名。")
                    continue

            # 处理包含集数的文件
            if not match:
                match = re.search(episode_regex, file_name)
                # print(match)
                if match:
                    episode = match.group(1) or match.group(2)
                    new_name = f"S{season}E{episode} {file_name}"
                    print(episode)
                else:
                    print(f"{file_name}未匹配集数正则'{episode_regex}'")



            # 处理 NCOP 和 NCED 文件
            nc_match = re.search(r'(\bNCOP\b|\bNCED\b)(\s*\d*)', file_name)
            if nc_match:
                nc_type = nc_match.group(1)
                episode = nc_match.group(2).strip() or "01"
                new_name = f"{nc_type} {episode} {file_name}"

            # 处理包含集数但不在方括号内的文件
            if not match and not nc_match:
                match = re.search(r'(\d+)\s*\[', file_name)
                if match:
                    episode = match.group(1)
                    new_name = f"S{season}E{episode} {file_name}"

            # 处理 'END' 标签的文件
            if 'END' in file_name:
                match = re.search(r'(\d+)\s*END', file_name)
                if match:
                    episode = match.group(1)
                    new_name = f"S{season}E{episode} END {file_name}"

            # 如果新名称与旧名称不同,则重命名文件
            new_path = os.path.join(root, new_name)
            if old_path != new_path:
                shutil.move(old_path, new_path)
                print(f"重命名 '{file_name}' 为 '{new_name}'")
            # else:
                # print(f"'{file_name}'无需重命名")

if __name__ == "__main__":
    # 获取当前目录
    current_dir = os.getcwd()

    # 询问用户当前季度
    season = input("请输入当前季度(例如:1 或 2): ")
    if not season.isdigit():
        print("输入的季度不是数字,请输入正确的季度。")
        exit(1)

    # 询问用户是否要批量修改已经符合 SxExx 格式的文件
    modify_existing = input("是否要批量修改已经符合 SxExx 格式的文件?(y/n): ")

    # 调用函数重命名文件
    rename_files(current_dir, season, modify_existing)

    print("文件重命名完成!")
   旧的版本  ==>  展开 / 收起

使用方式

安装 Python3 (apt install python3)直接在文件夹使用或者设置 alias , 将 alias renameVideo="python3 /path/to/rename.py" 加入你终端的 rc 文件( ~/.bashrc~/.zshrc ), 之后在对应的文件夹运行 renameVideo ,此脚本会自动将 文件名转为 S1E01 开头或者 NCOP 和 NCED 开头。

由于是 AI 写的,对此脚本并无信心,对此脚本造成的影响概不负责 QwQ (前后提示)(宇宙级安全声明)
一个敢于为了自己梦想而奋斗的人
最后更新于 2024-07-17