在 node.js 中转换 iTunes XML 播放列表时遇到问题 [英] Having trouble converting itunes XML playlist in node.js

查看:44
本文介绍了在 node.js 中转换 iTunes XML 播放列表时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前段时间我编写了一个快速的小节点命令行实用程序,将 XML 格式的 iTunes 播放列表转换为 m3u、xspf 等.这样我就可以在我工作的 linux 机器、android 手机* 等上使用它们.

Some time ago I wrote a quick little node command-line utility to convert itunes playlists in XML format to m3u, xspf, etc. so I could use them on my linux box at work, android phone*, etc.

* 我有超过 25 GB 的音乐收藏,doubletwist 等人在试图与我的 mac 同步时死翘翘了

* I have a 25+ gigabyte music collection, doubletwist et al just keel over and die trying to sync with my mac

起初这很好,但随着我的音乐收藏的增长,我遇到了一个障碍:似乎没有媒体播放器可以找到任何包含非英语 unicode 字符(例如 ñ、í 和几乎所有日语汉字)的文件.不是每个字符都会导致这个问题,但在大多数情况下是一个原因.

At first this was fine, but as my music collection has grown I've hit a snag: no media player can seem to find any files with non-English unicode characters such as ñ, í, and pretty much any Japanese kanji. Its not every single character that causes this problem, but for the most part its a thing.

由于itunes文件路径是部分url编码的(并且不需要匹配目标格式的约束),并且需要部分替换为目标机器上的正确路径,我有以下代码处理文件路径(去掉无关的东西):

Since the itunes file paths are partially url-encoded (and need to not be to match the constraints of the target formats), and need to be partially replaced to the correct path on the target machine, I have the following code to deal with the file paths (stripped of irrelevant stuff):

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 文件中的示例:

I've tried decodeURIComponent, decodeURI, and my own custom function referenced above. Here is an example from the XML file:

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 的文件名:

Which seems fine, but VLC, clementine, etc. can't find. Here's the file name straight from Nautilus:

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

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

Which is in the directory referenced in the path. Note that the inflection as interpreted by decodeURIComponent is on the 'a' rather than the 'n' (my function doesn't make that error, so that's not the problem per se). How can I modify this such that media players can find the tracks?

推荐答案

所以问题最终出在组合字符上.解码,规范化,然后重新-encoding 修复它.

So the problem ended up being the combining characters. Decoding, normalizing, and re-encoding fixes it.

这篇关于在 node.js 中转换 iTunes XML 播放列表时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆