您的位置:首页 > 编程语言 > Qt开发

QT 利用slite3和mediainfodll类获取多媒体标签

2015-07-04 22:11 429 查看
源文件目录下
//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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ QT 音频文件标签