Merge pull request #701 from TsudaKageyu/flac-strip

Enable FLAC::File to remove non-standard tags.
This commit is contained in:
Tsuda Kageyu 2015-12-24 09:41:48 +09:00
commit 626d13467e
3 changed files with 98 additions and 0 deletions

View File

@ -372,6 +372,20 @@ void FLAC::File::removePictures()
}
}
void FLAC::File::strip(int tags)
{
if(tags & ID3v1)
d->tag.set(FlacID3v1Index, 0);
if(tags & ID3v2)
d->tag.set(FlacID3v2Index, 0);
if(tags & XiphComment) {
xiphComment()->removeAllFields();
xiphComment()->removeAllPictures();
}
}
bool FLAC::File::hasXiphComment() const
{
return !d->xiphCommentData.isEmpty();

View File

@ -66,6 +66,23 @@ namespace TagLib {
class TAGLIB_EXPORT File : public TagLib::File
{
public:
/*!
* This set of flags is used for various operations and is suitable for
* being OR-ed together.
*/
enum TagTypes {
//! Empty set. Matches no tag types.
NoTags = 0x0000,
//! Matches Vorbis comments.
XiphComment = 0x0001,
//! Matches ID3v1 tags.
ID3v1 = 0x0002,
//! Matches ID3v2 tags.
ID3v2 = 0x0004,
//! Matches all tag types.
AllTags = 0xffff
};
/*!
* Constructs a FLAC file from \a file. If \a readProperties is true the
* file's audio properties will also be read.
@ -265,6 +282,21 @@ namespace TagLib {
*/
void addPicture(Picture *picture);
/*!
* This will remove the tags that match the OR-ed together TagTypes from
* the file. By default it removes all tags.
*
* \warning This will also invalidate pointers to the tags as their memory
* will be freed.
*
* \note In order to make the removal permanent save() still needs to be
* called.
*
* \note This won't remove the Vorbis comment block completely. The
* vendor ID will be preserved.
*/
void strip(int tags = AllTags);
/*!
* Returns whether or not the file on disk actually has a XiphComment.
*

View File

@ -36,6 +36,7 @@ class TestFLAC : public CppUnit::TestFixture
CPPUNIT_TEST(testSaveID3v1);
CPPUNIT_TEST(testUpdateID3v2);
CPPUNIT_TEST(testEmptyID3v2);
CPPUNIT_TEST(testStripTags);
CPPUNIT_TEST_SUITE_END();
public:
@ -416,6 +417,57 @@ public:
}
}
void testStripTags()
{
ScopedFileCopy copy("silence-44-s", ".flac");
{
FLAC::File f(copy.fileName().c_str());
f.xiphComment(true)->setTitle("XiphComment Title");
f.ID3v1Tag(true)->setTitle("ID3v1 Title");
f.ID3v2Tag(true)->setTitle("ID3v2 Title");
f.save();
}
{
FLAC::File f(copy.fileName().c_str());
CPPUNIT_ASSERT(f.hasXiphComment());
CPPUNIT_ASSERT(f.hasID3v1Tag());
CPPUNIT_ASSERT(f.hasID3v2Tag());
CPPUNIT_ASSERT_EQUAL(String("XiphComment Title"), f.xiphComment()->title());
CPPUNIT_ASSERT_EQUAL(String("ID3v1 Title"), f.ID3v1Tag()->title());
CPPUNIT_ASSERT_EQUAL(String("ID3v2 Title"), f.ID3v2Tag()->title());
f.strip(FLAC::File::ID3v2);
f.save();
}
{
FLAC::File f(copy.fileName().c_str());
CPPUNIT_ASSERT(f.hasXiphComment());
CPPUNIT_ASSERT(f.hasID3v1Tag());
CPPUNIT_ASSERT(!f.hasID3v2Tag());
CPPUNIT_ASSERT_EQUAL(String("XiphComment Title"), f.xiphComment()->title());
CPPUNIT_ASSERT_EQUAL(String("ID3v1 Title"), f.ID3v1Tag()->title());
f.strip(FLAC::File::ID3v1);
f.save();
}
{
FLAC::File f(copy.fileName().c_str());
CPPUNIT_ASSERT(f.hasXiphComment());
CPPUNIT_ASSERT(!f.hasID3v1Tag());
CPPUNIT_ASSERT(!f.hasID3v2Tag());
CPPUNIT_ASSERT_EQUAL(String("XiphComment Title"), f.xiphComment()->title());
f.strip(FLAC::File::XiphComment);
f.save();
}
{
FLAC::File f(copy.fileName().c_str());
CPPUNIT_ASSERT(f.hasXiphComment());
CPPUNIT_ASSERT(!f.hasID3v1Tag());
CPPUNIT_ASSERT(!f.hasID3v2Tag());
CPPUNIT_ASSERT(f.xiphComment()->isEmpty());
CPPUNIT_ASSERT_EQUAL(String("reference libFLAC 1.1.0 20030126"), f.xiphComment()->vendorID());
}
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(TestFLAC);