在 C++ 中使用 TagLib 删除除 APIC 之外的所有标签
Remove all tags except APIC with TagLib in C++
我写了一个函数,应该从 MPEG 文件中删除所有标签,APIC 标签除外,但我得到了混合且不可预测的结果。有时,除了 "year" 之外的所有标签都被正确删除(大多数情况下会发生这种情况),有时,一个或多个其他标签会附加到 "year" 标签。
我肯定做错了什么。这是我的功能:
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;
}
}
我写了一个函数,应该从 MPEG 文件中删除所有标签,APIC 标签除外,但我得到了混合且不可预测的结果。有时,除了 "year" 之外的所有标签都被正确删除(大多数情况下会发生这种情况),有时,一个或多个其他标签会附加到 "year" 标签。
我肯定做错了什么。这是我的功能:
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;
}
}