Helg1980
Helg1980

Reputation: 129

How to use QMediaMetaData correctly to define media files parameters?

I need to determine the parameters of the audio file: bitrate, sampling rate, bit depth, audio format.

I have tried to use it like that:

#include <QtMultimedia/QMediaMetaData>
#include <QtMultimedia/QMediaObject>
#include "mainwindow.h"
#include "ui_mainwindow.h"
.
.
.
void MainWindow::on_pushButton_1_clicked()
{
   QMediaObject mediafile;
   QString file_name = "/run/media/helg/WDC/test.aac";   
   mediafile.setMedia(QUrl(file_name));
   QString bitrate = mediafile.metaData(QMediaMetaData::AudioBitRate).toString();
   .
   .
   .
}
.
.
.

But it doesn't work:

error: no matching constructor for initialization of QMediaObject.

I'm not even sure if I should use a QMediaObject or something else.

Upvotes: 3

Views: 1771

Answers (2)

Mehran Khazaeizadeh
Mehran Khazaeizadeh

Reputation: 1115

You're welcome.


  1. Define a player variable:
private:
    QMediaPlayer *m_player;

In your code:

   m_player = new QMediaPlayer(this);
  connect(m_player, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)), this, SLOT(onMediaStatusChanged(QMediaPlayer::MediaStatus)));

   m_player->setMedia(QUrl::fromLocalFile("C:\\yourMediaFileAddress"));
  1. Define the onMediaStatusChanged slot e.g. as follows:
void MainWindow::onMediaStatusChanged(QMediaPlayer::MediaStatus status)
{
    if (status == QMediaPlayer::LoadedMedia)
        GetMetaData(m_player);
}

The GetMetaData() method lists all available metadata:

void MainWindow::GetMetaData(QMediaPlayer *player)
{
   // Get the list of keys there is metadata available for
   QStringList metadatalist = player->availableMetaData();

   // Get the size of the list
   int list_size = metadatalist.size();

   //qDebug() << player->isMetaDataAvailable() << list_size;

   // Define variables to store metadata key and value
   QString metadata_key;
   QVariant var_data;

   for (int indx = 0; indx < list_size; indx++)
   {
     // Get the key from the list
     metadata_key = metadatalist.at(indx);

     // Get the value for the key
     var_data = player->metaData(metadata_key);

    qDebug() << metadata_key << var_data.toString();
   }
 }

The output on a sample media file is something like this:

"AudioBitRate" "128000"
"AudioCodec" "MPEG Audio Layer-3 (MP3)"
"ChannelCount" "2"
"Copyright" "SaM"
"Duration" "5069876"
"PixelAspectRatio" ""
"Resolution" ""
"SampleRate" "48000"
"VideoBitRate" "1028056"
"VideoCodec" "MPEG-4 part 2 Video (MP4V)"
"VideoFrameRate" "24"

Upvotes: 1

Mehran Khazaeizadeh
Mehran Khazaeizadeh

Reputation: 1115

The QMediaMetaData just provides media information and therefore it cannot be instantiated by developer. You can use this method:

QFile sourceFile;   // class member.
QAudioOutput* audio; // class member.
{
    sourceFile.setFileName("/tmp/test.raw");
    sourceFile.open(QIODevice::ReadOnly);

    QAudioFormat format;
    // Set up the format, eg.
    format.setSampleRate(8000);
    format.setChannelCount(1);
    format.setSampleSize(8);
    format.setCodec("audio/pcm");
    format.setByteOrder(QAudioFormat::LittleEndian);
    format.setSampleType(QAudioFormat::UnSignedInt);

    QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
    if (!info.isFormatSupported(format)) {
        qWarning() << "Raw audio format not supported by backend, cannot play audio.";
        return;
    }

    audio = new QAudioOutput(format, this);
    connect(audio, SIGNAL(stateChanged(QAudio::State)), this, SLOT(handleStateChanged(QAudio::State)));
    audio->start(&sourceFile);
}

Reference: https://doc.qt.io/qt-5/qaudiooutput.html

Upvotes: 1

Related Questions