| 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 "subtitleconverter.h" |
| 16 | #include <QFile> |
| 17 | #include <QTextStream> |
| 18 | #include <cmath> |
| 19 | #include "libqnapi.h" |
| 20 | #include "movieinfo/movieinfoprovider.h" |
| 21 | #include "utils/encodingutils.h" |
| 22 | |
| 23 | SubtitleConverter::SubtitleConverter( |
| 24 | QSharedPointer<const SubtitleFormatsRegistry> subtitleFormatsRegistry, |
| 25 | QSharedPointer<const MovieInfoProvider> movieInfoProvider, |
| 26 | bool skipConvertAds) |
| 27 | : subtitleFormatsRegistry(subtitleFormatsRegistry), |
| 28 | movieInfoProvider(movieInfoProvider), |
| 29 | skipConvertAds(skipConvertAds) {} |
| 30 | |
| 31 | QString SubtitleConverter::detectFormat(const QString &subtitleFile) const { |
| 32 | const QStringList &lines = readFile(subtitleFile, "UTF-8", 15); |
| 33 | return detectFormat(lines); |
| 34 | } |
| 35 | |
| 36 | QString SubtitleConverter::detectFormat( |
| 37 | const QStringList &subtitleLines) const { |
| 38 | foreach (QString format, subtitleFormatsRegistry->listFormatNames()) { |
| 39 | if (subtitleFormatsRegistry->select(format)->detect(subtitleLines)) { |
| 40 | return format; |
| 41 | } |
| 42 | } |
| 43 | return ""; |
| 44 | } |
| 45 | |
| 46 | bool SubtitleConverter::convertSubtitles(QString subtitleFile, |
| 47 | QString targetFormatName, |
| 48 | QString targetFileName, |
| 49 | double movieFPS, double fpsRatio, |
| 50 | double delayOffset) const { |
| 51 | return convertSubtitles(subtitleFile, targetFormatName, targetFileName, |
| 52 | [=]() -> double { return movieFPS; }, fpsRatio, |
| 53 | delayOffset); |
| 54 | } |
| 55 | |
| 56 | bool SubtitleConverter::convertSubtitles(QString subtitleFile, |
| 57 | QString targetFormatName, |
| 58 | QString targetFileName, |
| 59 | QString movieFile) const { |
| 60 | return convertSubtitles( |
| 61 | subtitleFile, targetFormatName, targetFileName, [&]() -> double { |
| 62 | const Maybe<MovieInfo> mmi = movieInfoProvider->getMovieInfo(movieFile); |
| 63 | return mmi ? mmi.value().frameRate() : 0.0; |
| 64 | }); |
| 65 | } |
| 66 | |
| 67 | bool SubtitleConverter::convertSubtitles(QString subtitleFile, |
| 68 | QString targetFormatName, |
| 69 | QString targetFileName, |
| 70 | std::function<double()> determineFPS, |
| 71 | double fpsRatio, |
| 72 | double delayOffset) const { |
| 73 | EncodingUtils eu; |
| 74 | QString encoding = eu.detectFileEncoding(subtitleFile); |
| 75 | |
| 76 | QStringList subtitleLines = readFile(subtitleFile, encoding); |
| 77 | |
| 78 | QString detectedFormat = detectFormat(subtitleLines); |
| 79 | if (detectedFormat.isEmpty()) return false; |
| 80 | |
| 81 | QSharedPointer<const SubtitleFormat> inputFormat = |
| 82 | subtitleFormatsRegistry->select(detectedFormat); |
| 83 | QSharedPointer<const SubtitleFormat> targetFormat = |
| 84 | subtitleFormatsRegistry->select(targetFormatName); |
| 85 | |
| 86 | SubFile sf = inputFormat->decode(subtitleLines); |
| 87 | double frameRate = 0.0; |
| 88 | |
| 89 | if (inputFormat->isTimeBased() != targetFormat->isTimeBased()) { |
| 90 | frameRate = determineFPS(); |
| 91 | if (frameRate <= 0.0) return false; |
| 92 | |
| 93 | if (targetFormat->isTimeBased()) { |
| 94 | // convert frames to timestamps |
| 95 | for (int i = 0; i < sf.entries.size(); ++i) { |
| 96 | sf.entries[i].frameStart = |
| 97 | frame2ts(sf.entries[i].frameStart, frameRate); |
| 98 | sf.entries[i].frameStop = frame2ts(sf.entries[i].frameStop, frameRate); |
| 99 | } |
| 100 | } else { |
| 101 | // convert timestamps to frames |
| 102 | for (int i = 0; i < sf.entries.size(); ++i) { |
| 103 | sf.entries[i].frameStart = |
| 104 | ts2frame(sf.entries[i].frameStart, frameRate); |
| 105 | sf.entries[i].frameStop = ts2frame(sf.entries[i].frameStop, frameRate); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if (fpsRatio != 1.0) { |
| 111 | for (int i = 0; i < sf.entries.size(); ++i) { |
| 112 | sf.entries[i].frameStart = |
| 113 | (long)floor(fpsRatio * sf.entries[i].frameStart); |
| 114 | sf.entries[i].frameStop = (long)floor(fpsRatio * sf.entries[i].frameStop); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (delayOffset != 0.0) { |
| 119 | long offset = (long)(delayOffset * 1000.0); |
| 120 | |
| 121 | if (targetFormat->isTimeBased()) { |
| 122 | for (int i = 0; i < sf.entries.size(); ++i) { |
| 123 | sf.entries[i].frameStart = |
| 124 | std::max(0L, sf.entries[i].frameStart + offset); |
| 125 | sf.entries[i].frameStop = |
| 126 | std::max(0L, sf.entries[i].frameStop + offset); |
| 127 | } |
| 128 | } else { |
| 129 | if (frameRate <= 0.0) frameRate = determineFPS(); |
| 130 | if (frameRate <= 0.0) return false; |
| 131 | |
| 132 | for (int i = 0; i < sf.entries.size(); ++i) { |
| 133 | sf.entries[i].frameStart = std::max( |
| 134 | 0L, ts2frame(frame2ts(sf.entries[i].frameStart, frameRate) + offset, |
| 135 | frameRate)); |
| 136 | sf.entries[i].frameStop = std::max( |
| 137 | 0L, ts2frame(frame2ts(sf.entries[i].frameStop, frameRate) + offset, |
| 138 | frameRate)); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if (!skipConvertAds && !sf.entries.isEmpty()) { |
| 144 | SubToken stQNapi; |
| 145 | stQNapi.type = STT_WORD; |
| 146 | stQNapi.payload = "QNapi"; |
| 147 | |
| 148 | if (!sf.entries.last().tokens.contains(stQNapi)) { |
| 149 | SubEntry adEntry; |
| 150 | if (targetFormat->isTimeBased()) { |
| 151 | adEntry.frameStart = sf.entries.back().frameStop + 2000L; |
| 152 | adEntry.frameStop = adEntry.frameStart + 8000L; |
| 153 | } else { |
| 154 | adEntry.frameStart = sf.entries.back().frameStop + 50L; |
| 155 | adEntry.frameStop = adEntry.frameStart + 200L; |
| 156 | } |
| 157 | QString ad = QObject::tr("Subtitles downloaded and processed by QNapi|"); |
| 158 | adEntry.tokens = targetFormat->decodeTokenStream(ad); |
| 159 | SubToken urlToken; |
| 160 | urlToken.type = STT_WORD; |
| 161 | urlToken.payload = LibQNapi::webpageUrl(); |
| 162 | adEntry.tokens.push_back(urlToken); |
| 163 | sf.entries.push_back(adEntry); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | QStringList targetLines = targetFormat->encode(sf); |
| 168 | |
| 169 | return writeFile(targetFileName, encoding, targetLines); |
| 170 | } |
| 171 | |
| 172 | long SubtitleConverter::ts2frame(long ts, double frameRate) const { |
| 173 | return static_cast<long>(floor(frameRate * ts / 1000.0)); |
| 174 | } |
| 175 | |
| 176 | long SubtitleConverter::frame2ts(long frame, double frameRate) const { |
| 177 | return static_cast<long>(floor(1000.0 * frame / frameRate)); |
| 178 | } |
| 179 | |
| 180 | QStringList SubtitleConverter::readFile(const QString &filename, |
| 181 | QString encoding, |
| 182 | long atMostLines) const { |
| 183 | QStringList buff; |
| 184 | long current = 0; |
| 185 | QFile inputFile(filename); |
| 186 | if (inputFile.open(QIODevice::ReadOnly | QIODevice::Text)) { |
| 187 | QTextStream in(&inputFile); |
| 188 | in.setCodec(qPrintable(encoding)); |
| 189 | while (!in.atEnd() && (atMostLines == 0 || current < atMostLines)) { |
| 190 | buff += in.readLine(); |
| 191 | ++current; |
| 192 | } |
| 193 | inputFile.close(); |
| 194 | } |
| 195 | return buff; |
| 196 | } |
| 197 | |
| 198 | bool SubtitleConverter::writeFile(const QString &filename, QString encoding, |
| 199 | const QStringList &lines) const { |
| 200 | QFile outputFile(filename); |
| 201 | if (outputFile.open(QIODevice::WriteOnly | QIODevice::Text)) { |
| 202 | QTextStream out(&outputFile); |
| 203 | out.setCodec(qPrintable(encoding)); |
| 204 | foreach (QString line, lines) { out << line << "\n"; } |
| 205 | outputFile.close(); |
| 206 | return true; |
| 207 | } else { |
| 208 | return false; |
| 209 | } |
| 210 | } |