How to force QT5 MediaPlayer to show Subtitles?

本小妞迷上赌 提交于 2020-05-09 14:01:27

问题


I am evaluating a migration from Qt 4.8 towards Qt 5.2 and the most important point is the multimedia backend. In Qt 5.2 there are some important features the Phonon backend in Qt 4.8 does not provide. But at least the elder version showed subtitles (SRT file in the same directory as the video file).

Neither the documentation nor the trial-and-error provided me any results.

So, does anyone know how to FORCE Qt 5 to show these subtitles? Or isnt it even supported (would be a shame)

Any help is appreciated..

PS: I need exactly the opposite: Disable showing subtitle file in QMediaPlayer


回答1:


You have to set the flag GST_PLAY_FLAG_TEXT on playbin2. (It is usually on by default. If need be then change it in the ctor of QGstreamerPlayerSession).

And if your subtitle file is external then you will have to set the "suburi" property on playbin2. The value of the suburi property is the path to the subtitle file. This change should be done in the method QGstreamerPlayerSession::loadFromUri.

In Qt5.2 these changes have to be done in the file qgstreamerplayersession.cpp.You will find the file at qtmultimedia/src/plugins/gstreamer/mediaplayer. The file location may be different for the older 4.x versions.

The other thing I observed is that the plugin code sets the flag GST_PLAY_FLAG_NATIVE_VIDEO. The subtitles do not show up if this flag is set. You will have to prevent the plugin code from setting this flag. Either you can comment out the code that sets this flag or you will have to set the environment variable QT_GSTREAMER_PLAYBIN_FLAGS to 0x00000017 ( which is GST_PLAY_FLAG_VIDEO|GST_PLAY_FLAG_AUDIO|GST_PLAY_FLAG_TEXT). Setting it to any value will skip the GST_PLAY_FLAG_NATIVE_VIDEO flag.

After you make these changes, build the plugin and use it.




回答2:


I searched a lot, apparently Qt mediaplayer does not support subtitle, I use srt parser to show subtitle.

My BackEnd class code:

 #include "backend.h"
BackEnd::BackEnd(QObject *parent) : QObject(parent)
{
     readSubtitleFile("./en.srt");
}

void BackEnd::readSubtitleFile(QString directory)
{
      cout<< "readSubtitleFile:"<< directory.toStdString()<<endl;
      if(!isFileExist(directory.toStdString())) {
          cout<< "file does not exist"<<endl;
          return ;
      }
      SubtitleParserFactory *subParserFactory = new SubtitleParserFactory(directory.toStdString());
      SubtitleParser *parser = subParserFactory->getParser();

      sub = parser->getSubtitles();
}

QString BackEnd::getSubtitleText(double playTime)
{
    for(SubtitleItem * element : sub) {
        double startTime = element->getStartTime();
        double endTime = element->getEndTime();
        if( (startTime <= playTime) && (playTime <= endTime)) {
            cout<< "getSubtitleText: founded"<< element->getText()<<endl;
            return QString::fromStdString(element->getText());
        }
    }
    cout<< "getSubtitleText: not founded"<< endl;
    return "";
}

bool BackEnd::isFileExist(const string &temp)
{
    if (FILE *file = fopen(temp.c_str(), "r")) {
            fclose(file);
            return true;
        } else {
            return false;
        }
}

My qml file:

    Window {
    visible: true
    width: 900
    height: 700
    title: qsTr("My Player")
      Rectangle {
        id: root
        color: "black"
        width: parent.width
        height: parent.height
        function msToTime(duration) {
          var milliseconds=parseInt((duration%1000)/100),
          seconds = Math.floor((duration / 1000) % 60),
        minutes = Math.floor((duration / (1000 * 60)) % 60),
        hours = Math.floor((duration / (1000 * 60 * 60)) % 24);

      hours = (hours < 10) ? "0" + hours : hours;
      minutes = (minutes < 10) ? "0" + minutes : minutes;
      seconds = (seconds < 10) ? "0" + seconds : seconds;

      return hours + ":" + minutes + ":" + seconds;
    }
    Column {
        width: parent.width
        height: parent.height
        Item {
            width: parent.width
            height: parent.height-100
            MediaPlayer {
                id: mediaplayer
                source: "file:///E:/1.mp4"
            }

            VideoOutput {
                anchors.fill: parent
                source: mediaplayer
            }
        }
        Text {
            id: subtitleText
            text: qsTr("")
            y: -150
            font.pixelSize: 18
            color: "white"
            anchors.horizontalCenter: 
         parent.horizontalCenter
        }
    }
    Timer {
      id: refreshTimer
      interval: 1000//30 // 60 Hz
      running: true
      repeat: true
      onTriggered: {
         durationPass.text =  
            root.msToTime(mediaplayer.position);
         subtitleText.text = 

        BackEnd.getSubtitleText(mediaplayer.position);
      }
  }
}


来源:https://stackoverflow.com/questions/21219633/how-to-force-qt5-mediaplayer-to-show-subtitles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!