-
Notifications
You must be signed in to change notification settings - Fork 0
/
SrtParser.cpp
153 lines (132 loc) · 3.9 KB
/
SrtParser.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "SrtParser.h"
#include <QDebug>
#include <QFile>
#include <QRegularExpression>
#include <QTextStream>
SrtParser::SrtParser()
{
qDebug() << "初始化SRT解析器";
}
SrtParser::~SrtParser() {}
bool SrtParser::parse(const QString &filePath)
{
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug() << "无法打开SRT文件:" << filePath;
return false;
}
QTextStream in(&file);
in.setEncoding(QStringConverter::Utf8); // 使用新的编码设置方式
m_srtInfoList.clear();
SrtInfo currentInfo;
int state = 0; // 0:序号 1:时间 2:内容
QString line;
while (!in.atEnd())
{
line = in.readLine().trimmed();
switch (state)
{
case 0: // 序号
if (!line.isEmpty())
{
bool ok;
line.toInt(&ok);
if (!ok)
{
qDebug() << "无效的SRT序号行:" << line;
file.close();
return false;
}
state = 1;
}
break;
case 1: // 时间
if (!line.isEmpty())
{
if (!parseTimeString(line, currentInfo.time))
{
qDebug() << "无效的时间格式:" << line;
file.close();
return false;
}
state = 2;
currentInfo.content.clear();
}
break;
case 2: // 内容
if (line.isEmpty())
{
if (!currentInfo.content.isEmpty() && !currentInfo.time.isEmpty())
{
m_srtInfoList.append(currentInfo);
currentInfo = SrtInfo(); // 重置当前字幕信息
state = 0;
}
}
else
{
if (!currentInfo.content.isEmpty())
{
currentInfo.content += "\n";
}
currentInfo.content += line;
}
break;
}
}
// 处理最后一条字幕:只有当状态为2(正在处理内容)且内容和时间都不为空时才添加
if (state == 2 && !currentInfo.content.isEmpty() && !currentInfo.time.isEmpty())
{
m_srtInfoList.append(currentInfo);
}
file.close();
qDebug() << "成功解析SRT文件,共" << m_srtInfoList.size() << "条字幕";
return true;
}
bool SrtParser::save(const QString &filePath, const QVector<SrtInfo> &srtInfoList)
{
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug() << "无法创建SRT文件:" << filePath;
return false;
}
QTextStream out(&file);
out.setEncoding(QStringConverter::Utf8); // 使用新的编码设置方式
for (int i = 0; i < srtInfoList.size(); ++i)
{
// 写入序号
out << (i + 1) << "\n";
// 写入时间
out << srtInfoList[i].time << "\n";
// 只写入翻译内容
if (!srtInfoList[i].translateContent.isEmpty())
{
out << srtInfoList[i].translateContent;
}
else
{
// 如果没有翻译内容,则写入原文
out << srtInfoList[i].content;
}
// 写入空行
out << "\n\n";
}
file.close();
qDebug() << "成功保存SRT文件:" << filePath;
return true;
}
bool SrtParser::parseTimeString(const QString &timeStr, QString &time)
{
// 时间格式:00:00:00,000 --> 00:00:00,000
QRegularExpression timeRegex(
"([0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}) --> ([0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3})");
QRegularExpressionMatch match = timeRegex.match(timeStr);
if (!match.hasMatch())
{
return false;
}
time = timeStr;
return true;
}