fixAlbumArt

To discuss development of addons / skins / customization of MediaMonkey.

Moderators: jiri, drakinite, Addon Administrators

Ludek
Posts: 4959
Joined: Fri Mar 09, 2007 9:00 am

Re: fixAlbumArt

Post by Ludek »

OK, it wasn't so easy as the image format might need to be converted during that process (e.g. BMP to PNG), plus not all track types supports images stored in file tag (e.g. WAV, AIFF), etc.

Therefore I needed to introduce new method move2TagAsync, usage like this:

Code: Select all

                                            firstCover.move2TagAsync().then(() => {                                                
                                                next(); // succcess
                                            }, (error) => {       
                                                ODS('Error during moving cover to file tag: ' + error);                                                                                     
                                                next();
                                            });
The method will be included in the next 5.0.3 build and the whole code looks like this (tested that it works):

Code: Select all

                function removeAll(trackList) {

                    var cvr;
                    var cvrList;

                    // Create a background task so that the user can see the progress in the bottom bar
                    var taskProgress = app.backgroundTasks.createNew();
                    taskProgress.leadingText = ('Fixing art: ');
                    var trackCount = trackList.count;
                    var trackIdx = 0;

                    listAsyncForEach(trackList,
                        // callback for each track
                        (track, next) => {

                            // Update the background task text
                            trackIdx++;
                            taskProgress.text = sprintf('%d of %d', trackIdx, trackCount);

                            cvrList = track.loadCoverListAsync();
                            cvrList.whenLoaded().then(() => {
                                cvrList.modifyAsync(function () {
                                    if (cvrList.count > 0) {

                                        //loop through all covers to find if any cover identified as the front
                                        //any cover not identified as a front cover is set as "Not Specified"
                                        var blnFrontFound = false;
                                        for (var intCnt = 0; intCnt < cvrList.count; intCnt++) {
                                            cvr = cvrList.getValue(intCnt);
                                            if (blnFrontFound == true) {
                                                cvr.coverTypeDesc = "Not specified";
                                            } else {
                                                if (cvr.coverTypeDesc == "Cover (front)") {
                                                    blnFrontFound = true;
                                                } else {
                                                    cvr.coverTypeDesc = "Not specified";
                                                }
                                            }
                                        }

                                        //if none found make the first embedded cover the front
                                        if (blnFrontFound == false) {
                                            for (var intCnt = 0; intCnt < cvrList.count; intCnt++) {
                                                if (cvrList.getValue(intCnt).coverStorage == 0) {
                                                    cvr = cvrList.getValue(intCnt);
                                                    cvr.coverTypeDesc = "Cover (front)";
                                                    blnFrontFound = true;
                                                    intCnt = cvrList.count + 1;
                                                }
                                            }
                                        }

                                        //if none found, make first image the front
                                        if (blnFrontFound == false) {
                                            cvr = cvrList.getValue(0);
                                            cvr.coverTypeDesc = "Cover (front)";
                                        }

                                        //select all covers except front cover
                                        for (var intCnt = 0; intCnt < cvrList.count; intCnt++) {
                                            cvr = cvrList.getValue(intCnt);
                                            if (cvr.coverTypeDesc == "Cover (front)") {
                                                cvrList.setSelected(intCnt, false);
                                            } else {
                                                cvrList.setSelected(intCnt, true);
                                            }
                                        };
                                    }
                                    cvrList.deleteSelected();

                                    if (cvrList.count > 0) {
                                        var firstCover = cvrList.getValue(0);

                                        // The if (firstCover &&) part is just a sanity check to avoid a crash 
                                        if (firstCover && firstCover.coverStorage != 0) {
	                                   cvrList.modified = true; // just in case no cover has been deleted and we change the storage below                                                                            
                                            firstCover.move2TagAsync().then(() => {                                                
                                                next(); // succcess
                                            }, (error) => {       
                                                ODS('Error during moving cover to file tag: ' + error);                                                                                     
                                                next(); // error
                                            });
                                            return;
                                        }
                                    }
                                    // Commit track, then AFTER the commit is done, process the next track
                                    next();
                                });
                            });
                        },
                        // callback when done
                        () => {
                            // finish the background task
                            trackList.commitAsync().then(() => {
                                taskProgress.terminate();
                                messageDlg(_('Album Art is fixed!'), 'Information', ['btnOK'], {
                                    defaultButton: 'btnOK'
                                }, undefined);
                            });
                        }
                    );
                }
MPG
Posts: 418
Joined: Tue May 13, 2008 11:22 pm

Re: fixAlbumArt

Post by MPG »

Thanks Ludek! I look forward to trying it out!
TIA
MPG
Triumph - Hold On: Music holds the secret, to know it can make you whole.
Post Reply