transfer

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 18 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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 ChineseSubtitle

func ChineseSubtitle(lang string) bool

ChineseSubtitle 报告这种语言是不是中文(简体、繁体或没分简繁的中文)。

func CleanName

func CleanName(s string) string

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

func FileID

func FileID(path string) (string, error)

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

func IsISO

func IsISO(name string) bool

IsISO 报告文件名是不是 ISO 镜像(电影当作一个视频文件整理,剧集里的不整理)。

func IsLibraryVideo

func IsLibraryVideo(name string) bool

IsLibraryVideo 报告媒体库里的这个文件是不是视频:普通的视频文件、ISO 镜像与 DVD 原盘的 VOB(蓝光原盘的 .m2ts 本来就是视频)。 删除库里的文件时用它看目录里还有没有视频。

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 Place

func Place(src, dst string) error

Place 把同一个目录里的 src 挪到 dst,不覆盖已有的文件。先用原子的不覆盖改名(Linux 的 RENAME_NOREPLACE、Windows 的 MoveFileEx);不支持时用硬链接放过去,确认 src 还是放过去的那个文件才删掉 src 这个名字(确认和删除之间别人换掉 src 的窗口只有这一瞬)。都不支持时返回错误,不冒险覆盖。dst 已存在时返回 ErrTargetExists。

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 SubtitleLangRank

func SubtitleLangRank(lang string) int

SubtitleLangRank 是语言的先后(数小的在前):简体 → 繁体 → 中文 → 英 → 日 → 韩,认不出的最后。

func SubtitleTarget

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

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

func SubtitleTargetLang

func SubtitleTargetLang(videoTarget, lang, ext string, n int) string

SubtitleTargetLang 同 SubtitleTarget,语言由调用方给(空串是不加语言后缀;站点字幕的文件名里没写语言时用站点列表上的), ext 是字幕的扩展名(带点)。

func TransferDetail

func TransferDetail(src, dst, mode string) (Outcome, Detail, error)

TransferDetail 同 Transfer,另外回这次怎么放的(复制用没用写时复制)。

func Within

func Within(root, p string) bool

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

Types

type Detail

type Detail struct {
	// Cloned 表示复制用了写时复制(reflink):库里这份和源文件共用磁盘上的数据,不多占空间
	Cloned bool
}

Detail 是一次整理的细节。

type Disc

type Disc struct {
	// Kind 是 BDMV 或 VIDEO_TS(同一个根目录里两种都有时是 BDMV)
	Kind string `json:"kind"`
	// Root 是原盘根目录相对保存目录的路径(用 / 分隔);BDMV 直接在保存目录里时为空
	Root string `json:"root"`
	// Path 是原盘根目录在 pt-tools 里的路径
	Path string `json:"path"`
	// Files 是根目录下原盘结构目录(BDMV、CERTIFICATE 等)里的文件,按种子里的顺序
	Files []DiscFile `json:"files"`
	// Main 是最大的视频流(BDMV/STREAM 里的 .m2ts,或 VIDEO_TS 里的 VTS_*.VOB)在 Files 里的下标;没有时为 -1
	Main int `json:"main"`
	// Size 是 Files 的大小合计
	Size int64 `json:"size"`
}

Disc 是种子里的一张原盘:蓝光的 BDMV 或 DVD 的 VIDEO_TS 目录,以及它们的上一级(原盘根目录)。

type DiscFile

type DiscFile struct {
	File
	In string `json:"in"`
}

DiscFile 是原盘里的一个文件:In 是相对原盘根目录的路径(用 / 分隔,例如 BDMV/STREAM/00001.m2ts)。

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"`
	Discs     []Disc `json:"discs,omitempty"`
}

Selection 是挑选的结果。种子里有原盘(BDMV、VIDEO_TS 目录)时只挑原盘:Discs 列出每一张,Disc 是第一张的种类, 不再挑散的视频与字幕。

func Select

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

Select 从种子的文件里挑出要整理的视频与字幕。minSize 为 0 时用 DefaultMinVideoBytes。ISO 镜像当作视频挑。 有原盘目录(BDMV、VIDEO_TS)的种子只挑原盘(见 Disc),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