How can I extract metadata from a music file?
-
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!
-
@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
-
-
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?
-
@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
-
@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 -
@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.
-
@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 aQStringList
.QVariant::toString()
does not list that as a type it can convert to a string, and thenCalling 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 andQMediaMetaData::stringValue(QMediaMetaData::Key key) const
.