c++ - 在 C++ 中使用 TagLib 删除除 APIC 之外的所有标签

标签 c++ mp3 taglib id3v2

我写了一个函数,应该从 MPEG 文件中删除所有标签,APIC 标签除外,但我得到了混合且不可预测的结果。有时,除了“年”之外的所有标签都被正确删除(这种情况经常发生),有时,一个或多个其他标签会附加到“年”标签。

我肯定做错了什么。这是我的功能:

void stripTags(const char* path) {
    MPEG::File m(path);
    m.strip(MPEG::File::ID3v1 | MPEG::File::APE, true); //I added this because otherwise, all tags would stay the first time I executed stripTags(). The second time I would execute it then the tags would be mostly gone (except for "year" as mentioned)
    ByteVector handle = "APIC";
    ID3v2::Tag *t = m.ID3v2Tag();
    if (t) {
        for (ID3v2::FrameList::ConstIterator it = t->frameList().begin(); it != t->frameList().end(); it++) {
            if ((*it)->frameID() != handle) {
                t->removeFrames((*it)->frameID());
                it = t->frameList().begin(); //since the doc says that removeFrames invalidates the pointer returned by frameList, I update this pointer after each removal
            }
        }
        m.save();
    }
    m.strip(MPEG::File::ID3v1 | MPEG::File::APE, true);
}

感谢帮助

最佳答案

在循环中,当您删除一个标签时,您将重置迭代器。但是随后循环继续,它做的第一件事是 it++,这意味着您的循环将跳过一个条目。

你可以像这样修改你的循环

for (ID3v2::FrameList::ConstIterator it = t->frameList().begin(); it != t->frameList().end(); /* nothing here */) {
    if ((*it)->frameID() != handle) {
        t->removeFrames((*it)->frameID());
        it = t->frameList().begin();
    } else {
        ++it;
    }
}

关于c++ - 在 C++ 中使用 TagLib 删除除 APIC 之外的所有标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32690616/

相关文章:

c++ - 作为引用传递时 istream 的无效初始化错误

c++ - 使用派生类的程序的错误输出。不为心之光

c++ - #ifdef 用于 32 位平台

Grails:如何在标签库中使用请求范围服务?

file - 来自 ByteArray/Stream 的 TagLib-sharp 文件

c++ - c++ using 指令的范围

html - 在mp3旁使用firefox时,请使用其他音效来源

python - 如何使用python在pygame中定义快进按钮?

javascript - Firefox MediaSource 扩展上的 mp3 支持

java - 如何将 BigDecimal 传递到自定义标记库 (WebSphere)