Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. How can I extract metadata from a music file?
QtWS25 Last Chance

How can I extract metadata from a music file?

Scheduled Pinned Locked Moved Solved Qt 6
7 Posts 4 Posters 1.1k 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.
  • P Offline
    P Offline
    Pbaodoge
    wrote on 28 Jun 2022, 04:44 last edited by
    #1

    Hello, I'm trying to extract metadata from music files so that I can use it for specific applications. I tried to use QMediaPlayer. However, as I'm using Qt 6, some metadata functions have been removed. Therefore, I'm looking for a solution to extract the metadata, so that I can use titles, artist names,... etc, etc.
    So this code in Qt 6 is completely failed:

        QMediaPlayer *player = new QMediaPlayer;
        player->setSource(QUrl::fromLocalFile("music.flac"));
        QStringList mdlist = player->availableMetaData(); // No member named 'availableMetaData' in 'QMediaPlayer'
    

    If you know how can fix this, please help me. Thank you!

    J 1 Reply Last reply 28 Jun 2022, 05:17
    0
    • P Pbaodoge
      28 Jun 2022, 06:12

      I tried with metaData, but it is still failed:

          QMediaPlayer *player = new QMediaPlayer;
          player->setSource(QUrl::fromLocalFile("music.flac"));
          QStringList mdlist = player->metaData(); //Calling 'metaData' with incomplete return type 'QMediaMetaData'
      

      Maybe my code is wrong or something?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 28 Jun 2022, 06:17 last edited by
      #4

      @Pbaodoge said in How can I extract metadata from a music file?:

      Calling 'metaData' with incomplete return type 'QMediaMetaData'

      you have to include QMediaMetaData header file as shown in https://doc-snapshots.qt.io/qt6-dev/qmediametadata.html

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • P Pbaodoge
        28 Jun 2022, 04:44

        Hello, I'm trying to extract metadata from music files so that I can use it for specific applications. I tried to use QMediaPlayer. However, as I'm using Qt 6, some metadata functions have been removed. Therefore, I'm looking for a solution to extract the metadata, so that I can use titles, artist names,... etc, etc.
        So this code in Qt 6 is completely failed:

            QMediaPlayer *player = new QMediaPlayer;
            player->setSource(QUrl::fromLocalFile("music.flac"));
            QStringList mdlist = player->availableMetaData(); // No member named 'availableMetaData' in 'QMediaPlayer'
        

        If you know how can fix this, please help me. Thank you!

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 28 Jun 2022, 05:17 last edited by
        #2

        @Pbaodoge What about https://doc.qt.io/qt-6/qmediaplayer.html#metaData ?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Pbaodoge
          wrote on 28 Jun 2022, 06:12 last edited by
          #3

          I tried with metaData, but it is still failed:

              QMediaPlayer *player = new QMediaPlayer;
              player->setSource(QUrl::fromLocalFile("music.flac"));
              QStringList mdlist = player->metaData(); //Calling 'metaData' with incomplete return type 'QMediaMetaData'
          

          Maybe my code is wrong or something?

          J J 2 Replies Last reply 28 Jun 2022, 06:17
          0
          • P Pbaodoge
            28 Jun 2022, 06:12

            I tried with metaData, but it is still failed:

                QMediaPlayer *player = new QMediaPlayer;
                player->setSource(QUrl::fromLocalFile("music.flac"));
                QStringList mdlist = player->metaData(); //Calling 'metaData' with incomplete return type 'QMediaMetaData'
            

            Maybe my code is wrong or something?

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 28 Jun 2022, 06:17 last edited by
            #4

            @Pbaodoge said in How can I extract metadata from a music file?:

            Calling 'metaData' with incomplete return type 'QMediaMetaData'

            you have to include QMediaMetaData header file as shown in https://doc-snapshots.qt.io/qt6-dev/qmediametadata.html

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            3
            • P Pbaodoge
              28 Jun 2022, 06:12

              I tried with metaData, but it is still failed:

                  QMediaPlayer *player = new QMediaPlayer;
                  player->setSource(QUrl::fromLocalFile("music.flac"));
                  QStringList mdlist = player->metaData(); //Calling 'metaData' with incomplete return type 'QMediaMetaData'
              

              Maybe my code is wrong or something?

              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 28 Jun 2022, 06:21 last edited by
              #5

              @Pbaodoge in addition to what @jsulm said,

              metaData() returns a QVariant not a QStringList, and expects a key as argument
              https://doc.qt.io/qt-5/qmediaobject.html#metaData
              and
              https://doc.qt.io/qt-5/qmediametadata.html


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              3
              • P Offline
                P Offline
                Pbaodoge
                wrote on 29 Jun 2022, 05:04 last edited by
                #6

                @jsulm @J-Hilk
                Thanks, that works. However, it returns a blank QString. This means that it can't fetch the metadata information. That seems to be a big problem.
                Here's my code:

                #include "mainwindow.hpp"
                
                MainWindow::MainWindow(QWidget *parent)
                    : QMainWindow(parent)
                {
                    player = new QMediaPlayer;
                    player->setSource(QUrl::fromLocalFile(path));
                    connect(player, &QMediaPlayer::mediaStatusChanged, this, &MainWindow::mediaLoaded); //wait until media is loaded, then execute the function
                    player->setSource(QUrl::fromLocalFile(path));
                };
                
                void MainWindow::mediaLoaded() {
                    if(player->mediaStatus() == QMediaPlayer::LoadedMedia)  { // which means that the media is loaded
                         QMediaMetaData mdData = player->metaData();
                         QVariant var = mdData[QMediaMetaData::Genre];
                         qDebug() << var.toString();
                    }
                }
                

                It outputs nothing. I checked every part of the program with qDebug(). everything works just fine. The result is just nothing.

                J 1 Reply Last reply 29 Jun 2022, 08:23
                0
                • P Pbaodoge
                  29 Jun 2022, 05:04

                  @jsulm @J-Hilk
                  Thanks, that works. However, it returns a blank QString. This means that it can't fetch the metadata information. That seems to be a big problem.
                  Here's my code:

                  #include "mainwindow.hpp"
                  
                  MainWindow::MainWindow(QWidget *parent)
                      : QMainWindow(parent)
                  {
                      player = new QMediaPlayer;
                      player->setSource(QUrl::fromLocalFile(path));
                      connect(player, &QMediaPlayer::mediaStatusChanged, this, &MainWindow::mediaLoaded); //wait until media is loaded, then execute the function
                      player->setSource(QUrl::fromLocalFile(path));
                  };
                  
                  void MainWindow::mediaLoaded() {
                      if(player->mediaStatus() == QMediaPlayer::LoadedMedia)  { // which means that the media is loaded
                           QMediaMetaData mdData = player->metaData();
                           QVariant var = mdData[QMediaMetaData::Genre];
                           qDebug() << var.toString();
                      }
                  }
                  

                  It outputs nothing. I checked every part of the program with qDebug(). everything works just fine. The result is just nothing.

                  J Offline
                  J Offline
                  JonB
                  wrote on 29 Jun 2022, 08:23 last edited by JonB
                  #7

                  @Pbaodoge said in How can I extract metadata from a music file?:

                       QVariant var = mdData[QMediaMetaData::Genre];
                       qDebug() << var.toString();
                  

                  Please read the docs carefully! :) QMediaMetaData::Genre returns a QStringList. QVariant::toString() does not list that as a type it can convert to a string, and then

                  Calling QVariant::toString() on an unsupported variant returns an empty string.

                  Rewrite your code appropriately. You might also try QMediaMetaData::keys() to see what's there and QMediaMetaData::stringValue(QMediaMetaData::Key key) const.

                  1 Reply Last reply
                  3

                  6/7

                  29 Jun 2022, 05:04

                  • Login

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