1. 程式人生 > >QT 利用slite3和mediainfodll類獲取多媒體標籤

QT 利用slite3和mediainfodll類獲取多媒體標籤

原始檔目錄下
//PRO檔案
#利用sqlite3資料庫操作,將gbk碼轉換成unicode碼
#KAKASI2014-6-29

QT+=\
widgets\
testlib

HEADERS+=\
QMediaInfo.h\
QUnitTest.h

SOURCES+=\
test.cpp\
QMediaInfo.cpp\
QUnitTest.cpp

LIBS+=D:\QTPractice\mediainfo\sqlite3.lib

//QMEDIAINFO。h
/*
功能:封裝MediaInfodll功能
KAKASI 2014-6-29
*/
#define _UNICODE
#ifndef QMEDIAINFO_H
#define QMEDIAINFO_H
#include "MediaInfoDLL.h"
#include <QString>
class QMediaInfo
{
public:
    QMediaInfo(QString &fileName);
    ~QMediaInfo();
    QString getInfo(QString str,bool willConvert = false) const;
    template<typename T>
    static void d(T t, bool b=false);
private:
    MediaInfoDLL::MediaInfo *MI;
};
#endif // QMEDIAINFO_H
//.CPP檔案
#include "QMediaInfo.h"
#include <QDebug>
#include <QFileInfo>
#include "sqlite3.h"
#include "getGBK.h"
#include <QMessageBox>
bool isExist(QString fileName)
{
    QFileInfo info(fileName);
    return info.exists();
}
template<typename T>
void QMediaInfo::d(T t, bool b)
{
    //return;
    if (b)
        qDebug() << hex << t;
    else
        qDebug() << t;
}
QMediaInfo::QMediaInfo(QString &fileName)
{
    using namespace MediaInfoDLL;
    MI = new MediaInfo;
    if(MI->IsReady())
        d("MI is READY");
    if(isExist("MediaInfo.dll"))
        d("mediainfo.dll is exist");
    if(isExist(fileName))
        d(fileName + " is exist");
    //can not use (LPCWSTR)"shui2.mp3"
    const String name = fileName.toStdWString();
    size_t b = MI->Open(name);
    if(b == 0)
        d(fileName + " is not opened");
    if(b == 1)
    {
        d(fileName + " is opened");
    }
}
//功能,接受一個QString,將其轉換下GBK->UNICODE
QString convert(QString str)
{
    //gbk -> unicode
    //std::string unicode[6] = {"4e2d","6587","4eca","751f","4eca","4e16"};
    //getString(unicode,6,false);//CDC0,BAE9,B8D5
    //gbk 中文從81e0開始
    QString temp = str;
    QString res="";
    QString uniRes="";
    for (int i = 0; i < temp.length(); ++i)
    {
        QChar ch = temp.at(i);  //cd
        QString strs="";
        strs.setNum(ch.unicode(),16);//205
        res.append(strs);
    }
    //QMediaInfo::d(res);
    //每次讀取1個,如果大於81,從讀取2個,查詢替換
    QString perStr=res.mid(0,2);
    int l = res.length();
    bool b;
    std::string s[1];
    std::string r;
    for(int pos = 0; pos < l;)
    {
        //QMediaInfo::d("next 2 "+perStr);
        if (perStr.toInt(&b,16) >= 0x81)
        {
            perStr=res.mid(pos,4);
            //QMediaInfo::d(" 4 "+perStr);
            pos += 4;
            //轉換
            s[0]= perStr.toStdString();
            r = getString("code.db",s,1,false);
            //QMediaInfo::d(QString::fromStdString(r));
            QString temp = "0x"+ QString::fromStdString(r);
            QChar tmp = temp.toInt(&b,16);
            //QMediaInfo::d(tmp);
            uniRes.append(tmp);
            res.replace(perStr,QString::fromStdString(r));
        }
        else
        {
            QChar tmp = perStr.toInt(&b,16);
            uniRes.append(tmp);
            pos += 2;
        }
        //讀取下一個
        if (pos + 2 <= l)
        {
            perStr=res.mid(pos,2);
        }
    }
    //QMediaInfo::d(res);
    //QMediaInfo::d(uniRes);
    //QChar ss = 0x5c60;
    //QMediaInfo::d(ss);
    return uniRes;
}
QString QMediaInfo::getInfo(QString str, bool willConvert) const
{
    using namespace MediaInfoDLL;
    QString temp;
    QString tmp(str);
    String all = MI->Get(Stream_General,0,tmp.toStdWString());
    if (str.toUpper() == "ALL")
        all = MI->Inform();
    temp = QString::fromStdWString(all);
    if (willConvert)temp = convert(temp);
    return temp;
}
QMediaInfo::~QMediaInfo()
{
    MI->Close();
    delete MI;
    MI = 0;
}
//測試檔案
#include <QtWidgets/QApplication>
#include "QMediaInfo.h"
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QString fileName = "bw.mp3";
    QMediaInfo info(fileName);
    //info.getAll();
    QMediaInfo::d(info.getInfo("Performer",true));
    QMediaInfo::d(info.getInfo("Title",true));
    QMediaInfo::d(info.getInfo("BitRate"));
    QMediaInfo::d(info.getInfo("Duration"));
    //QMediaInfo::d(info.getInfo("All"));
    return app.exec();
}
//單元測試檔案.h
#ifndef QUNITTEST_H
#define QUNITTEST_H
#include <QObject>
#include "QMediaInfo.h"
class QUnitTest : public QObject
{
    Q_OBJECT
public:
    QUnitTest();
private Q_SLOTS:
    void initTestCase();
    void cleanupTestCase();
    void testCase1();
    void testCase1_data();
};
#endif // QUNITTEST_H

//單元測試檔案.cpp
#include <QString>
#include <QtTest>
#include <QObject>
#include <QDebug>
#include "QUnitTest.h"
QUnitTest::QUnitTest(){}
void QUnitTest::initTestCase(){}
void QUnitTest::cleanupTestCase(){}
void QUnitTest::testCase1()
{
    //TestClass p;
    //QVERIFY(p.Sun() == 0);
    //above two lines is right without _data function
    //QString& getInfo("Performer",true)
    QString fileName = "bw.mp3";//D:/音樂/霸王別姬.mp3";
    QMediaInfo info(fileName);
    QFETCH(QString, a);
    QFETCH(bool ,   b);
    QFETCH(QString, result);
    QCOMPARE(info.getInfo(a,b), result);
}
void QUnitTest::testCase1_data()
{
    QTest::addColumn<QString>("a");
    QTest::addColumn<bool>("b");
    QTest::addColumn<QString>("result");
    QTest::newRow("0") <<"BitRate"<<false<<"56000";
    QTest::newRow("1") <<"Performer"<<true<<"屠洪剛";
    QTest::newRow("2") <<"Title"<<true<<"霸王別姬";
}
//QTEST_MAIN(QUnitTest)