0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

node-id3でid3タグを編集する

Last updated at Posted at 2019-07-27

node.jsでmp3ファイルのid3タグ(曲名や収録アルバム名など)を編集する。

使用するAPI

楽天ブックスCD検索API
https://webservice.rakuten.co.jp/api/bookscdsearch/

node-id3のインストール

npm install node-id3

コード

id3.js

var req = require("request");
var artistname = "アーティストの名前";
//空白は-(p.s red i => p.s.-red-i)
var cdtitle = "アルバム名";
var url = "https://app.rakuten.co.jp/services/api/BooksCD/Search/20170404?applicationId=1036033389143023148&title=" + encodeURI(cdtitle) + "&artistName=" +  encodeURI(artistname);
var fs = require("fs");
var id3 = require("node-id3");


function writetag(array) {

/*
arrayの中身
[ 
  [ 'P.S. RED I (初回限定盤 CD+DVD)',
    'TK from 凛として時雨',
    [ 'P.S. RED I',
      'moving on',
      'P.S. RED I (Instrumental)',
      'Fantastic Magic',
      'Signal',
      'film A moment',
      'katharsis' ] ],
  [ 'P.S. RED I',
    'TK from 凛として時雨',
    [ 'P.S. RED I', 'moving on', 'P.S. RED I 
    (Instrumental)' ] 
  ] 
]
*/
  
  var path = "mp3があるフォルダのパス";
  var dir = fs.readdirSync(path);
  var tags;

  for (var i = 0; i <= array.length - 1; i++) {
    if (array[i][2].length == dir.length) {
      for (var j = 0; j <= dir.length - 1; j++) {
        tags = {
          title: array[i][2][j],
          artist: array[i][1],
          album: array[i][0],
          //APIC: "./hogehoge.jpg", CDジャケット?
        }
        id3.write(tags, path + "/" + dir[j]);
      }
    }
  }
}


req.get(url, function(res, body, error) {

    var array;
    var info = [];
    var item_list = ["title", "artistName", "playList"];
    var content = JSON.parse(body.body).Items;

    for (var i = 0; i <= content.length - 1; i++) {
      array = [];
      for (var j = 0; j <= item_list.length - 1; j++) {
          if (j == item_list.length - 1) {
              content[i] = content[i].Item[item_list[j]].split("#");
              //falseと判定された要素を削除(Boolean => 空白文字やnull等がfalseになる型)
              content[i] = content[i].filter(Boolean);
              array.push(content[i]);
          } else {
              array.push(content[i].Item[item_list[j]]);
          }
       }
      info.push(array);
    }

    writetag(info);

});


0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?