Skip to content

Commit

Permalink
Post-processing: Convert character encoding only when needed (QNapi#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
baczus authored and a-sum-duma committed Aug 3, 2020
1 parent 5fc6b0a commit 0d62c60
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
32 changes: 18 additions & 14 deletions libqnapi/src/subtitlepostprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ SubtitlePostProcessor::SubtitlePostProcessor(

void SubtitlePostProcessor::perform(const QString& movieFilePath,
const QString& subtitleFilePath) const {
if (!QFileInfo(subtitleFilePath).exists()) return;

if (ppConfig.removeLines()) {
ppRemoveLinesContainingWords(subtitleFilePath, ppConfig.removeLinesWords());
}
Expand All @@ -35,11 +37,9 @@ void SubtitlePostProcessor::perform(const QString& movieFilePath,
ppReplaceDiacriticsWithASCII(subtitleFilePath);
break;
case ECM_CHANGE:
if (!ppConfig.encodingAutoDetectFrom() ||
!ppChangeSubtitlesEncoding(subtitleFilePath, ppConfig.encodingTo())) {
ppChangeSubtitlesEncoding(subtitleFilePath, ppConfig.encodingFrom(),
ppChangeSubtitlesEncoding(subtitleFilePath,
ppDetermineEncodingFrom(subtitleFilePath),
ppConfig.encodingTo());
}
break;
case ECM_ORIGINAL:
// Nie ruszaj pobranych napisów!
Expand All @@ -55,7 +55,6 @@ void SubtitlePostProcessor::perform(const QString& movieFilePath,

bool SubtitlePostProcessor::ppReplaceDiacriticsWithASCII(
const QString& subtitleFilePath) const {
if (!QFileInfo(subtitleFilePath).exists()) return false;

QString from = encodingUtils.detectFileEncoding(subtitleFilePath);

Expand Down Expand Up @@ -84,6 +83,12 @@ bool SubtitlePostProcessor::ppReplaceDiacriticsWithASCII(
bool SubtitlePostProcessor::ppChangeSubtitlesEncoding(
const QString& subtitleFilePath, const QString& from,
const QString& to) const {

if (QString::compare(from, ppConfig.encodingTo(), Qt::CaseInsensitive) == 0) {
// The same encoding, conversion isn't required.
return false;
}

QFile f(subtitleFilePath);
if (!f.open(QIODevice::ReadOnly)) return false;

Expand All @@ -109,20 +114,19 @@ bool SubtitlePostProcessor::ppChangeSubtitlesEncoding(
return true;
}

bool SubtitlePostProcessor::ppChangeSubtitlesEncoding(
const QString& subtitleFilePath, const QString& to) const {
if (!QFileInfo(subtitleFilePath).exists()) return false;

QString from = encodingUtils.detectFileEncoding(subtitleFilePath);

if (from.isEmpty()) return false;
QString SubtitlePostProcessor::ppDetermineEncodingFrom(const QString& subtitleFilePath) const {
if (ppConfig.encodingAutoDetectFrom()) {
QString autodetectedFrom = encodingUtils.detectFileEncoding(subtitleFilePath);
if (!autodetectedFrom.isEmpty()) {
return autodetectedFrom;
}
}

return ppChangeSubtitlesEncoding(from, to);
return ppConfig.encodingFrom();
}

bool SubtitlePostProcessor::ppRemoveLinesContainingWords(
const QString& subtitleFilePath, QStringList wordList) const {
if (!QFileInfo(subtitleFilePath).exists()) return false;

wordList = wordList.filter(QRegExp("^(.+)$"));

Expand Down
3 changes: 1 addition & 2 deletions libqnapi/src/subtitlepostprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ class SubtitlePostProcessor {
bool ppReplaceDiacriticsWithASCII(const QString& subtitleFilePath) const;
bool ppChangeSubtitlesEncoding(const QString& subtitleFilePath,
const QString& from, const QString& to) const;
bool ppChangeSubtitlesEncoding(const QString& subtitleFilePath,
const QString& to) const;
QString ppDetermineEncodingFrom(const QString& subtitleFilePath) const;
bool ppRemoveLinesContainingWords(const QString& subtitleFilePath,
QStringList wordList) const;

Expand Down

0 comments on commit 0d62c60

Please sign in to comment.