Files
SceneMarshal/scripts/media-rename
T
ali c278f2f53f feat(specials): 新增 specials 模式,按播出日期计算并写入特典放置信息
- 规则:特辑日期 < 该季首集 → 插到第 1 集前;>= 末集 → 整季之后;
  其它 → 插到「日期严格晚于它」的那一集之前(同日播出的特辑排在正片之后)
- 季归属优先取特辑名里的「第N季」,否则按日期找最近开播的一季
- 写入 episode nfo 的 airsbefore_season / airsbefore_episode / airsafter_season
- 写入失败(权限)不再中断整批,单独计数
- scripts/media-rename 支持 MEDIA_UID/MEDIA_GID,可切 root 写 Jellyfin 生成的 nfo
- 实测:11 部剧写入成功(现在就出发 75、你好星期六 198、花儿与少年 70…)

已知:Jellyfin 12.4 目前未读入这些字段(NFO 已写、API 刷新不生效),待进一步确认存储位置
2026-11-20 01:52:11 +08:00

107 lines
3.2 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# media-rename —— 宿主机代理,把参数透传进 python 容器执行 SceneMarshal
#
# 用法:
# media-rename # 处理当前目录
# media-rename auto # 指定 main.py 模式
# media-rename -p "/hdd/sda1/Quark-Down/苍兰诀 (2022)"
# media-rename -p 某目录 extract-subs
# media-rename -h # 透传 main.py 帮助
#
# 说明:
# - 未给 -p/--path 且不是 -h 时,默认取当前目录;
# - 路径会 realpath 展开,并限制在容器已挂载的范围内;
# - 有 TTY 时使用 docker exec -it(可交互确认/输入标题年份),无 TTY 时自动降级。
set -euo pipefail
CONTAINER="${MEDIA_RENAME_CONTAINER:-python}"
PYTHON_BIN="/python/media/bin/python"
SCRIPT="/python/media/src/main.py"
ALLOWED_PREFIXES=(/hdd/sda1 /python)
die() { printf '\033[0;31m❌ %s\033[0m\n' "$*" >&2; exit 1; }
args=()
path_found=0
target_path=""
want_help=0
i=1
while (( i <= $# )); do
arg="${!i}"
case "$arg" in
-h|--help)
want_help=1
args+=("$arg")
;;
-p|--path)
args+=("$arg")
path_found=1
i=$((i + 1))
(( i <= $# )) || die "$arg 后面缺少路径参数"
raw="${!i}"
target_path="$(realpath -e -- "$raw" 2>/dev/null)" || die "路径不存在或无法解析: $raw"
args+=("$target_path")
;;
*)
args+=("$arg")
;;
esac
i=$((i + 1))
done
if (( ! path_found && ! want_help )); then
target_path="$(pwd)"
args+=("-p" "$target_path")
fi
if [[ -n "$target_path" ]]; then
allowed=0
for prefix in "${ALLOWED_PREFIXES[@]}"; do
if [[ "$target_path" == "$prefix" || "$target_path" == "$prefix"/* ]]; then
allowed=1
break
fi
done
(( allowed )) || die "路径越界: $target_path 未挂载给容器(仅允许 ${ALLOWED_PREFIXES[*]})"
fi
exec_opts=(-u "${MEDIA_UID:-1000}:${MEDIA_GID:-1000}")
if [[ -t 0 && -t 1 ]]; then
exec_opts+=(-it)
fi
# docker exec 的客户端一旦断开,容器里的进程不会自动退出(会变孤儿继续跑)。
# 所以这里自己兜底:SSH 会话被挂断(SIGHUP)或收到 TERM/INT 时,
# 顺手把容器内同一目标的进程结束掉。
cleanup_container() {
# 容器是精简镜像,没有 pkill/pgrep,只能扫 /proc 自己找
[[ -n "$target_path" ]] || return 0 # 没有明确目标就不乱杀
pattern="$SCRIPT -p $target_path"
docker exec -u 1000:1000 "$CONTAINER" sh -c '
me=$$
pat="$1"
for p in /proc/[0-9]*; do
pid=${p#/proc/}
[ "$pid" = "$me" ] && continue
[ -r "$p/cmdline" ] || continue
c=$(tr "\0" " " < "$p/cmdline" 2>/dev/null) || continue
case "$c" in
*"$pat"*) kill -TERM "$pid" 2>/dev/null || true ;;
esac
done
' sh "$pattern" >/dev/null 2>&1 || true
}
trap 'cleanup_container; exit 129' HUP
trap 'cleanup_container; exit 130' INT
trap 'cleanup_container; exit 143' TERM
set +e
docker exec "${exec_opts[@]}" "$CONTAINER" "$PYTHON_BIN" "$SCRIPT" "${args[@]}"
code=$?
set -e
trap - HUP INT TERM
exit "$code"