Ticket #2225: ext_subtitle_file_fixes.patch
File ext_subtitle_file_fixes.patch, 3.4 KB (added by , 19 years ago) |
---|
-
libs/libmythtv/tv_play.cpp
Improvements and fixes to the external (text) subtitle file support: * Fix printing the loaded subtitle file to the log. * Fix loading of UTF8 subs: detect if the strings cannot be presented as valid UTF8 chars and fall back to Latin1 in that case (TODO: add user option to select the fall back encoding, as it's not possible to reliably detect the 8-bit encoding)
old new 7053 7053 baseName + "*.srt; " + baseName + "*.sub; " + baseName + "*.txt;"); 7054 7054 7055 7055 bool found = false; 7056 QString candidate = ""; 7056 7057 QStringList::const_iterator it = candidates.begin(); 7057 7058 for (; (it != candidates.end()) && !found; ++it) 7058 7059 { 7059 if (nvp->LoadExternalSubtitles(dirName + "/" + *it)) 7060 candidate = dirName + "/" + *it; 7061 if (nvp->LoadExternalSubtitles(candidate)); 7060 7062 found = true; 7061 7063 } 7062 7064 7063 7065 if (found) 7064 7066 { 7065 7067 VERBOSE(VB_PLAYBACK, LOC + 7066 QString("Loaded text subtitles from '%1'.").arg( *it));7068 QString("Loaded text subtitles from '%1'.").arg(candidate)); 7067 7069 } 7068 7070 7069 7071 return found; -
libs/libmythtv/textsubtitleparser.cpp
old new 5 5 */ 6 6 7 7 #include <stdio.h> 8 #include < qtextcodec.h>8 #include <cstring> 9 9 #include <algorithm> 10 #include <qtextcodec.h> 10 11 11 12 using std::lower_bound; 12 13 … … 115 116 116 117 target.SetFrameBasedTiming(!sub_data.uses_time); 117 118 119 120 // assume the text to be in UTF8 compatible format until encountering 121 // a string of which characters cannot be encoded by UTF8 122 QTextCodec* utf8Codec = QTextCodec::codecForName("utf8"); 123 assert(utf8Codec); 124 125 bool utf8 = true; 126 118 127 // convert the subtitles to our own format and free the original structures 119 128 for (int sub_i = 0; sub_i < sub_data.num; ++sub_i) 120 129 { … … 129 138 130 139 for (int line = 0; line < sub->lines; ++line) 131 140 { 132 newsub.textLines.push_back(QString(sub->text[line])); 141 const char *subLine = sub->text[line]; 142 int lineLength = std::strlen(subLine); 143 144 // check if the string contains unknown chars to UTF8, 145 // if the encoding has not been detected to be non-UTF8 146 // before (the heuristics method can sometimes return 147 // larger than lineLength for unknown reason, thus the >= ) 148 utf8 = utf8 && 149 (utf8Codec->heuristicContentMatch(subLine, lineLength) >= lineLength); 150 151 // use Latin1 as the fall back encoding in case there was 152 // non-UTF8 strings detected 153 // TODO: user option for setting the fall back encoding could 154 // be nice (it's impossible to detect the correct 8-bit encoding 155 // automatically) 156 if (utf8) 157 newsub.textLines.push_back(QString::fromUtf8(subLine)); 158 else 159 newsub.textLines.push_back(QString::fromLatin1(subLine)); 160 133 161 free(sub->text[line]); 134 162 } 135 136 163 target.AddSubtitle(newsub); 137 164 } 138 165