Ticket #6240: patch_external_subtitles.diff

File patch_external_subtitles.diff, 3.2 KB (added by jo@…, 11 years ago)

patch external subtitles

  • libs/libmythtv/playercontext.cpp

     
    11#include <math.h>
    22#include <qtimer.h>
    33#include <qpainter.h>
     4#include <QDir>
    45
    56#include "playercontext.h"
    67#include "NuppelVideoPlayer.h"
     
    455456        _nvp->SetNoAudio();
    456457    else
    457458    {
    458         _nvp->LoadExternalSubtitles(buffer->GetFilename());
     459        //_nvp->LoadExternalSubtitles(buffer->GetFilename());
     460        LoadExternalSubtitles(_nvp, buffer->GetFilename());
    459461    }
    460462
    461463    if ((embedwinid > 0) && embedbounds)
     
    527529    return StartDecoderThread(maxWait);
    528530}
    529531
     532bool PlayerContext::LoadExternalSubtitles(NuppelVideoPlayer *nvp,
     533                               const QString &videoFile)
     534{
     535    if (videoFile.isEmpty())
     536        return false;
     537
     538    QString fileName = videoFile;
     539    QString dirName  = ".";
     540
     541    int dirPos = videoFile.lastIndexOf(QChar('/'));
     542    if (dirPos > 0)
     543    {
     544        fileName = videoFile.mid(dirPos + 1);
     545        dirName = videoFile.left(dirPos);
     546    }
     547
     548    QString baseName = fileName;
     549    int suffixPos = fileName.lastIndexOf(QChar('.'));
     550    if (suffixPos > 0)
     551        baseName = fileName.left(suffixPos);
     552
     553    // The dir listing does not work if the filename has the following chars,
     554    // so we convert them to the wildcard '?'
     555    baseName = baseName.replace("[", "?").replace("]", "?");
     556    baseName = baseName.replace("(", "?").replace(")", "?");
     557
     558    // Some Qt versions do not accept paths in the search string of
     559    // entryList() so we have to set the dir first
     560    QDir dir;
     561    dir.setPath(dirName);
     562
     563    // Try to find files with the same base name, but ending with
     564    // '.srt', '.sub', or '.txt'
     565    QStringList el;
     566    el += baseName + "*.srt";
     567    el += baseName + "*.sub";
     568    el += baseName + "*.txt";
     569    QStringList candidates = dir.entryList(el);
     570
     571    bool found = false;
     572    QString candidate = "";
     573    QStringList::const_iterator it = candidates.begin();
     574    for (; (it != candidates.end()) && !found; ++it)
     575    {
     576        candidate = dirName + "/" + *it;
     577        if (nvp->LoadExternalSubtitles(candidate))
     578            found = true;
     579    }
     580
     581    if (found)
     582    {
     583        VERBOSE(VB_PLAYBACK, LOC +
     584                QString("Loaded text subtitles from '%1'.").arg(candidate));
     585    }
     586
     587    return found;
     588}
     589
    530590/** \fn PlayerContext::StartDecoderThread(int)
    531591 *  \brief Starts player, must be called after StartRecorder().
    532592 *  \param maxWait How long to wait for NuppelVideoPlayer to start playing.
  • libs/libmythtv/playercontext.h

     
    4747    bool CreateNVP(TV *tv, QWidget *widget,
    4848                   TVState desiredState,
    4949                   WId embedwinid, const QRect *embedBounds);
     50    bool LoadExternalSubtitles(NuppelVideoPlayer *nvp,
     51                                              const QString &videoFile);
    5052    void TeardownPlayer(void);
    5153    bool StartDecoderThread(int maxWait = -1);
    5254    bool StartOSD(TV *tv);