【问题标题】:TagLib: could not open fileTagLib:无法打开文件
【发布时间】:2012-05-14 07:55:59
【问题描述】:

我正在尝试编写一个简单的程序来了解 TagLib (http://developer.kde.org/~wheeler/taglib.html) 的工作原理。该程序可以很好地与代码中的 cmets 配合使用。但是当我更改代码时,我得到了这些我无法解决的错误。 这是代码

#include<iostream>
#include<QString>
//#include "Helper.h"
#include "../../player/Util.h"
#include <taglib/tag.h>
#include <taglib/taglib.h>
#include <taglib/fileref.h>
#include <QString>


#include <boost/filesystem.hpp>

using std::string;
struct MetaData{

    string filepath, artist, album, title;
    signed int year, track_num, length_ms, bitrate;
};

//MetaData getMetaDataOfFile(QString file)
MetaData getMetaDataOfFile(string file)
{
    MetaData md;
    //TagLib::FileRef f(TagLib::FileName(file.toUtf8())); 
    const char * filename = file.c_str();
    std::cout<< filename;
    TagLib::FileRef f(TagLib::FileName(filename)); 

    //md.filepath = file.toStdString();
    md.filepath = file;

    //boost::filesystem::path filePath(file.toStdString());
    boost::filesystem::path filePath(file);
    md.title = filePath.stem().string();

    if(f.isNull()) return md;
    if(!f.tag()) return md;
    if(f.tag()->isEmpty()) return md;

    string artist = f.tag()->artist().to8Bit(true);
    string album = f.tag()->album().to8Bit(true);
    string title = f.tag()->title().to8Bit(true);
    uint year = f.tag()->year();
    uint track = f.tag()->track();
    int bitrate = f.audioProperties()->bitrate() * 1000;
    int length = f.audioProperties()->length();

    md.album = cnvrtString2FirstUpper(album);
    md.artist = cnvrtString2FirstUpper(artist);
    md.title = cnvrtString2FirstUpper(title);
    //md.filepath = file.toStdString();
    md.filepath = file;
    md.length_ms = length * 1000;
    md.year = year;
    md.track_num = track;
    md.bitrate = bitrate;

    if(md.title.length()==0)
    {

    //boost::filesystem::path filePath(file.toStdString());
    boost::filesystem::path filePath(file);
    md.title = filePath.stem().string();
    }

    return md;
}






int main()
{
    using namespace std;



    //QString file("/home/vickey/Downloads/song1.mp3");
    string file("/home/vickey/Downloads/song1.mp3");
    MetaData md = getMetaDataOfFile(file);

    return 0;

}

编译时出现此错误

 g++ getmd.cpp -o getmd -I /usr/include/qt4/ -I /usr/include/qt4/QtCore/ -ltag -lboost_system -lboost_filesystem -lQtCore -g
getmd.cpp: In function ‘MetaData getMetaDataOfFile(std::string)’:
getmd.cpp:36:10: error: request for member ‘isNull’ in ‘f’, which is of non-class type ‘TagLib::FileRef(TagLib::FileName) {aka TagLib::FileRef(const char*)}’
getmd.cpp:37:11: error: request for member ‘tag’ in ‘f’, which is of non-class type ‘TagLib::FileRef(TagLib::FileName) {aka TagLib::FileRef(const char*)}’
getmd.cpp:38:10: error: request for member ‘tag’ in ‘f’, which is of non-class type ‘TagLib::FileRef(TagLib::FileName) {aka TagLib::FileRef(const char*)}’
getmd.cpp:40:23: error: request for member ‘tag’ in ‘f’, which is of non-class type ‘TagLib::FileRef(TagLib::FileName) {aka TagLib::FileRef(const char*)}’
getmd.cpp:41:22: error: request for member ‘tag’ in ‘f’, which is of non-class type ‘TagLib::FileRef(TagLib::FileName) {aka TagLib::FileRef(const char*)}’
getmd.cpp:42:22: error: request for member ‘tag’ in ‘f’, which is of non-class type ‘TagLib::FileRef(TagLib::FileName) {aka TagLib::FileRef(const char*)}’
getmd.cpp:43:19: error: request for member ‘tag’ in ‘f’, which is of non-class type ‘TagLib::FileRef(TagLib::FileName) {aka TagLib::FileRef(const char*)}’
g    etmd.cpp:44:20: error: request for member ‘tag’ in ‘f’, which is of non-class type ‘TagLib::FileRef(TagLib::FileName) {aka TagLib::FileRef(const char*)}’
getmd.cpp:45:21: error: request for member ‘audioProperties’ in ‘f’, which is of non-class type ‘TagLib::FileRef(TagLib::FileName) {aka TagLib::FileRef(const char*)}’
getmd.cpp:46:20: error: request for member ‘audioProperties’ in ‘f’, which is of non-class type ‘TagLib::FileRef(TagLib::FileName) {aka TagLib::FileRef(const char*)}’

【问题讨论】:

  • 当你把代码改成什么??
  • 代码中的注释为原代码。注释后面是修改后的代码。
  • 尝试在构造函数参数周围多加一对括号:TagLib::FileRef f((TagLib::FileName(file.toUtf8()))); 这可以帮助解决 C++ 语法的特殊歧义。
  • 奇怪,但现在它正在使用“TagLib::FileRef f(TagLib::FileName(file.c_str()));” .我真的不知道出了什么问题,但现在它工作正常:)
  • 使用c_str() 进行文件名转换会因某些编码而失败(它会编译,但不会打开文件)。请参阅下面的答案,了解正确的做事方式。

标签: c++ audio boost metadata taglib


【解决方案1】:

两个版本(已注释和未注释)都是错误的。 Unix 上的 TagLib 需要 const char * 文件名。从QString 获得一个的最佳方式是使用QFile::encodeName()。所以你应该有这样的东西:

TagLib::FileRef f(QFile::encodeName(file).constData());

【讨论】:

  • TagLib::FileRef f(TagLib::FileName(file.toUtf8()));在文件是 QString 的地方炒锅很好。该程序目前仅支持基本字符串文件名,将来对于 UTF 文件名 c_str() 不是一个好的解决方案。有需要时我会考虑你的解决方案
  • 再次抱歉,但这是不正确的。我发布的方法是将QString 转换为const char * 的正确方法。 file.toUtf8()编译,但对于不使用 UTF-8 编码的文件系统上的非 ASCII 文件名,它将失败。例如,如果您在 ISO-8859-1 编码的文件系统上有一个来自 Björk 的文件,您的代码将无法打开它。使用QFile::encodeName() 考虑了底层文件系统的编码。
  • 呃,不能再编辑评论了——我应该知道这是将QString 转换为const char * 以用于文件名 的正确方法;其他方法在其他情况下完全没问题。
猜你喜欢
  • 1970-01-01
  • 2019-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-09
  • 2018-11-30
  • 2015-10-16
相关资源
最近更新 更多