benrr101 / node-taglib-sharp

A node.js port of mono/taglib-sharp
GNU Lesser General Public License v2.1
42 stars 11 forks source link

How to add pictures? #34

Closed vk22 closed 3 years ago

vk22 commented 3 years ago

Sorry, can get how to add cover to aiff file?

I try this:

var pictureBuffer = fs.readFileSync(orderFolderName+'Front.png')  

var pic = {
    data: pictureBuffer,
    mimeType: 'image/png'
}

const myFile = File.createFromPath(orderFolderName+'/test.aif');

myFile.tag.title = result.tracklist[0].title;
myFile.tag.album = result.title;
myFile.tag.performers = [result.artists_sort];
myFile.tag.pictures = [pic];
myFile.tag.genres = result.styles;
myFile.tag.year = result.year;
myFile.save();
myFile.dispose();

But i get error: Argument null: data was not provided

benrr101 commented 3 years ago

Hi thanks for reaching out!

I tried out your code (fudging in some values for your result object) and noticed a couple things:

Otherwise, it looks pretty good. I patched up your code a bit and ended up with (in typescript):

import * as fs from "fs";
import {ByteVector, File, PictureType} from "node-taglib-sharp";

const myFile = File.createFromPath("path_to_aif");
const pic = {
    data: ByteVector.fromPath("path_to_img"),
    mimeType: 'image/png',
    type: PictureType.FrontCover,
    filename: undefined, // not required
    description: undefined // not required
}

myFile.tag.title = "title";
myFile.tag.album = "album";
myFile.tag.performers = ["foo", "bar", "baz"];
myFile.tag.pictures = [pic];
myFile.tag.genres = ["reggae"];
myFile.tag.year = 2021;
myFile.save();
myFile.dispose();

Alternatively, you can let the library construct an IPicture object by doing Picture.fromPath("path_to_img");

This library mostly follows the paradigms from the original .NET implementation, including the (somewhat confusing) ByteVector class. If you have any suggestions for how I can better document or implement it to work better for node developers, please let me know!

benrr101 commented 3 years ago

Looks like that resolves your issue. Let me know if you have anymore issues, and thanks again for trying out node-taglib-sharp!

vk22 commented 3 years ago

Hi! I'm sorry I didn't answer right away. Was away. Your answer helped me. Thank you very much! But I have a couple more questions, I'll write a little later today! Have a nice day!

vk22 commented 2 years ago

Hey mate! Sorry, but i'm back)) I have some strange with saving file after adding tags.

My code:

const myFile = File.createFromPath(path);                   
const pic = {
        data: ByteVector.fromPath(pathToPic),
        mimeType: 'image/png',
        type: PictureType.FrontCover,
        filename: 'Cover.png', 
        description: 'Cover.png'
    }
    myFile.tag.pictures = [pic];    
    myFile.tag.title = tagsData.trackTitle;
    myFile.tag.album = tagsData.releaseAlbum;
    myFile.tag.performers = [tagsData.artists];
    myFile.tag.albumArtists = [tagsData.albumArtists];
    myFile.tag.genres = tagsData.styleAsString;
    if (tagsData.year) {
      myFile.tag.year = tagsData.year;
    }
    myFile.tag.track = tagsData.indexTrack;
    myFile.tag.trackCount = tagsData.trackCount;
    myFile.save();
    myFile.dispose(); 

After saving i don't see cover and tags in finder or preview. - http://prntscr.com/26bi92t

They are present, I can see they in players (itunes) or tag editors (kid3, meta) - http://prntscr.com/26bi9er And then if I open this file in kid3 and save it - tags and cover will be visible in finder and preview - http://prntscr.com/26bia32

OS: Mac OS Files type: aiff

Can you help me? Regards!