Important: Please read the Qt Code of Conduct -

Extract media duration from metadata

  • 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?

  • Hi! QAudioDecoder can do this for you.

    void MainWindow::on_pushButton_clicked()
        QAudioDecoder *ad = new QAudioDecoder(this);
        connect(ad, &QAudioDecoder::durationChanged, this, &MainWindow::onDurationChanged);
    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.

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

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

  • Depending on your platform, audio decoding might not be provided by the backend. See:

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

  • @UnitScan


    Do you want this?

    qlonglong duration = mediaPlayer->metaData("Duration");

  • @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

  • @UnitScan

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

        auto    playlist = new QMediaPlaylist;
        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;

  • [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:

    I hope will be usefull