Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

weband-id3-to-json.js 2.6 KiB

4 anos atrás
4 anos atrás
4 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. const jsmediatags = require('jsmediatags');
  2. const fs = require('fs');
  3. const INPUT = './public/audio/';
  4. const OUTPUT = 'src/data.json';
  5. const data = {"albums" : []};
  6. const promises = [];
  7. try {
  8. if (fs.existsSync(INPUT)) {
  9. //file exists
  10. }
  11. else {
  12. console.log("\n============== Weband Error ====================\n\n");
  13. console.log("Audio files are missing. Please run first : \n\n$ ./scripts/weband-init.sh <input_directory> \n");
  14. console.log("\n================================================\n\n");
  15. process.exit(1);
  16. }
  17. } catch(err) {
  18. console.log(err);
  19. process.exit(1);
  20. }
  21. function is_dir(path) {
  22. try {
  23. var stat = fs.lstatSync(path);
  24. return stat.isDirectory();
  25. } catch (e) {
  26. // lstatSync throws an error if path doesn't exist
  27. return false;
  28. }
  29. }
  30. function id3ToJSON(path){
  31. fs.readdirSync(path).forEach(file => {
  32. let isDir = is_dir(path + file);
  33. if(isDir)
  34. id3ToJSON(path + file + '/');
  35. else {
  36. let filePath = path + file;
  37. if(!filePath.endsWith(".mp3") && !filePath.endsWith(".wav"))
  38. return;
  39. promises.push(new Promise((resolve, reject) => {
  40. new jsmediatags.Reader(filePath)
  41. .read({
  42. onSuccess: (tag) => {
  43. var tags = tag.tags;
  44. if(!data.artist)
  45. data.artist = tags.artist;
  46. let album = getAlbum(tags.album);
  47. if(!album){
  48. album = {"title": tags.album, "tracks": [], "year": tags.year}
  49. if (fs.existsSync(path + 'cover.png'))
  50. album.cover = (path + 'cover.png').replace(INPUT,'audio/');
  51. data.albums.push(album);
  52. }
  53. let track = {"title": tags.title, "file" : filePath.replace(INPUT,'audio/')};
  54. album.tracks.push(track);
  55. resolve(tag);
  56. },
  57. onError: (error) => {
  58. console.log('Error');
  59. console.log(error);
  60. reject(error);
  61. }
  62. });
  63. }));
  64. }
  65. });
  66. }
  67. function getAlbum(albumTitle){
  68. for(let i = 0 ; i < data.albums.length; i++){
  69. if(data.albums[i].title === albumTitle)
  70. return data.albums[i];
  71. }
  72. return null;
  73. }
  74. id3ToJSON(INPUT);
  75. Promise.all(promises).then((values) => {
  76. var jsonContent = JSON.stringify(data, null, 2);
  77. console.log(jsonContent);
  78. fs.writeFile(OUTPUT, jsonContent, 'utf8', function (err) {
  79. if (err) {
  80. console.log("An error occured while writing JSON Object to File.");
  81. return console.log(err);
  82. }
  83. console.log("JSON file has been saved : " + OUTPUT);
  84. });
  85. });