Ticket #9836: srt.patch

File srt.patch, 4.3 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..552b83b 100644
    a b 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
     323    // try and determine the text codec
     324    QByteArray test(sub_data.rbuffer_text, sub_data.rbuffer_len);
     325    QTextCodec *textCodec = QTextCodec::codecForUtfText(test, NULL);
     326    if (!textCodec)
     327    {
     328        LOG(VB_VBI, LOG_WARNING, "Failed to autodetect a UTF encoding.");
     329        QString codec = gCoreContext->GetSetting("SubtitleCodec", "");
     330        if (!codec.isEmpty())
     331            textCodec = QTextCodec::codecForName(codec.toLatin1());
     332        if (!textCodec)
     333            textCodec = QTextCodec::codecForName("utf-8");
     334        if (!textCodec)
     335        {
     336            LOG(VB_VBI, LOG_ERR,
     337                QString("Failed to find codec for subtitle file '%1'")
     338                .arg(fileName));
     339            return;
     340        }
     341    }
     342
     343    LOG(VB_VBI, LOG_INFO, QString("Opened subtitle file '%1' with codec '%2'")
     344        .arg(fileName).arg(textCodec->name().constData()));
     345
     346    // load the entire subtitle file, converting to unicode as we go
     347    QScopedPointer<QTextDecoder> dec(textCodec->makeDecoder());
     348    QString data = dec->toUnicode(sub_data.rbuffer_text, sub_data.rbuffer_len);
     349    if (data.isEmpty())
    323350    {
    324         delete[] sub_data.rbuffer_text;
     351        LOG(VB_VBI, LOG_WARNING,
     352            QString("Data loaded from subtitle file '%1' is empty.")
     353            .arg(fileName));
    325354        return;
    326355    }
    327356
    328     target.SetFrameBasedTiming(!sub_data.uses_time);
    329     target.Clear();
     357    // convert back to utf-8 for parsing
     358    QByteArray ba = data.toUtf8();
     359    delete[] sub_data.rbuffer_text;
     360    sub_data.rbuffer_text = (char*)ba.data();
     361    sub_data.rbuffer_len = data.size();
    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    if (textCodec)
     380        dec.reset(textCodec->makeDecoder());
    344381
    345     // convert the subtitles to our own format and free the original structures
    346382    for (int sub_i = 0; sub_i < sub_data.num; ++sub_i)
    347383    {
    348384        const subtitle_t *sub = &loaded_subs[sub_i];
    void TextSubtitleParser::LoadSubtitles(const QString &fileName, 
    357393        for (int line = 0; line < sub->lines; ++line)
    358394        {
    359395            const char *subLine = sub->text[line];
    360             QString str = dec->toUnicode(subLine, strlen(subLine));
     396            //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}