Ticket #9836: srt2.patch

File srt2.patch, 4.7 KB (added by Jim Stichnoth, 7 years ago)
  • mythtv/libs/libmythtv/textsubtitleparser.cpp

    diff --git a/mythtv/libs/libmythtv/textsubtitleparser.cpp b/mythtv/libs/libmythtv/textsubtitleparser.cpp
    index 28265c6..3e64e2e 100644
    a b public: 
    9999        {
    100100            m_remoteFile = NULL;
    101101            m_localFile = new QFile(filename);
    102             if (!m_localFile->open(QIODevice::ReadOnly | QIODevice::Text))
     102            if (!m_localFile->open(QIODevice::ReadOnly))
    103103            {
    104104                delete m_localFile;
    105105                m_localFile = NULL;
    void TextSubtitleParser::LoadSubtitles(const QString &fileName, 
    318318    LOG(VB_VBI, LOG_INFO,
    319319        QString("Finished reading %1 subtitle bytes (requested %2)")
    320320        .arg(numread).arg(new_len));
    321     subtitle_t *loaded_subs = sub_read_file(&sub_data);
    322     if (!loaded_subs)
     321
     322    // try to determine the text codec
     323    QByteArray test(sub_data.rbuffer_text, sub_data.rbuffer_len);
     324    QTextCodec *textCodec = QTextCodec::codecForUtfText(test, NULL);
     325    if (!textCodec)
     326    {
     327        LOG(VB_VBI, LOG_WARNING, "Failed to autodetect a UTF encoding.");
     328        QString codec = gCoreContext->GetSetting("SubtitleCodec", "");
     329        if (!codec.isEmpty())
     330            textCodec = QTextCodec::codecForName(codec.toLatin1());
     331        if (!textCodec)
     332            textCodec = QTextCodec::codecForName("utf-8");
     333        if (!textCodec)
     334        {
     335            LOG(VB_VBI, LOG_ERR,
     336                QString("Failed to find codec for subtitle file '%1'")
     337                .arg(fileName));
     338            return;
     339        }
     340    }
     341
     342    LOG(VB_VBI, LOG_INFO, QString("Opened subtitle file '%1' with codec '%2'")
     343        .arg(fileName).arg(textCodec->name().constData()));
     344
     345    // load the entire subtitle file, converting to unicode as we go
     346    QScopedPointer<QTextDecoder> dec(textCodec->makeDecoder(/*QTextCodec::IgnoreHeader*/));
     347    QString data = dec->toUnicode(sub_data.rbuffer_text, sub_data.rbuffer_len);
     348    if (data.isEmpty())
    323349    {
    324         delete[] sub_data.rbuffer_text;
     350        LOG(VB_VBI, LOG_WARNING,
     351            QString("Data loaded from subtitle file '%1' is empty.")
     352            .arg(fileName));
    325353        return;
    326354    }
    327355
    328     target.SetFrameBasedTiming(!sub_data.uses_time);
    329     target.Clear();
     356    // convert back to utf-8 for parsing
     357    QByteArray ba = data.toUtf8();
     358    //delete[] sub_data.rbuffer_text;
     359    sub_data.rbuffer_text = (char*)ba.data();
     360    sub_data.rbuffer_len = ba.size();
     361    LOG(VB_VBI, LOG_INFO, QString("UTF-8 subs: %1").arg(QString(sub_data.rbuffer_text).replace(QChar(0), "NUL")));
    330362
    331     QTextCodec *textCodec = NULL;
    332     QString codec = gCoreContext->GetSetting("SubtitleCodec", "");
    333     if (!codec.isEmpty())
    334         textCodec = QTextCodec::codecForName(codec.toLatin1());
    335     if (!textCodec)
    336         textCodec = QTextCodec::codecForName("utf-8");
    337     if (!textCodec)
     363    subtitle_t *loaded_subs = sub_read_file(&sub_data);
     364    if (!loaded_subs)
    338365    {
    339         delete[] sub_data.rbuffer_text;
     366        //delete[] sub_data.rbuffer_text;
     367        LOG(VB_VBI, LOG_ERR, QString("Failed to read subtitles from '%1'")
     368            .arg(fileName));
    340369        return;
    341370    }
    342371
    343     QTextDecoder *dec = textCodec->makeDecoder();
     372    LOG(VB_VBI, LOG_INFO, QString("Found %1 subtitles in file '%2'")
     373        .arg(sub_data.num).arg(fileName));
     374    target.SetFrameBasedTiming(!sub_data.uses_time);
     375    target.Clear();
     376
     377    // convert the subtitles to our own format, free the original structures
     378    // and convert back to unicode
     379    textCodec = QTextCodec::codecForName("utf-8");
     380    if (textCodec)
     381        dec.reset(textCodec->makeDecoder());
    344382
    345     // convert the subtitles to our own format and free the original structures
    346383    for (int sub_i = 0; sub_i < sub_data.num; ++sub_i)
    347384    {
    348385        const subtitle_t *sub = &loaded_subs[sub_i];
    void TextSubtitleParser::LoadSubtitles(const QString &fileName, 
    357394        for (int line = 0; line < sub->lines; ++line)
    358395        {
    359396            const char *subLine = sub->text[line];
    360             QString str = dec->toUnicode(subLine, strlen(subLine));
     397            QString str;
     398            if (textCodec)
     399                str = dec->toUnicode(subLine, strlen(subLine));
     400            else
     401                str = QString(subLine);
    361402            newsub.textLines.push_back(str);
    362403
    363404            free(sub->text[line]);
    void TextSubtitleParser::LoadSubtitles(const QString &fileName, 
    365406        target.AddSubtitle(newsub);
    366407    }
    367408
    368     delete dec;
     409    //delete dec;
    369410    // textCodec object is managed by Qt, do not delete...
    370411
    371412    free(loaded_subs);
    372     delete[] sub_data.rbuffer_text;
     413    //delete[] sub_data.rbuffer_text;
    373414
    374415    target.SetLastLoaded();
    375416}