melchor629 / node-flac-bindings

Nodejs bindings to libFLAC
ISC License
17 stars 1 forks source link

Any documentation on how to write metadata to a flac file? #17

Closed shahriar-shojib closed 4 years ago

shahriar-shojib commented 4 years ago

Hello, is there a documentation on how to write track metadata into a flac file using this module or is it out of scope for this project?

melchor629 commented 4 years ago

Hi! Currently I have no documentation for the API, because this package is just a nice-cool-looking bindings to the C API. But I will try to explain a bit.

Currently there are three metadata APIs in libFLAC:

These are mapped in this package as (extracted from the readme):

const flac = require('flac-bindings');

// level 0 API
console.log(flac.api.metadata0);

// level 1 API
console.log(flac.api.SimpleIterator);

// level 2 API
console.log(flac.api.Chain);
console.log(flac.api.Iterator);

// metadata objects
console.log(flac.api.metadata);

The level 0 contains the 0 functions in it (names are similar, with an IDE that understands TypeScript types you'll get autocomplete suggestions).

The level 1 and level 2 APIs are classes that wraps the behaviour of each object in it. For example, all functions that start with FLAC__metadata_chain_ are methods inside flac.api.Iterator class.

The metadata objects (VorbisComment, Picture...) can be found inside flac.api.metadata;

So now, if you read the libFLAC documentation, you will be able to (more or less) write metadata to files.

You can checkout the tests for some examples of how to use it in JS: metadata level 1 tests and metadata level 2 tests.

A quick (but not tested) example:

const { api: flac } = require('flac-bindings');

const it = new flac.SimpleIterator();
it.init('path/to/the/file.flac');

// adding a vorbis comment: the ID3-like tags for Ogg and FLAC
// cannot modify the first block which is STREAMINFO, so adding one after it
const vorbisComment = new flac.metadata.VorbisCommentMetadata();
vorbisComment.appendComment('TITLE=Example Song');
vorbisComment.appendComment('ARTIST=Unknown');
it.insertBlockAfter(vorbisComment);

// no need to close anything, it is already written to the file

And that's all I think. Hope this helps you start writing metadata to flac files.

I will let this issue open so if you have more questions, just write them on it. If you think this is all you need, feel free to close the issue.

shahriar-shojib commented 4 years ago

Thanks , I am now able to add metadata How do I write a picture from a buffer or a file into the flac? Note that I am only concerned about writing only since the files I will be working with will not contain any metadata or picture.

shahriar-shojib commented 4 years ago

I am trying to do it like this,

it.init('./file.flac');
const vorbisComment = new flac.metadata.VorbisCommentMetadata();
vorbisComment.appendComment('TITLE=Example Song');
vorbisComment.appendComment('ARTIST=Unknown');
vorbisComment.appendComment('ALBUM=test');
const picture = new flac.metadata.PictureMetadata();
const reader = fs.createReadStream('./picture.jpg');
reader.pipe(picture.data);

reader.on('finish', () => {
    it.insertBlockAfter(vorbisComment);
    it.insertBlockAfter(picture);
});

by doing it like this I get:

_stream_readable.js:680
  dest.on('unpipe', onunpipe);
       ^

TypeError: Cannot read property 'on' of null
shahriar-shojib commented 4 years ago

Nevermind, I was able to get this done with fs.readFile

fs.readFile('./picture.jpg', (err, data) => {
    picture.data = data;
    it.insertBlockAfter(vorbisComment);
    it.insertBlockAfter(picture);
});
melchor629 commented 4 years ago

Cool, I see you managed to modify flac metadata correctly :)

As you can see, the picture metadata expects a buffer and can be read with fs.readFile (or fs.readFileSync). Also I recommend you to fill the picture.mimeType if you don't do it.

Thanks for raising the question, and happy coding :)