在 node.js 中转换 iTunes XML 播放列表时遇到问题

Having trouble converting itunes XML playlist in node.js

前段时间我写了一个快速的小节点 command-line 实用程序来将 XML 格式的 iTunes 播放列表转换为 m3u、xspf 等,这样我就可以在我的 linux 上使用它们盒子在工作,android phone* 等

* 我有超过 25 GB 的音乐 collection,doubletwist 等人在尝试与我的 mac

同步时崩溃了

起初还好,但随着我的音乐 collection 越来越大,我遇到了一个障碍:似乎没有媒体播放器可以找到任何带有 non-English unicode 字符的文件,例如 ñ、 í,以及几乎任何日语汉字。不是每一个字符都会导致这个问题,但在大多数情况下是一个问题。

由于itunes文件路径有一部分url-encoded(并且不需要匹配目标格式的约束),需要部分替换为目标上的正确路径machine,我有以下代码来处理文件路径(删除了不相关的东西):

let location;

// need try/catch because some track names contain unescaped '%' that
// cause the decode function to throw.
try {
  location = decodeURIComponent(x.location.slice(7));
} catch (e) {

  // function references a hash of about 200 url encodings and
  // replaces occurences of them in the path, poor man's (slow) 
  // replacement for the built-in
  location = replaceURLEscapes(x.location.slice(7));
}

我已经尝试了 decodeURIComponent, decodeURI,以及上面引用的我自己的自定义函数。这是 XML 文件中的示例:

file:///Users/username/Music/iTunes/iTunes%20Media/Music/Compilations/Chronicles%20of%20Time/3-05%20Melodi%CC%81a%20de%20la%20montan%CC%83a%20(feat.%20Doug%20Perry%20&%20Matheus%20S.%20Garcia%20Souza).m4a

转换为:

/home/username/Music/Compilations/Chronicles of Time/3-05 Melodía de la montaña (feat. Doug Perry & Matheus S. Garcia Souza).m4a

这看起来不错,但找不到 VLC、clementine 等。这是直接来自 Nautilus 的文件名:

3-05 Melodía de la montaña (feat. Doug Perry & Matheus S. Garcia Souza).m4a

在路径中引用的目录中。请注意,decodeURIComponent 解释的变形是在 'a' 而不是 'n' 上(我的函数不会出现该错误,所以这本身不是问题)。 我如何修改它以便媒体播放器可以找到曲目?

所以问题最终是组合字符。解码、normalizing 和重新编码修复它。