How to force QT5 MediaPlayer to show Subtitles?

后端 未结 2 2088
日久生厌
日久生厌 2021-01-26 12:45

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

相关标签:
2条回答
  • 2021-01-26 13:12

    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.

    0 讨论(0)
  • 2021-01-26 13:26

    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);
          }
      }
    }
    
    0 讨论(0)
提交回复
热议问题