-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubtitleThread.cpp
More file actions
154 lines (143 loc) · 3.88 KB
/
subtitleThread.cpp
File metadata and controls
154 lines (143 loc) · 3.88 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
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
154
#include "subtitleThread.h"
#include <QFile>
#include <qtextstream.h>
#include <QDebug>
#include <qregularexpression.h>
#include <QTime>
#include <QDir>
subtitleThread::subtitleThread(QObject *parent)
: QThread(parent)
{
readSubtitleMode = false;
openMode = false;
}
subtitleThread::~subtitleThread()
{
}
//设置文件名
void subtitleThread::setFileName(const QString & name)
{
fileName = name;
}
//加载字幕
void subtitleThread::run()
{
QFile file(fileName);
if (!file.open(QIODevice::Text | QIODevice::ReadOnly)) {
emit loadStatus(QStringLiteral("无法加载字幕文件"));
file.close();
return;
}
QTextStream in(&file);
in.setCodec("UTF-8");
txtList.clear();
txtListEnd.clear();
if (fileName.indexOf(QRegularExpression(".srt$")) != -1) {
while (!in.atEnd()) {
if (in.readLine().indexOf(QRegularExpression("^\\d+$")) != -1) {
QString line = in.readLine();
QRegularExpression rx("(\\d\\d:\\d\\d:\\d\\d)");
QRegularExpressionMatch match = rx.match(line);
QStringList timeList;
while (match.hasMatch()) {
timeList << match.captured(1);
match = rx.match(line, match.capturedEnd());
}
line = in.readLine();
QString text;
while (!line.isEmpty()) {
line.replace(QRegularExpression("{.*?}"), "");
if (!text.isEmpty())
text += "\n";
text += line;
line = in.readLine();
}
txtList[timeList.at(0)] = text;
txtListEnd[timeList.at(1)] = "";
}
}
}
else if (fileName.indexOf(QRegularExpression(".ass$")) != -1) {
while (!in.atEnd()) {
QString line = in.readLine();
if (line.indexOf("Dialogue:") != -1) {
QRegularExpression rx("(\\d:\\d\\d:\\d\\d)");
QRegularExpressionMatch match = rx.match(line);
QStringList timeList;
while (match.hasMatch()) {
timeList << match.captured(1).insert(0, "0");
match = rx.match(line, match.capturedEnd());
}
line.replace(QRegularExpression("Dialogue:.*,,"), "").replace(QRegularExpression("^{.*?}$|{.*?}"), "").replace("\\N", "\n");
txtList[timeList.at(0)] = line;
txtListEnd[timeList.at(1)] = "";
}
}
}
file.close();
openMode = true;
timeControl = 0;
emit loadStatus(QStringLiteral("加载字幕文件成功"));
exec(); //保持线程循环
}
//查找当前目录是否有字幕文件
void subtitleThread::findFile(const QString &name)
{
QString path = name;
QString tempFileName = path.split("/").at(path.split("/").size() - 1);
tempFileName.replace(QRegularExpression("(\\.)\\S{1,4}$"), "");
path.replace(path.split("/").at(path.split("/").size() - 1), "");
QDir dir(path);
QStringList list;
list << "*.ass" << "*.srt";
dir.setNameFilters(list);
list = dir.entryList();
for (int i = 0; i < list.size(); i++) {
if (list.at(i).indexOf(tempFileName) != -1) {
fileName = path + list.at(i);
emit haveFound(true);
emit loadStatus(QStringLiteral("找到本地字幕文件"));
return;
}
}
emit haveFound(false);
}
//清除字幕
void subtitleThread::clearSubtitle()
{
txtList.clear();
txtListEnd.clear();
}
//提前时间
int subtitleThread::addTime()
{
return (timeControl += 100);
}
//延迟时间
int subtitleThread::lowerTime()
{
return (timeControl -= 100);
}
void subtitleThread::readTime(qint64 position) //播放进度匹配字幕
{
if (openMode) {
if (!readSubtitleMode) {
QString time = QTime::fromMSecsSinceStartOfDay(position + timeControl).toString("hh:mm:ss");
if (!txtList.value(time).isNull()) {
emit readSubtitleText(txtList.value(time));
readSubtitleMode = true;
}
}
else {
QString time = QTime::fromMSecsSinceStartOfDay(position + timeControl).toString("hh:mm:ss");
if (!txtListEnd.value(time).isNull()) {
emit readSubtitleText("");
readSubtitleMode = false;
}
if (!txtList.value(time).isNull()) {
emit readSubtitleText(txtList.value(time));
readSubtitleMode = true;
}
}
}
}