MythTV master
srtwriter.cpp
Go to the documentation of this file.
2
3#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
4#include <QStringConverter>
5#endif
6#include <QTime>
7
9
10// SRTWriter implementation
11
12SRTWriter::SRTWriter(const QString &fileName) :
13 m_outFile(fileName), m_outStream(&m_outFile)
14{
15#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
16 m_outStream.setCodec("UTF-8");
17#else
18 m_outStream.setEncoding(QStringConverter::Utf8);
19#endif
20 if (!m_outFile.open(QFile::WriteOnly))
21 {
22 LOG(VB_GENERAL, LOG_ERR, QString("Failed to create '%1'").arg(fileName));
23 }
24 else
25 {
26 LOG(VB_GENERAL, LOG_DEBUG, QString("Created '%1'").arg(fileName));
27 }
28}
29
33void SRTWriter::AddSubtitle(const OneSubtitle &sub, int number)
34{
35 m_outStream << number << Qt::endl;
36
37 m_outStream << FormatTime(sub.m_startTime) << " --> ";
38 m_outStream << FormatTime(sub.m_startTime + sub.m_length) << Qt::endl;
39
40 if (!sub.m_text.isEmpty())
41 {
42 for (const auto & text : std::as_const(sub.m_text))
43 m_outStream << text << Qt::endl;
44 m_outStream << Qt::endl;
45 }
46}
47
51QString SRTWriter::FormatTime(std::chrono::milliseconds time_in_msec)
52{
53 QTime time = QTime::fromMSecsSinceStartOfDay(time_in_msec.count());
54 return time.toString("HH:mm:ss,zzz");
55}
Represents one subtitle record.
std::chrono::milliseconds m_length
Time we have to show subtitle, msec.
QStringList m_text
Lines of text of subtitles.
std::chrono::milliseconds m_startTime
Time we have to start showing subtitle, msec.
static QString FormatTime(std::chrono::milliseconds time_in_msec)
Formats time to format appropriate to SubRip file.
Definition: srtwriter.cpp:51
void AddSubtitle(const OneSubtitle &sub, int number)
Adds next subtitle.
Definition: srtwriter.cpp:33
SRTWriter(const QString &fileName)
Definition: srtwriter.cpp:12
QTextStream m_outStream
Output stream associated with m_outFile.
Definition: srtwriter.h:36
QFile m_outFile
Output file.
Definition: srtwriter.h:34
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39