Qt 使用tablib 获取多媒体tag信息

Posted yongxo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt 使用tablib 获取多媒体tag信息相关的知识,希望对你有一定的参考价值。

最近项目需要, 要获取音乐文件tag信息. 有两个方式, 本人偏向第二种方式. 效率比较高,可控性比较好.

一.QML方式

使用QML Audio component 进行解析. 将多媒体文件都放到Playlist中, 在遍历每一个文件取出文件中的tag.
```javascript
import QtQuick 2.7
import QtQuick.Controls 2.4
import QtMultimedia 5.8

Item {
property alias playList: parseMultimedia.playlist
property alias musicPlayer: musicPlayer

WorkerScript {
    id: worker
    property bool running: false
    property var type
    source: "dataloader.js"
    onMessage: {
        if (messageObject.action === ‘sortArtist‘) {
            var msg = {‘action‘: ‘sortAlbum‘, ‘model‘: songsListModel,
                ‘dest‘: albumListModel};
            worker.sendMessage(msg);
        }
    }
}

Audio {
    id: musicPlayer
    autoPlay: true
    autoLoad: true
    onSourceChanged: {
    }
    playlist: Playlist {
        id: musicPlayList
    }
}

Audio {
    id: parseMultimedia
    property var fileName
    //autoPlay :true
    autoLoad: true
    muted: true
    loops: Audio.Infinite
    playlist: parsePlaylist
}

Playlist {
    id: parsePlaylist
}

Timer {
    id: insertTimer
    property int indexPath: 0
    interval: 200;
    running: false; repeat: true
    onTriggered: {
        if ( indexPath < musicManager.count()) {
            parseMultimedia.play()
            parseMultimedia.pause()
            var title = parseMultimedia.metaData.title

            var artist = parseMultimedia.metaData.albumArtist
            if (artist === undefined) {
                artist = ‘unknown‘
            }

            var album = parseMultimedia.metaData.albumTitle
            if (album === undefined) {
                album = ‘unknown‘
            }

            var source = Qt.resolvedUrl(parseMultimedia.playlist.itemSource(indexPath))
            var fileName = musicManager.getFileInfoName(indexPath)
            if (title === undefined) {
                title =  fileName
            }
            songsListModel.set(parsePlaylist.currentIndex, {"playlistIndex": parsePlaylist.currentIndex,
                                   "title": parseMultimedia.metaData.title,
                                   "album": album,
                                   "artist": artist,
                                   "source": source,
                                   "fileName": fileName });
            indexPath = indexPath + 1
            parsePlaylist.next()
           // listView.forceLayout();

        } else {
            if (indexPath >= musicManager.count() ) {
                insertTimer.stop()
                insertTimer.indexPath = 0
                appWindow.isReady = true
                allSongListModelReady();
              }
        }
    }

    onRunningChanged: {
        if (!running) {
            var msg = {‘action‘: ‘sortArtist‘, ‘model‘: songsListModel,
                ‘dest‘:artistListModel};
            worker.sendMessage(msg);
        }
    }
}

Timer {
    id: loadModelTimer
    property int indexPath: 0
    running: false; repeat: false
    onTriggered: {
        parsePlaylist.currentIndex = 0
        insertTimer.start()
    }
}

Connections {
    target: musicManager
    onLoadfinish: {
        parsePlaylist.clear()

        loadModelTimer.interval = musicManager.count()
        loadModelTimer.start()
        for (var i = 0; i< musicManager.count(); i++) {
            parsePlaylist.addItem("file://" + musicManager.getFileInfoPath(i))
        }
        loadFolderData();
    }
}

function loadFolderData() {
   for (var i = 0; i < musicManager.folderCount(); i++) {

       var item = musicManager.folderItem(i);
       folderListModel.set(i, {"baseName": item.baseName,
                                       "uid": item.uId ,
                                       "dropTarget": "none",
                                       "isFolder": item.isFolder,
                                       "parentFolder": item.parentFolder,
                                       "folderOpen":item.folderOpen,
                                       "source": item.path,
                                       "fileCount": item.fileCount })
   }
}
Component.onCompleted: {
    musicManager.runParse();
}

}
```

二.C++ tablib 方式

这个没什么好说的, 直接将tablib编译成和一平台一样的类型就可以了.例如: arm. x86.
```cpp
int main(int argc, char **argv)

{

QString sname;

QString singer;

QString album;

QFileInfo fileInfo(QString("/sdcard/Music/Alone.mp3"));

qDebug() << fileInfo.absoluteFilePath();

const char *fileName = "/sdcard/Music/Alone.mp3";
TagLib::MPEG::File *mpegFile = new TagLib::MPEG::File(fileName);
qDebug() << "mpegFile: " << mpegFile;
if(false == mpegFile->isOpen()) {
qDebug() << "-----open failed---";

} else {

sname = QString(mpegFile->tag()->title().toCString(true));

singer = QString(mpegFile->tag()->artist().toCString(true));

album = QString(mpegFile->tag()->album().toCString(true));
qDebug() << sname;

qDebug() << singer;

qDebug() << album;

}
}
```
这里有我自己编译QT 项目.
QT tablib







































以上是关于Qt 使用tablib 获取多媒体tag信息的主要内容,如果未能解决你的问题,请参考以下文章

Python 中 tablib 模块的使用

QT调试时怎么查看某个指针指向的一片内存区域的信息

8.1.1 获取系统上可用的摄像头信息

tablib.Dataset()操作exl类型数据之“类方法”研究

tablib把数据导出为ExcelJSONCSV等格式的Py库(写入数据并导出exl)

Qt 扫描进程列表以及获取进程信息