我编写了一个函数,该函数应该从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/

10-12 17:32
查看更多