Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Extract media duration from metadata
QtWS25 Last Chance

Extract media duration from metadata

Scheduled Pinned Locked Moved Solved General and Desktop
qmediaplayerqmediaplaylist
9 Posts 3 Posters 5.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • UnitScanU Offline
    UnitScanU Offline
    UnitScan
    wrote on last edited by
    #1

    Since duration is available only while media is playing, there is another way to retrieve the media duration? I'm creating a TableWidget to display all the files in the playlist, and beyond the filename would obtain the duration.

    Can you help me?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by A Former User
      #2

      Hi! QAudioDecoder can do this for you.

      void MainWindow::on_pushButton_clicked()
      {
          QAudioDecoder *ad = new QAudioDecoder(this);
          ad->setSourceFilename("/home/user/file.mp3");
          connect(ad, &QAudioDecoder::durationChanged, this, &MainWindow::onDurationChanged);
          ad->start();
      }
      
      void MainWindow::onDurationChanged(qint64 d)
      {
          QTime t(0,0,0,0);
          t = t.addMSecs(d); 
          ui->label->setText( t.toString("hh:mm:ss") );
      }
      

      Edit: Removed code that deleted ad to early.

      1 Reply Last reply
      0
      • UnitScanU Offline
        UnitScanU Offline
        UnitScan
        wrote on last edited by
        #3

        I have already tried the QAudioDecoder class, but Qt sends me this error:

        defaultServiceProvider::requestService(): no service found for - "org.qt-project.qt.audiodecode"

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          Depending on your platform, audio decoding might not be provided by the backend. See: http://wiki.qt.io/Qt_5.7_Multimedia_Backends#Audio_plugins

          1 Reply Last reply
          0
          • UnitScanU Offline
            UnitScanU Offline
            UnitScan
            wrote on last edited by UnitScan
            #5

            I'm running Qt 5.7 on Window 7. msvc2013 installed, but QAudioDecoder still not work

            D 1 Reply Last reply
            0
            • UnitScanU UnitScan

              I'm running Qt 5.7 on Window 7. msvc2013 installed, but QAudioDecoder still not work

              D Offline
              D Offline
              Devopia53
              wrote on last edited by
              #6

              @UnitScan

              Hi.

              Do you want this?

              qlonglong duration = mediaPlayer->metaData("Duration");
              
              UnitScanU 1 Reply Last reply
              0
              • D Devopia53

                @UnitScan

                Hi.

                Do you want this?

                qlonglong duration = mediaPlayer->metaData("Duration");
                
                UnitScanU Offline
                UnitScanU Offline
                UnitScan
                wrote on last edited by UnitScan
                #7

                @Devopia53

                void MainWindow::playerOnMediaStatusChanged(QMediaPlayer::MediaStatus status) {
                    if (status == QMediaPlayer::BufferedMedia) {
                        QVariant duration = player->metaData("Duration");
                        qDebug() << duration.toLongLong();
                    }
                }
                

                returns 0

                Yes, I know, there is player->duration(), but as I mentioned early works only for the current media

                D 1 Reply Last reply
                0
                • UnitScanU UnitScan

                  @Devopia53

                  void MainWindow::playerOnMediaStatusChanged(QMediaPlayer::MediaStatus status) {
                      if (status == QMediaPlayer::BufferedMedia) {
                          QVariant duration = player->metaData("Duration");
                          qDebug() << duration.toLongLong();
                      }
                  }
                  

                  returns 0

                  Yes, I know, there is player->duration(), but as I mentioned early works only for the current media

                  D Offline
                  D Offline
                  Devopia53
                  wrote on last edited by
                  #8

                  @UnitScan

                  Yeah, you're right.
                  However, it is possible resolved by using a few tricks.
                  like this:

                      auto    playlist = new QMediaPlaylist;
                      playlist->addMedia(QUrl::fromLocalFile("/Sample/Music/1.mp3"));
                      playlist->addMedia(QUrl::fromLocalFile("/Sample/Music/2.mp3"));
                      playlist->addMedia(QUrl::fromLocalFile("/Sample/Music/3.mp3"));
                      player->setPlaylist(playlist);
                      playlist->setCurrentIndex(0);
                      //player->play();
                  
                      static bool     isExtractMode = true;
                      connect(player, &QMediaPlayer::mediaStatusChanged,
                              [&, playlist](QMediaPlayer::MediaStatus status){
                          if (isExtractMode) {
                              if (status == QMediaPlayer::LoadedMedia) {
                                  qDebug() << "Duration: " << player->metaData("Duration");
                  
                                  int     index = playlist->nextIndex();
                                  if (index < 0) {
                                      isExtractMode = false;
                                      playlist->setCurrentIndex(0);
                                  }
                                  else
                                      playlist->setCurrentIndex(index);
                              }
                          }
                      });
                  
                  
                  1 Reply Last reply
                  0
                  • UnitScanU Offline
                    UnitScanU Offline
                    UnitScan
                    wrote on last edited by
                    #9

                    [SOLVED]

                    I had to resort to the help of TagLibs library. And since the documentation available how to build this library to make it interact with Qt are very confusing and outdated, I proceeded to make available the library that I managed, with great difficulty, to compile by my hand:

                    http://www.unitscan.org/archives/301

                    I hope will be usefull

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved