1. 程式人生 > >Qt 使用tablib 獲取多媒體tag資訊

Qt 使用tablib 獲取多媒體tag資訊

最近專案需要, 要獲取音樂檔案tag資訊. 有兩個方式, 本人偏向第二種方式.  效率比較高,可控性比較好.

一. 使用QML Audio component 進行解析. 將多媒體檔案都放到Playlist中, 在遍歷每一個檔案取出檔案中的tag.

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();     } }

二.使用獨立的庫tablib 解析.

第二種方式 實現方式

int main(int argc, char **argv) {     qDebug() << "yong-----main---";     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() << "yong-----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;     }

}