qt creator使用taglib读取音频元信息,windows平台vcpkg安装

时间:2024-12-11 12:49:51

注意:qt creator用的构建组件是qt 6.2.3 MSVC2019 64bit

  1. 安装vcpkg
// 我的安装位置C:\vcpkg
git clone https://github.com/microsoft/vcpkg.git C:\vcpkg
cd C:\vcpkg
.\bootstrap-vcpkg.bat

// 设置系统环境变量
VCPKG_ROOT = C:/vcpkg
  1. 用vcpkg安装taglib
vcpkg install taglib
  1. Qt项目中使用taglib, .pro文件中加入
// 注意vcpkg的安装路径
INCLUDEPATH += C:/vcpkg/installed/x64-windows/include/taglib
LIBS += -LC:/vcpkg/installed/x64-windows/lib -ltag -ltag_c
  1. Qt项目文件中使用
#include <tag.h>
#include <fileref.h>
...
// 加载音频文件
    QString filePath = "Music/Tell Me-潘玮柏.mp3";
    TagLib::FileRef f(filePath.toLocal8Bit().data());

    // 检查文件是否加载成功
    if (!f.isNull() && f.tag()) {
        TagLib::Tag *tag = f.tag();
        TagLib::AudioProperties *properties = f.audioProperties();
        
        // 输出音频文件的标签信息
        QString title = QString::fromWCharArray(tag->title().toCWString());
        QString artist = QString::fromWCharArray(tag->artist().toCWString());
        QString album = QString::fromWCharArray(tag->album().toCWString());
        QString year = QString::number(tag->year());
        
        int duration = properties->length();
        qDebug() << "Title:" << title;
        qDebug() << "Artist:" << artist;
        qDebug() << "Album:" << album;
        qDebug() << "Duration:" << duration << "seconds";
    } else {
     	qDebug() << "读取文件失败";
    }
...