如何获取音频和视频文件在Qt的持续时间不使用QMediaPlayer [英] How to get Duration of audio and video files in Qt without using QMediaPlayer

查看:4396
本文介绍了如何获取音频和视频文件在Qt的持续时间不使用QMediaPlayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用一个应用程序,其中我可以使用 QDirIterator 遍历系统驱动器,并查找音频/视频文件,使用 QStandardItemModel 获取详细信息,并显示它QTreeview。我已成功显示文件名,类型,大小,更改日期,但 DURATION 是我无法做到的。

I have been working on an application where I can traverse the system drives using QDirIterator and look for audio/video files, grab the details using QStandardItemModel and display it on QTreeview. I have been successful in displaying file name, type, size, date-modified but DURATION is something which I am not able to do.

下面是代码:

// Displays Files in Detail View on Clicking Drive
void DetailView::on_DriveView_clicked(const QModelIndex &index)
{
int m_count_row = 0;

QStandardItemModel *model = new QStandardItemModel(0,0);
QString sPath = pSystemModel->fileInfo(index).absoluteFilePath();
pSystemTreeViewModel->setRootPath(sPath);
ui->DriveListView->setRootIndex(pSystemTreeViewModel->index(sPath));

pSystemModel->setRootPath(QDir::currentPath());
pSystemModel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs );
pSystemTreeViewModel->setFilter( QDir::Files | QDir::NoDotAndDotDot );

QStringList m_list;
QDirIterator dirIt(sPath,QDirIterator::Subdirectories);

while (dirIt.hasNext())
{
    dirIt.next();
    if (QFileInfo(dirIt.filePath()).isFile())
    {
        if (QFileInfo(dirIt.filePath()).suffix() == "mp3" ||(QFileInfo(dirIt.filePath()).suffix() == "mts" ) ||(QFileInfo(dirIt.filePath()).suffix() == "m2ts" ))
        {
            m_list << dirIt.filePath();

            QModelIndex m_index = model->index(m_count_row, 0, QModelIndex());
            model->setHeaderData( 0, Qt::Horizontal, "Name" );
            model->setHeaderData( 1, Qt::Horizontal, "Type" );
            model->setHeaderData( 2, Qt::Horizontal, "Size" );
            model->setHeaderData( 3, Qt::Horizontal, "Date Modified" );

            model->setData( m_index, dirIt.fileName(), Qt::DecorationRole );
            QStandardItem *itemName = new QStandardItem(dirIt.fileName());
            model->setItem(m_count_row, 0, itemName);

            model->setData( m_index, dirIt.fileInfo().suffix(), Qt::DecorationRole );
            QStandardItem *itemExtention = new QStandardItem( dirIt.fileInfo().suffix());
            model->setItem(m_count_row, 1, itemExtention);

            model->setData( m_index, dirIt.fileInfo().size(), Qt::DecorationRole );
            float fFileSize = dirIt.fileInfo().size();
            float fFileKB = fFileSize / 1024; //kilobyte
            float fFileMB = fFileKB / 1024; //megabyte
            float fFinalSize = ceilf(fFileMB * 100) / 100;

            QString sSizeValue = QString::number(fFinalSize);
            QStandardItem *itemSize = new QStandardItem(sSizeValue + " MB");
            model->setItem(m_count_row, 2, itemSize);

            model->setData( m_index, dirIt.fileInfo().lastModified(), Qt::DecorationRole );
            QDateTime m_time = dirIt.fileInfo().lastModified();
            QString sTime = m_time.toString("dd/MM/yyyy hh:mm:ss");
            QStandardItem *itemDate = new QStandardItem(sTime);
            model->setItem(m_count_row, 3, itemDate);

            ui->DriveListView->setModel(model);
            ui->DriveListView->setRootIsDecorated(false);
            m_count_row++;
        }
    }

    pSystemTreeViewModel->setNameFilterDisables(false);
}
}

我不知道是否可以上面的方式,因为我找不到任何这样的属性。是他们的任何其他方式吗?我不想使用QMediaPlayer。任何其他替代解决方案,可以帮助我更新我的上述代码与持续时间?

I am not sure whether I can get the duration using the above way since I couldn't find any such property. Is their any other way of doing it? I don want to use QMediaPlayer. Any other alternate solution which can help me update my above code with Duration???

推荐答案

使用Qt

可以尝试探索Qt的 Phonon模块

我认为此功能 Phonon的MediaObject类可能有助于获得您想要实现的目标。

I think this function in the Phonon's MediaObject class might be helpful in getting what you are trying to achieve.

请阅读该函数末尾写的警告,以避免得到错误的结果。

P.S. Do read the warning written at the end of that function, to avoid getting wrong results.

编辑1:
不使用Qt

阅读有关TagLib库的信息此处。检查他们提及的有关获取文件长度的功能。它也是在LGPL以下。

Read about TagLib Library here. Check this function they have mentioned regarding getting the length of file. And it is under LGPL as well.

这篇关于如何获取音频和视频文件在Qt的持续时间不使用QMediaPlayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆