forked from Kam1k4dze/SubChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_main.cpp
97 lines (76 loc) · 2.82 KB
/
cli_main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "ytt_generator.h"
#include <CLI/CLI.hpp>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
// dumb and simple way to parse CSV
std::vector<ChatMessage> parseCSV(const std::string &filename, int timeMultiplier) {
std::vector<ChatMessage> messages;
std::ifstream file(filename);
std::string line;
if (!file.is_open()) {
std::cerr << "Error: Could not open file " << filename << "\n";
std::exit(-1);
}
std::getline(file, line);
if (line != "time,user_name,user_color,message") {
std::cerr << "Error: Unexpected CSV header format.\n";
std::exit(-1);
}
while (std::getline(file, line)) {
std::stringstream ss(line);
std::string field;
ChatMessage msg;
std::getline(ss, field, ',');
msg.time = std::stoi(field) * timeMultiplier;
std::getline(ss, msg.user.name, ',');
std::getline(ss, field, ',');
msg.user.color = field.empty() ? getRandomColor(msg.user.name) : Color(field);
std::getline(ss, msg.message);
if (msg.message.size() >= 2 &&
msg.message.front() == '"' &&
msg.message.back() == '"') {
msg.message = msg.message.substr(1, msg.message.size() - 2);
}
messages.emplace_back(std::move(msg));
}
return messages;
}
int main(int argc, char *argv[]) {
CLI::App app{"Chat → YTT/SRV3 subtitle generator"};
std::string configPath, csvPath, outputPath;
std::string timeUnit;
app.add_option("-c,--config", configPath, "Path to INI config file")
->required()
->check(CLI::ExistingFile);
app.add_option("-i,--input", csvPath, "Path to chat CSV file")
->required()
->check(CLI::ExistingFile);
app.add_option("-o,--output", outputPath, "Output file (e.g. output.srv3 or output.ytt)")
->required();
app.add_option("-u,--time-unit", timeUnit, "Time unit inside CSV: “ms” or “sec”")
->required()
->check(CLI::IsMember({"ms", "sec"}, CLI::ignore_case));
CLI11_PARSE(app, argc, argv);
int multiplier = (timeUnit == "sec") ? 1000 : 1;
ChatParams params;
if (!params.loadFromFile(configPath.c_str())) {
std::cerr << "Error: Cannot open config file: " << configPath << "\n";
return 1;
}
auto chat = parseCSV(csvPath, multiplier);
if (chat.empty()) {
std::cerr << "Error: Failed to parse chat CSV or it's empty: " << csvPath << "\n";
return 1;
}
std::string xml = generateXML(chat, params);
std::ofstream out(outputPath);
if (!out) {
std::cerr << "Error: Cannot open output file: " << outputPath << "\n";
return 1;
}
out << xml;
std::cout << "Successfully wrote subtitles to: " << outputPath << "\n";
return 0;
}