使用C#从iTunes删除死轨(不存在重复项)

image

I hate iTunes on Windows with the heat of a thousand suns. It is a pox on my existence and it has cost me hours of pain and suffering. I'm sure it's all unicorns and spun sugar on MAC, but as a guy using Windows with dozens of gigabytes of music on a shared file server that I've ripped since the first CD I ever purchased, it's hell on earth. I could manage all my music with Zune (and I do have a Zune Pass for leasing music I don't want to buy) but there are iDevices in the house and iTunes is what it takes.

我讨厌Windows上的iTunes,因为它有一千个阳光。 这是我生存的痘痘,这使我付出了数小时的痛苦和磨难。 我敢肯定,所有这些都是在MAC上独角兽和旋转糖,但是作为一个使用Windows并在共享文件服务器上使用数十GB音乐的人(自从我购买了第一张CD以来,我就一直在翻录它),这真是地狱。 我可以使用Zune来管理所有音乐(并且我有Zune Pass可以出租我不想购买的音乐),但是家里有iDevices,而iTunes就是需要的。

Did I mention it sucks? It's slow, frustrating, and is effectively just a giant listbox that exists only to mess up my music library and occasionally try to sneak Safari and QuickTime onto my machines. But enough about Satan, let's yank some dead tracks, shall we?

我有提到它很烂吗? 它缓慢,令人沮丧,实际上只是一个巨大的列表框,其存在是为了弄乱我的音乐库,并偶尔尝试将Safari和QuickTime偷偷潜入我的计算机。 但是关于撒旦的事情已经足够了,让我们删除一些死路吧?

At some point, I ended up with hundreds of dead tracks and/or duplicates. I ended up with "LL Cool J - I Need Love 1.mp3" and "LL Cool J - I Need Love 2.mp3" along side the original. It was taking up many gigs of duplicate space. I searched for *1.mp3 and *2.mp3, etc, and deleted the dupes on disk.

在某个时候,我最终遇到了数百条不正确的轨迹和/或重复的轨迹。 我最后得到了“ LL Cool J-我需要爱1.mp3”和“ LL Cool J-我需要爱2.mp3”。 它占用了大量演出空间。 我搜索了* 1.mp3和* 2.mp3等,并删除了磁盘上的重复对象。

However, this then left me with a big iTunes database that THINKS it has music, even though the file on disk is long gone. Now, as a napalm-style solution, you CAN delete your iTunes library completely and re-add it. It'll add just files that exist, but you will lose any edits, artwork, etc, you may have added. This solution wasn't cool for me so I say, nay nay.

但是,这使我离开了一个大型iTunes数据库,认为它可以播放音乐,即使磁盘上的文件早已消失了。 现在,作为凝固汽油弹式的解决方案,您可以完全删除iTunes库并重新添加它。 它只会添加已存在的文件,但是您可能会丢失可能已添加的所有编辑,插图等。 这个解决方案对我来说并不酷,所以我说,不,不。

There's a MILLION stupid little shareware apps that purport to fix duplicates and remove dead tracks. In my case, removing dupes was easy, but yanking dead tracks isn't worth me spending money.

有百万个愚蠢的小型共享软件应用程序,旨在修复重复项并清除死线。 以我为例,删除骗子很容易,但是废除死路并不值得花钱。

Instead, I sat down tonight and decided to write a script in C# that said something like "foreach track, does that track exist where the system thinks it should be? No? Delete it."

相反,我今晚坐下来,决定用C#编写一个脚本,上面写着“ foreach轨道,该轨道是否存在于系统认为应该存在的位置?否?删除它”。

A little googling with Bing, however, brought me to a three year old post at ScarTech where Shawn (shame he's stopped blogging) did the code for me! Yay. Here's a bit of his code, using the iTunes COM SDK. His stuff was written using Visual Studio Express 2008, but I upgraded to 2010. Also, I'm on 64-bit and his COM code expects x86, so I changed the project properties from AnyCPU to x86 and it worked great.

但是,与Bing进行一些谷歌搜索后,把我带到ScarTech的一个三岁的岗位,在那儿,Shawn(可耻的是他停止写博客了)为我编写了代码! 好极了。 使用iTunes COM SDK,这是他的一些代码。 他的资料是使用Visual Studio Express 2008编写的,但我升级到了2010。此外,我使用的是64位,并且他的COM代码要求使用x86,因此我将项目属性从AnyCPU更改为x86,并且效果很好。

//create a reference to iTunes
iTunesAppClass iTunes = new iTunesAppClass();
//get a reference to the collection of all tracks
IITTrackCollection tracks = iTunes.LibraryPlaylist.Tracks;
for (int i = trackCount; i > 0; i–)
{
IITTrack track = tracks[i];
if (track.Kind == ITTrackKind.ITTrackKindFile)
{
IITFileOrCDTrack fileTrack = (IITFileOrCDTrack)track;
//if the file doesn’t exist, we’ll delete it from iTunes
if (fileTrack.Location == String.Empty || !System.IO.File.Exists(fileTrack.Location))
{
fileTrack.Delete();
}
}
}

Here's before:

在此之前:

1

So I ran it...

所以我跑了

iTunes COM Sample Screenshot

...and here's the afterparty:

...这是聚会之后的事情:

image

Looks like it checked 7219 tracks and removed 570 dead ones. Yum.

看起来它检查了7219条音轨,并删除了570条死音。 好吃

I'd also encourage you to check out the second part of Shawn's iTunes and C# tutorial series where he creates a duplicate finder that knows to keep the higher bitrate song.

我也鼓励您查看Shawn的iTunes和C#教程系列的第二部分,其中他创建了一个重复的查找器,该查找器知道可以保留更高比特率的歌曲

You can download Shawn's code here but as he hasn't blogged in a LONG time, I've mirrored it at SkyDrive. Big thanks to Shawn at ScarTech for saving me my evening and for tidying up my iTunes.

您可以在此处下载Shawn的代码,但是由于他很久没有写博客了,所以我在SkyDrive上做了镜像。 非常感谢ScarTech的Shawn挽救了我的夜晚并整理了iTunes。

Good stuff, the internets.

好东西,互联网。

翻译自: https://www.hanselman.com/blog/removing-dead-tracks-duplicates-that-dont-exist-from-itunes-using-c

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值