Copy tags from one file to another
patrick machielse
patrick at hieper.nl
Sat Dec 6 00:22:40 CET 2008
I'm (thinking of) using TabLib in my OS X program. I need the ability
to copy the tags from input files to newly created mp3's. After some
experimentation, I came up with the following code.
Does this look like a valid approach?
Thanks,
Patrick
=======
// return NO if tags could not be copied
BOOL CopyID3Tag(NSString *sourcePath, NSString *destPath)
{
if ( !sourcePath || !destPath ) return NO;
// get source tag
MPEG::File sourceFile([sourcePath fileSystemRepresentation],
false);
if ( !sourceFile.isValid() ) {
NSLog(@"%s: source file not valid (%@)", __PRETTY_FUNCTION__,
sourcePath);
return NO;
}
// get destination file reference
MPEG::File destFile([destPath fileSystemRepresentation], false);
if ( !destFile.isValid() ) {
NSLog(@"%s: destination file not valid (%@)",
__PRETTY_FUNCTION__, destPath);
return NO;
}
// tag copy flag
BOOL copied = NO;
// Tag::duplicate() only copys some simple properties, so we
must check
// for presence of both v1 and v2 tags and copy both if needed
ID3v1::Tag *sourceV1Tag = sourceFile.ID3v1Tag();
if ( sourceV1Tag ) {
ID3v1::Tag *destV1Tag = destFile.ID3v1Tag(true);
destV1Tag->setTitle(sourceV1Tag->title());
destV1Tag->setArtist(sourceV1Tag->artist());
destV1Tag->setAlbum(sourceV1Tag->album());
destV1Tag->setComment(sourceV1Tag->comment());
destV1Tag->setGenre(sourceV1Tag->genre());
destV1Tag->setYear(sourceV1Tag->year());
destV1Tag->setTrack(sourceV1Tag->track());
copied = YES;
}
ID3v2::Tag *sourceV2Tag = sourceFile.ID3v2Tag();
if ( sourceV2Tag ) {
printV2Tag(sourceV2Tag);
ID3v2::Tag *destV2Tag = destFile.ID3v2Tag(true);
while ( sourceV2Tag->frameList().size() ) {
ID3v2::Frame *f = *(sourceV2Tag->frameList().begin());
destV2Tag->addFrame(f);
sourceV2Tag->removeFrame(f, false /* memory owned by
destV2Tag */);
}
copied = YES;
}
// update destination file
return copied ? (true == destFile.save()) : YES;
}
=====
More information about the taglib-devel
mailing list