transfer

package
v1.0.0-rc.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package transfer 是整理入库的文件操作:从种子的文件里挑出要整理的视频与字幕、按模板算出库里的路径, 再用硬链接、复制、软链接或移动放进媒体库。不访问网络,也不读写数据库。

Index

Constants

View Source
const (
	DefaultMovieTemplate = `{{.Title}}{{if .Year}} ({{.Year}}){{end}}/{{.Title}}{{if .Year}} ({{.Year}}){{end}}{{if .Quality}} - {{.Quality}}{{end}}`
	DefaultTVTemplate    = `` /* 139-byte string literal not displayed */
)

默认命名模板(Emby、Jellyfin、Plex 都认):每部电影、每部剧集一个目录,剧集再按季分目录。模板不写扩展名。

View Source
const DefaultMinVideoBytes = 50 << 20

DefaultMinVideoBytes 是视频文件的默认最小体积:更小的当作样片或花絮。

Variables

View Source
var (
	SampleMovie = Vars{
		Title: "奥本海默", OriginalTitle: "Oppenheimer", Year: 2023, TMDBID: 872585, IMDbID: "tt15398776",
		Resolution: "2160p", Source: "BluRay", VideoCodec: "H.265", HDR: "HDR10", Audio: "DTS-HD MA", Group: "FRDS",
		Quality: "2160p BluRay HDR10 H.265",
	}
	SampleEpisode = Vars{
		Title: "最后生还者", OriginalTitle: "The Last of Us", Year: 2023, TMDBID: 100088, IMDbID: "tt3581920",
		Season: 1, Episode: 2, SeasonEpisode: "S01E02", EpisodeTitle: "感染",
		Resolution: "2160p", Source: "WEB-DL", VideoCodec: "H.265", HDR: "DV", Group: "HHWEB",
		Quality: "2160p WEB-DL DV H.265",
	}
)

SampleMovie 与 SampleEpisode 是检查模板与界面预览用的示例。

View Source
var (
	// ErrCrossDevice 表示源文件与媒体库不在同一个文件系统(硬链接做不了)。
	ErrCrossDevice = errors.New("源文件与媒体库不在同一个文件系统,不能硬链接:把下载目录和媒体库放在同一个分区(Docker 里挂成同一个卷),或改用复制")
	// ErrTargetExists 表示目标位置已经有一个不同的文件(洗版之外不替换)。
	ErrTargetExists = errors.New("目标位置已经有同名文件(不是同一个文件),没有覆盖")
	// ErrSourceMissing 表示源文件不存在。
	ErrSourceMissing = errors.New("源文件不存在")
	// ErrBadMode 表示整理方式不认识。
	ErrBadMode = errors.New("整理方式无效(可选 hardlink、copy、symlink、move)")
	// ErrNotOurs 表示库里的文件已经不是当初整理出的那个(换成了别的文件、不是软链接),没有删除。
	ErrNotOurs = errors.New("已经不是当初整理出的文件")
)
View Source
var (
	// ErrTemplate 表示模板写错了或渲染出的路径不能用(错误信息写明原因)。
	ErrTemplate = errors.New("命名模板不能用")
)

Functions

func Check

func Check(tpl, kind string) (string, error)

Check 用示例渲染一次模板,报告能不能用;返回示例渲染的结果给界面看。

func CleanName

func CleanName(s string) string

CleanName 把一段文字变成能放进路径的名字:去掉 \ / * ? " < > | 与控制字符,冒号换成「 - 」,合并空格,去掉首尾的空格与点。

func FileID

func FileID(path string) (string, error)

FileID 是文件所在设备、inode 编号、大小与修改时间(同一个文件的硬链接相同)。软链接取链接本身。 带上大小与修改时间:文件删掉后 inode 编号很快会被新文件重用,只比 inode 会把换掉的文件当成原来的。

func IsSubtitle

func IsSubtitle(name string) bool

IsSubtitle 报告文件名是不是字幕。

func IsVideo

func IsVideo(name string) bool

IsVideo 报告文件名是不是视频。

func MatchSubtitles

func MatchSubtitles(videos, subs []File, key func(File) string) [][]File

MatchSubtitles 把字幕分给视频:只有一个视频时全给它;否则先按文件名前缀(字幕名以视频名开头), 再按 key(季与集)一样来分。分不出去的字幕不整理。返回值的下标对应 videos。

func NormalizeMode

func NormalizeMode(mode string) (string, error)

NormalizeMode 把空串换成硬链接,并检查整理方式。

func RemoveEmptyDirs

func RemoveEmptyDirs(dir, root string)

RemoveEmptyDirs 从 dir 往上删空目录,到 root 为止(root 本身不删)。目录的实际位置(解开软链接后) 不在 root 的实际位置里时停下:库里有指到别处的目录软链接时,不去删库外的空目录。

func RemoveIfOurs

func RemoveIfOurs(dst, mode, fileID, src string) error

RemoveIfOurs 删掉库里的 dst,前提是它还是当初整理出的那个文件:硬链接、复制、移动按文件编号(fileID)确认, 软链接按指向的源文件确认。已经不在了返回 nil;确认不了时不删、返回错误。

func Render

func Render(tpl string, v Vars) (string, error)

Render 按模板算出库里的相对路径(不含扩展名,用 / 分隔)。tpl 为空时报错,由调用方先换成默认模板。 渲染出的每一级都去掉首尾的空格与点,空的一级(包括 . 与 ..)跳过,每级最多 200 字节。

func SeasonEpisode

func SeasonEpisode(season, episode, end int) string

SeasonEpisode 写出 S01E02,多集时 S01E02-E03;没有集数时只写季。

func SubtitleLang

func SubtitleLang(name string) string

SubtitleLang 从字幕文件名里认出语言,返回放进库里时加的后缀(zh-CN、zh-TW、zh、en、ja、ko);认不出返回空串。

func SubtitleTarget

func SubtitleTarget(videoTarget, subName string, n int) string

SubtitleTarget 是字幕放进库里的路径:视频的目标路径去掉扩展名,加上语言后缀与字幕的扩展名。 n 大于 1 时在语言后面加序号(同一种语言有几份字幕时)。

func Within

func Within(root, p string) bool

Within 报告 p 是不是在 root 里面(不含 root 本身)。

Types

type File

type File struct {
	Rel  string `json:"rel"`
	Path string `json:"path"`
	Size int64  `json:"size"`
}

File 是种子里的一个文件。Rel 是下载器给的、相对保存目录的路径(用 / 分隔),Path 是 pt-tools 里的绝对路径。

type Outcome

type Outcome int

Outcome 是一次整理的结果。

const (
	// Created 表示这次新放进了库里。
	Created Outcome = iota
	// AlreadyDone 表示库里已经是这个文件(同一个 inode,或指向源文件的软链接)。复制与移动出的文件和源文件
	// 不是同一个,认不出是不是自己放的,目标已存在时一律当作别人的文件。
	AlreadyDone
)

func Transfer

func Transfer(src, dst, mode string) (Outcome, error)

Transfer 把源文件 src 按 mode 放到 dst(父目录不存在时建好)。dst 已经是这个文件时返回 AlreadyDone; 是别的文件时返回 ErrTargetExists,不覆盖。硬链接跨文件系统时返回 ErrCrossDevice。

type Selection

type Selection struct {
	Videos    []File `json:"videos"`
	Subtitles []File `json:"subtitles"`
	Skipped   []Skip `json:"skipped"`
	Disc      string `json:"disc,omitempty"`
}

Selection 是挑选的结果。Disc 不为空时种子是原盘(BDMV、VIDEO_TS),整个种子不整理。

func Select

func Select(files []File, minSize int64) Selection

Select 从种子的文件里挑出要整理的视频与字幕。minSize 为 0 时用 DefaultMinVideoBytes。 原盘目录(BDMV、VIDEO_TS)的种子不挑:整个种子跳过,Disc 写明种类。

type Skip

type Skip struct {
	File   File   `json:"file"`
	Reason string `json:"reason"`
}

Skip 是没有选上的文件与原因。

type SourceKeptError

type SourceKeptError struct {
	Mode string
	Err  error
}

SourceKeptError 表示移动时文件已经放进库里,但删不掉源文件(例如下载目录只读):库里这份按 Mode 算 (硬链接放过去的是硬链接,复制过去的是复制),源文件留在原处。调用方按 Mode 记下这份文件。

func (*SourceKeptError) Error

func (e *SourceKeptError) Error() string

func (*SourceKeptError) Unwrap

func (e *SourceKeptError) Unwrap() error

type Vars

type Vars struct {
	Title         string // 标题(TMDB,按设置的语言)
	OriginalTitle string // 原名
	Year          int    // 年份;剧集是首播年份
	TMDBID        int
	IMDbID        string
	Season        int    // 季(电影为 0)
	Episode       int    // 集(电影为 0)
	EpisodeEnd    int    // 一个文件里有多集时的最后一集
	SeasonEpisode string // S01E02,多集时 S01E02-E03
	EpisodeTitle  string // 这一集的标题(TMDB 有时才有)
	Resolution    string // 2160p 一类
	Source        string // WEB-DL、BluRay 一类,Remux 时带上 Remux
	VideoCodec    string
	HDR           string // 多个时用空格隔开
	Audio         string // 多个时用空格隔开
	Group         string // 制作组
	Edition       string // 版本说明,多个时用空格隔开
	Quality       string // 分辨率、来源、HDR、编码连在一起,如「2160p WEB-DL HDR10 H.265」
}

Vars 是模板里能用的变量。字符串在渲染前已经去掉了路径里不能用的字符。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL