QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
SubtitleWriter.cc
Go to the documentation of this file.
1#include "SubtitleWriter.h"
2
3#include <QtCore/QDateTime>
4#include <QtCore/QFileInfo>
5#include <QtCore/QLocale>
6#include <QtCore/QTextStream>
7
9
10QGC_LOGGING_CATEGORY(SubtitleWriterLog, "Video.SubtitleWriter")
11
12namespace {
13
14// Layout of the telemetry columns, in subtitle pixels.
15constexpr int kColumnCount = 3;
16constexpr int kOffsetFactor = 100; // narrows the outer margins
17// Used when the recording's resolution is not known yet.
18constexpr QSize kDefaultVideoSize{1920, 1080};
19
21QString assTime(qint64 ms)
22{
23 const QChar zero(u'0');
24 return QStringLiteral("%1:%2:%3.%4")
25 .arg(ms / 3'600'000)
26 .arg((ms / 60'000) % 60, 2, 10, zero)
27 .arg((ms / 1000) % 60, 2, 10, zero)
28 .arg((ms / 10) % 100, 2, 10, zero);
29}
30
31} // namespace
32
34 : QObject(parent)
35{
36 (void) connect(&_timer, &QTimer::timeout, this, &SubtitleWriter::_captureTelemetry);
37}
38
43
45{
46 const QFileInfo info(videoFile);
47 return QStringLiteral("%1/%2.ass").arg(info.path(), info.completeBaseName());
48}
49
50void SubtitleWriter::startCapturingTelemetry(const QString& videoFile, QSize videoSize)
51{
53
54 _videoFile = videoFile;
55 _size = videoSize.isEmpty() ? kDefaultVideoSize : videoSize;
56 _rowWidth = (_size.width() + kOffsetFactor) / (kColumnCount + 1);
57 _telemetry.readLayout();
58
59 _file.setFileName(subtitleFile(videoFile));
60 qCDebug(SubtitleWriterLog) << "Writing overlay to file:" << _file.fileName();
61 if (!_file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) {
62 qCWarning(SubtitleWriterLog) << "Unable to write subtitle file" << _file.fileName() << _file.errorString();
63 return;
64 }
65
66 static constexpr int baseWidth = 640;
67 static constexpr int baseFontSize = 12;
68 const int scaledFontSize = (_size.width() * baseFontSize) / baseWidth;
69
70 QTextStream stream(&_file);
71 stream << QStringLiteral(
72 "[Script Info]\n"
73 "Title: QGroundControl Subtitle Telemetry file\n"
74 "ScriptType: v4.00+\n"
75 "WrapStyle: 0\n"
76 "ScaledBorderAndShadow: yes\n"
77 "YCbCr Matrix: TV.601\n"
78 "PlayResX: %1\n"
79 "PlayResY: %2\n"
80 "\n"
81 "[V4+ Styles]\n"
82 "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, "
83 "Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, "
84 "Alignment, MarginL, MarginR, MarginV, Encoding\n"
85 "Style: "
86 "Default,Monospace,%3,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,2,1,10,10,"
87 "10,1\n"
88 "\n"
89 "[Events]\n"
90 "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n")
91 .arg(_size.width())
92 .arg(_size.height())
93 .arg(scaledFontSize);
94 stream.flush();
95
96 _elapsed.start();
97 _pending = _sample(0);
98 _timer.start(1000 / _kSampleRate);
99}
100
102{
103 _timer.stop();
104 if (!_file.isOpen()) {
105 return;
106 }
107 qCDebug(SubtitleWriterLog) << "Stopping writing" << _file.fileName();
108 _writeSample(_pending, _elapsed.elapsed());
109 _pending = Sample();
110 _file.close();
111}
112
113void SubtitleWriter::_captureTelemetry()
114{
115 if (!_file.isOpen()) {
116 return;
117 }
118 const qint64 nowMs = _elapsed.elapsed();
119 _writeSample(_pending, nowMs);
120 _pending = _sample(nowMs);
121}
122
123SubtitleWriter::Sample SubtitleWriter::_sample(qint64 startMs) const
124{
125 Sample sample;
126 sample.startMs = startMs;
127
128 const std::optional<QList<TelemetryBarValues::Value>> values = _telemetry.read();
129 if (!values) {
130 return sample;
131 }
132
133 // The clock time lets a recording be matched with the flight's logs.
134 sample.dateTime = QDateTime::currentDateTime().toString(QLocale::system().dateFormat(QLocale::ShortFormat) +
135 QStringLiteral(" HH:mm:ss"));
136 for (const TelemetryBarValues::Value& value : *values) {
137 sample.names << QStringLiteral("%1:").arg(value.name);
138 sample.values << value.value;
139 }
140 return sample;
141}
142
143void SubtitleWriter::_writeSample(const Sample& sample, qint64 endMs)
144{
145 if ((sample.startMs < 0) || sample.dateTime.isEmpty() || (endMs <= sample.startMs) || !_file.isOpen()) {
146 return;
147 }
148
149 const QString start = assTime(sample.startMs);
150 const QString end = assTime(endMs);
151 const int valuesPerColumn = (static_cast<int>(sample.names.size()) + kColumnCount - 1) / kColumnCount;
152 const QString y = QString::number(_size.height() - 30);
153
154 QTextStream stream(&_file);
155 // Each column is a right-aligned list of names and a left-aligned list of values.
156 for (int column = 0; column < kColumnCount; column++) {
157 const int x = (-kOffsetFactor / 2) + (_rowWidth * (column + 1));
158 stream << QStringLiteral("Dialogue: 0,%1,%2,Default,,0,0,0,,{\\an3\\pos(%3,%4)}%5\n")
159 .arg(start, end, QString::number(x - 10), y,
160 sample.names.mid(column * valuesPerColumn, valuesPerColumn).join(QStringLiteral("\\N")));
161 stream << QStringLiteral("Dialogue: 0,%1,%2,Default,,0,0,0,,{\\pos(%3,%4)}%5\n")
162 .arg(start, end, QString::number(x), y,
163 sample.values.mid(column * valuesPerColumn, valuesPerColumn).join(QStringLiteral("\\N")));
164 }
165 stream << QStringLiteral("Dialogue: 0,%1,%2,Default,,0,0,0,,{\\pos(10,35)}%3\n").arg(start, end, sample.dateTime);
166}
#define QGC_LOGGING_CATEGORY(name, categoryStr)
SubtitleWriter(QObject *parent=nullptr)
QString videoFile() const
The video file the current capture belongs to.
static QString subtitleFile(const QString &videoFile)
The subtitle file that goes with videoFile: "<videoFile without extension>.ass".
void stopCapturingTelemetry()
void startCapturingTelemetry(const QString &videoFile, QSize videoSize)
~SubtitleWriter() override