File indexing completed on 2025-01-05 04:33:44

0001 /*
0002     SPDX-FileCopyrightText: 2007-2009 Sergio Pistone <sergio_pistone@yahoo.com.ar>
0003     SPDX-FileCopyrightText: 2010-2022 Mladen Milinkovic <max@smoothware.net>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef YOUTUBECAPTIONSINPUTFORMAT_H
0009 #define YOUTUBECAPTIONSINPUTFORMAT_H
0010 
0011 #include "core/richtext/richdocument.h"
0012 #include "helpers/common.h"
0013 #include "formats/inputformat.h"
0014 
0015 #include <QRegularExpression>
0016 
0017 namespace SubtitleComposer {
0018 class YouTubeCaptionsInputFormat : public InputFormat
0019 {
0020     friend class FormatManager;
0021 
0022 protected:
0023     YouTubeCaptionsInputFormat()
0024         : InputFormat($("YouTube Captions"), QStringList($("sbv")))
0025     {}
0026 
0027     bool parseSubtitles(Subtitle &subtitle, const QString &data) const override
0028     {
0029         staticRE$(reTime,
0030             "([\\d]+\\n)?"
0031             "(\\d+):([0-5][0-9]):([0-5][0-9])[,\\.]([0-9][0-9][0-9]),"
0032             "(\\d+):([0-5][0-9]):([0-5][0-9])[,\\.]([0-9][0-9][0-9])\\n");
0033         QRegularExpressionMatchIterator it = reTime.globalMatch(data);
0034         if(!it.hasNext())
0035             return false;
0036 
0037         do {
0038             QRegularExpressionMatch tm = it.next();
0039             const Time showTime(tm.captured(2).toInt(), tm.captured(3).toInt(), tm.captured(4).toInt(), tm.captured(5).toInt());
0040             const Time hideTime(tm.captured(6).toInt(), tm.captured(7).toInt(), tm.captured(8).toInt(), tm.captured(9).toInt());
0041 
0042             const int off = tm.capturedEnd();
0043             const QString text = data.mid(off, it.hasNext() ? it.peekNext().capturedStart() - off : -1).trimmed();
0044 
0045             // TODO does the format actually support styled text?
0046             // if so, does it use standard HTML style tags?
0047 
0048             SubtitleLine *l = new SubtitleLine(showTime, hideTime);
0049             l->primaryDoc()->setHtml(text, true);
0050             subtitle.insertLine(l);
0051         } while(it.hasNext());
0052 
0053         return subtitle.count() > 0;
0054     }
0055 };
0056 }
0057 
0058 #endif