If you would like to discuss anything, please contact me via email: Howard.Bayliss@sequence.co.uk

Thursday, June 21, 2012

TagLib Stream Buffer

Do you want to load TagLib mp3 properties from a buffer or stream?

As I write this, the latest version in the TagLib Master branch on GitHub supports this.

Here's some C++ code that I've hacked together to do it - thanks to Lukáš Lalinský for his help.

Note that I'm not a C++ Developer ;)

I need to use C++ in this instance as I'm working on a project that cannot use the .Net framework - more in a later post.


// Load a sample track into a stream. std::ifstream is("Track1.mp3");

if (!is.bad())
{
// Calculate the size of the stream.
long l = is.tellg();
is.seekg (0, ios::end);
long m = is.tellg();
is.seekg (0, ios::beg);

long diff = (m - l);

// Save the stream to a buffer.
char* buffer = new char[diff];
is.read(buffer, diff);

ByteVector v(buffer, diff);
TagLib::IOStream* stream = new TagLib::ByteVectorStream(v);

TagLib::ID3v2::FrameFactory *frameFactory = TagLib::ID3v2::FrameFactory::instance();
TagLib::MPEG::File* mpegFile =new TagLib::MPEG::File (stream,frameFactory,true,TagLib::AudioProperties::Accurate);

TagLib::FileRef* f = new TagLib::FileRef(mpegFile);

cout << f->tag()->title() << endl;
cout << mpegFile->ID3v1Tag()->artist() << endl;

is.close();
}

No comments:

Post a Comment