-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson2srt.cpp
More file actions
122 lines (93 loc) · 3.37 KB
/
json2srt.cpp
File metadata and controls
122 lines (93 loc) · 3.37 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <dirent.h>
#include <cstring>
void Json_srt(const char* inputPath, const char* outputPath);
int main(int argc, char *argv[]) {
std::cout << "Bilibili json2srt v1.0.0" << std::endl;
std::cout << " " << std::endl;
std::cout << "Usage: your_program -i input_file [-o output_file]" << std::endl;
std::cout << " " << std::endl;
std::cout << "Example:" << std::endl;
std::cout << " json2srt -i input.json -o output.srt" << std::endl;
std::cout << " " << std::endl;
std::string inputFileName, outputFileName;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "-i" && i + 1 < argc) {
inputFileName = argv[++i];
} else if (arg == "-o" && i + 1 < argc) {
outputFileName = argv[++i];
}
}
if (inputFileName.empty()) {
std::cerr << "Error: Input file not specified." << std::endl;
return 1;
}
Json_srt(inputFileName.c_str(), outputFileName.c_str());
std::cout.clear();
std::cout << "Success" << std::endl;
return 0;
}
void Json_srt(const char* inputPath, const char* outputPath) {
std::ifstream openfile;
std::string temp;
std::string buffer;
char Newname[256] = {0};
openfile.open(inputPath, std::ios::in);
int i = 0;
while (inputPath[i] != '.') {
Newname[i] = inputPath[i];
i++;
}
if (outputPath != nullptr && outputPath[0] != '\0') {
strcpy(Newname, outputPath);
} else {
strcat(Newname, ".srt");
}
std::ofstream outfile(Newname, std::ios::out);
if (!openfile.is_open()) {
std::cout << "Error opening file: " << inputPath << std::endl;
return;
}
while (std::getline(openfile, temp)) {
// Do something if needed
}
long int pbeigen = 0, pendl = 0, num, count = 0;
float time;
int hour, minute, second, millisecond;
while (1) {
count++;
pbeigen = temp.find("from", pendl);
if (pbeigen == -1) break;
outfile << count << "\n";
pendl = temp.find(",", pbeigen);
num = pendl - pbeigen - 6;
buffer = temp.substr(pbeigen + 6, num);
time = std::stof(buffer);
millisecond = (time - (int)time) * 1000;
second = (int)time % 60;
minute = (int)time / 60 % 60;
hour = (int)time / 60 / 60 % 12;
outfile << std::setfill('0') << std::setw(2) << hour << ":" << std::setw(2) << minute << ":" << std::setw(2) << second << "," << std::setw(3) << millisecond << " --> ";
pbeigen = temp.find("to", pendl);
pendl = temp.find(",", pbeigen);
num = pendl - pbeigen - 4;
buffer = temp.substr(pbeigen + 4, num);
time = std::stof(buffer);
millisecond = (time - (int)time) * 1000;
second = (int)time % 60;
minute = (int)time / 60 % 60;
hour = (int)time / 60 / 60 % 12;
outfile << std::setfill('0') << std::setw(2) << hour << ":" << std::setw(2) << minute << ":" << std::setw(2) << second << "," << std::setw(3) << millisecond << " \n ";
pbeigen = temp.find("content", pendl);
pendl = temp.find("\"}", pbeigen);
num = pendl - pbeigen - 10;
buffer = temp.substr(pbeigen + 10, num);
outfile << buffer << "\n\n";
if (pendl == -1)
break;
}
}