| 1 | /***************************************************************************** |
| 2 | ** QNapi |
| 3 | ** Copyright (C) 2008-2017 Piotr KrzemiĆski <[email protected]> |
| 4 | ** |
| 5 | ** This program is free software; you can redistribute it and/or modify |
| 6 | ** it under the terms of the GNU General Public License as published by |
| 7 | ** the Free Software Foundation; either version 2 of the License, or |
| 8 | ** (at your option) any later version. |
| 9 | ** |
| 10 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
| 11 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | ** |
| 13 | *****************************************************************************/ |
| 14 | |
| 15 | #include "subrip.h" |
| 16 | |
| 17 | bool SubRipSubtitleFormat::detect(const QStringList &lines) const { |
| 18 | QStringList lines2 = lines; |
| 19 | while (!lines2.isEmpty() && lines2.first().trimmed().isEmpty()) { |
| 20 | lines2.pop_front(); |
| 21 | } |
| 22 | |
| 23 | QString firstEntryBuff; |
| 24 | |
| 25 | foreach (QString line, lines2) { |
| 26 | if (line.isEmpty()) break; |
| 27 | firstEntryBuff += line + "\n"; |
| 28 | } |
| 29 | |
| 30 | QRegExp r( |
| 31 | "^(\\d+)(\\n|\\r\\n|\\s+)(\\d{2}):(\\d{2}):(\\d{2})\\,(\\d{3})\\s+\\-\\->" |
| 32 | "\\s+(\\d{2}):(\\d{2}):(\\d{2})\\,(\\d{3})(.*)"); |
| 33 | |
| 34 | return r.exactMatch(firstEntryBuff); |
| 35 | } |
| 36 | |
| 37 | SubFile SubRipSubtitleFormat::decode(const QStringList &lines) const { |
| 38 | SubFile sf; |
| 39 | |
| 40 | QRegExp r1( |
| 41 | "^(\\d+)\\s+(\\d{2}):(\\d{2}):(\\d{2})\\,(\\d{3})\\s+\\-\\->\\s+(\\d{2}):" |
| 42 | "(\\d{2}):(\\d{2})\\,(\\d{3})(.*)"); |
| 43 | QRegExp r2( |
| 44 | "^(\\d{2}):(\\d{2}):(\\d{2})\\,(\\d{3})\\s+\\-\\->\\s+(\\d{2}):(\\d{2}):(" |
| 45 | "\\d{2})\\,(\\d{3})(.*)"); |
| 46 | QRegExp rNumLine("^\\d+"); |
| 47 | |
| 48 | QString tokensBuff = "", numsBuff; |
| 49 | SrtTimestamps tss; |
| 50 | |
| 51 | foreach (QString line, lines) { |
| 52 | if (r1.exactMatch(line)) { |
| 53 | if (!tokensBuff.isEmpty()) addEntry(sf.entries, tokensBuff, tss); |
| 54 | tss.h1 = r1.cap(2).toInt(); |
| 55 | tss.m1 = r1.cap(3).toInt(); |
| 56 | tss.s1 = r1.cap(4).toInt(); |
| 57 | tss.ms1 = r1.cap(5).toInt(); |
| 58 | tss.h2 = r1.cap(6).toInt(); |
| 59 | tss.m2 = r1.cap(7).toInt(); |
| 60 | tss.s2 = r1.cap(8).toInt(); |
| 61 | tss.ms2 = r1.cap(9).toInt(); |
| 62 | numsBuff.clear(); |
| 63 | } else if (r2.exactMatch(line)) { |
| 64 | if (!tokensBuff.isEmpty()) addEntry(sf.entries, tokensBuff, tss); |
| 65 | tss.h1 = r2.cap(1).toInt(); |
| 66 | tss.m1 = r2.cap(2).toInt(); |
| 67 | tss.s1 = r2.cap(3).toInt(); |
| 68 | tss.ms1 = r2.cap(4).toInt(); |
| 69 | tss.h2 = r2.cap(5).toInt(); |
| 70 | tss.m2 = r2.cap(6).toInt(); |
| 71 | tss.s2 = r2.cap(7).toInt(); |
| 72 | tss.ms2 = r2.cap(8).toInt(); |
| 73 | numsBuff.clear(); |
| 74 | } else if (rNumLine.exactMatch(line)) { |
| 75 | numsBuff += line + "\n"; |
| 76 | } else if (!line.trimmed().isEmpty()) { |
| 77 | if (!numsBuff.isEmpty()) { |
| 78 | tokensBuff += numsBuff; |
| 79 | numsBuff.clear(); |
| 80 | } |
| 81 | tokensBuff += line + "\n"; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if (!tokensBuff.isEmpty()) addEntry(sf.entries, tokensBuff, tss); |
| 86 | |
| 87 | return sf; |
| 88 | } |
| 89 | |
| 90 | QStringList SubRipSubtitleFormat::encode(const SubFile &subFile) const { |
| 91 | QStringList lines; |
| 92 | int i = 1; |
| 93 | foreach (const SubEntry &entry, subFile.entries) { |
| 94 | lines << encodeEntry(entry, i++); |
| 95 | lines << ""; |
| 96 | } |
| 97 | return lines; |
| 98 | } |
| 99 | |
| 100 | void SubRipSubtitleFormat::addEntry(QVector<SubEntry> &entries, |
| 101 | QString &tokensBuff, |
| 102 | SrtTimestamps ×tamps) const { |
| 103 | SubEntry se; |
| 104 | se.frameStart = 3600000L * timestamps.h1 + 60000L * timestamps.m1 + |
| 105 | 1000L * timestamps.s1 + timestamps.ms1; |
| 106 | se.frameStop = 3600000L * timestamps.h2 + 60000L * timestamps.m2 + |
| 107 | 1000L * timestamps.s2 + timestamps.ms2; |
| 108 | se.tokens = decodeTokenStream(tokensBuff); |
| 109 | |
| 110 | while (!se.tokens.isEmpty() && se.tokens.back().type == STT_NEWLINE) |
| 111 | se.tokens.pop_back(); |
| 112 | |
| 113 | entries.push_back(se); |
| 114 | tokensBuff.clear(); |
| 115 | } |
| 116 | |
| 117 | QStringList SubRipSubtitleFormat::encodeEntry(const SubEntry &entry, |
| 118 | int i) const { |
| 119 | QString tokensBuff; |
| 120 | foreach (const SubToken &token, entry.tokens) { |
| 121 | tokensBuff += encodeToken(token); |
| 122 | } |
| 123 | |
| 124 | int h1 = entry.frameStart / 3600000; |
| 125 | int m1 = (entry.frameStart - h1 * 3600000) / 60000; |
| 126 | int s1 = (entry.frameStart - h1 * 3600000 - m1 * 60000) / 1000; |
| 127 | int ms1 = entry.frameStart - h1 * 3600000 - m1 * 60000 - s1 * 1000; |
| 128 | |
| 129 | int h2 = entry.frameStop / 3600000; |
| 130 | int m2 = (entry.frameStop - h2 * 3600000) / 60000; |
| 131 | int s2 = (entry.frameStop - h2 * 3600000 - m2 * 60000) / 1000; |
| 132 | int ms2 = entry.frameStop - h2 * 3600000 - m2 * 60000 - s2 * 1000; |
| 133 | |
| 134 | QStringList out; |
| 135 | out << QString::number(i) |
| 136 | << QString("%1:%2:%3,%4 --> %5:%6:%7,%8") |
| 137 | .arg(h1, 2, 10, QChar('0')) |
| 138 | .arg(m1, 2, 10, QChar('0')) |
| 139 | .arg(s1, 2, 10, QChar('0')) |
| 140 | .arg(ms1, 3, 10, QChar('0')) |
| 141 | .arg(h2, 2, 10, QChar('0')) |
| 142 | .arg(m2, 2, 10, QChar('0')) |
| 143 | .arg(s2, 2, 10, QChar('0')) |
| 144 | .arg(ms2, 3, 10, QChar('0')) |
| 145 | << tokensBuff.split("\n"); |
| 146 | return out; |
| 147 | } |
| 148 | |
| 149 | QString SubRipSubtitleFormat::encodeToken(const SubToken &entry) const { |
| 150 | switch (entry.type) { |
| 151 | case STT_WS: |
| 152 | return " "; |
| 153 | case STT_WORD: |
| 154 | return entry.payload; |
| 155 | case STT_NEWLINE: |
| 156 | return "\n"; |
| 157 | case STT_BOLD: |
| 158 | return "<b>"; |
| 159 | case STT_BOLD_END: |
| 160 | return "</b>"; |
| 161 | case STT_ITALIC: |
| 162 | return "<i>"; |
| 163 | case STT_ITALIC_END: |
| 164 | return "</i>"; |
| 165 | case STT_UNDERLINE: |
| 166 | return "<u>"; |
| 167 | case STT_UNDERLINE_END: |
| 168 | return "</u>"; |
| 169 | case STT_FONTCOLOR: |
| 170 | return QString("<font color=\"#%1\">").arg(entry.payload); |
| 171 | case STT_FONTCOLOR_END: |
| 172 | return "</font>"; |
| 173 | default: |
| 174 | return ""; |
| 175 | } |
| 176 | } |