add tool.
Some checks failed
Deploy Applications / PullDocker (push) Failing after 6m52s
Deploy Applications / Build (push) Failing after 2s
Windows CI / build (push) Has been cancelled

This commit is contained in:
2025-10-22 21:51:15 +08:00
parent 0354a73175
commit a842d429b7
6 changed files with 397 additions and 1 deletions

91
rename.cpp Normal file
View File

@@ -0,0 +1,91 @@
#include <filesystem>
#include <format>
#include <fstream>
#include <iostream>
#include <vector>
// g++ -std=c++20 rename.cpp -o rename
// title 字段需要,可以先随便填
constexpr auto Template = R"(
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<episodedetails>
<title></title>
<showtitle></showtitle>
<season>{}</season>
<episode>{}</episode>
<original_filename>{}</original_filename>
</episodedetails>
)";
struct SeasonRange {
int start;
int end;
int season;
};
const std::vector<SeasonRange> ranges = {
{1, 170, 1}, // 001-048 season 1 48集
};
// 文件夹内有 001.mkv-328.mkv 文件使用C++ STL filesystem 根据上面的对应关系生成 001.nfo-328.nfo, 使用 std::format 传入字符串
// Template 输出最终内容写入对应的nfo文件
int main(int argc, char const *argv[]) {
std::cout << "this is a simple rename app." << std::endl;
for (const auto &entry : std::filesystem::directory_iterator(".")) {
if (!entry.is_regular_file()) continue;
const auto &path = entry.path();
if (path.extension() != ".mkv") continue;
const std::string filename = path.filename().string();
const std::string stem = path.stem().string();
std::cout <<"stem: "<<stem<<std::endl;
// 提取前导数字部分
size_t num_length = 0;
while (num_length < stem.length() && std::isdigit(stem[num_length])) {
num_length++;
}
if (num_length == 0) {
std::cerr << "跳过无数字前缀的文件: " << filename << std::endl;
continue;
}
try {
const int episode_number = std::stoi(stem.substr(0, num_length));
// 确定季度和集数
int season = -1;
int episode_in_season = -1;
for (const auto &range : ranges) {
if (episode_number >= range.start && episode_number <= range.end) {
season = range.season;
episode_in_season = episode_number - range.start + 1;
break;
}
}
if (season == -1) {
std::cerr << "未找到对应的季度: " << episode_number << std::endl;
continue;
}
// 生成XML内容
std::string xml_content = std::format(Template, season, episode_in_season, filename);
// 写入.nfo文件
std::filesystem::path nfo_path = path;
nfo_path.replace_extension(".nfo");
std::ofstream out_file(nfo_path);
out_file << xml_content;
std::cout <<"filename: "<<filename<< ", episode_number: "<<episode_number<<", episode_in_season: "<<episode_in_season<< std::endl;
std::cout << "已生成: " << nfo_path.filename() << std::endl;
} catch (const std::exception &e) {
std::cerr << "处理文件 " << filename << " 时出错: " << e.what() << std::endl;
}
}
return 0;
}