Skip to content

Instantly share code, notes, and snippets.

@crutchcorn
Created March 14, 2020 07:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crutchcorn/3c33a9ced8c0cb6d31fa14bde14e0fbf to your computer and use it in GitHub Desktop.
Save crutchcorn/3c33a9ced8c0cb6d31fa14bde14e0fbf to your computer and use it in GitHub Desktop.
This is how I remove duplicate tracks in my music library
const mm = require('music-metadata');
const fs = require('fs');
const files = fs.readFileSync('../New\ playlist.m3u').toString().split("\r\n");
let songs;
songsP = Promise.all(files.map(file =>mm.parseFile(file).then(v => ({...v, fileName: file})).catch(a => null)))
songsP.then(val => songs = val)
// make sure there are really duplicates in the list
songs = songs.filter(song => !!song).filter(song => song && songs.find(sonn => (song.common.title == sonn.common.title) && (song.fileName != sonn.fileName)))
lowQSongs = {} // title: {bitrate: number, fileName: string}
for (const song of songs) {
const title = song.common.title;
const bitrate = song.format.bitrate;
const fileName = song.fileName;
if (lowQSongs[title] && lowQSongs[title].bitrate < bitrate) {
continue;
}
lowQSongs[title] = {bitrate, fileName};
}
Object.values(lowQSongs).map(a => a.fileName).forEach(f => fs.unlink(f, e => console.warn(e)))
@crutchcorn
Copy link
Author

This sure is some 12:30AM code quality if I've ever seen it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment