Extract media duration from metadata
-
wrote on 2 Aug 2016, 09:48 last edited by
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?
-
wrote on 2 Aug 2016, 10:27 last edited by A Former User 8 Feb 2016, 10:47
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. -
wrote on 2 Aug 2016, 10:34 last edited by
I have already tried the QAudioDecoder class, but Qt sends me this error:
defaultServiceProvider::requestService(): no service found for - "org.qt-project.qt.audiodecode"
-
wrote on 2 Aug 2016, 10:49 last edited by
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
-
wrote on 2 Aug 2016, 10:54 last edited by UnitScan 8 Feb 2016, 10:57
I'm running Qt 5.7 on Window 7. msvc2013 installed, but QAudioDecoder still not work
-
wrote on 2 Aug 2016, 14:47 last edited by UnitScan 8 Feb 2016, 15:58
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
-
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
wrote on 3 Aug 2016, 12:22 last edited byYeah, 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); } } });
-
wrote on 8 Aug 2016, 13:01 last edited by
[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
9/9