我解决了我的问题^^
以下代码扫描文件夹及其子目录,并为每个 .mp3 和 .wav 使用其 ID3 标签从 last.fm 中查找所有标签。
我需要添加更多文本来解释我的代码:
您可以使用主要的 args 来添加额外的选项,例如自定义位置,不做子目录,...。
使用2个参考:
• lastfm-sharp.dll
• taglib-sharp.dll
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Lastfm.Services;
namespace AddTags {
/// <summary>
/// Class made for scanning a folder and giving all mp3s more tags
/// </summary>
internal class Program {
public static void Main(string[] args) {
Session session = new Session("<api>", "<secret>");
// Authenticate it with a username and password to be able
// to perform write operations and access this user's profile
// private data.
session.Authenticate("<username>", Lastfm.Utilities.md5("<password>"));
DirectoryInfo dir = new DirectoryInfo("C:\\Users\\kiwi\\Dropbox\\music\\Mk3");
Console.WriteLine("Setting genretags for directory: " + dir.FullName);
foreach (FileInfo file in dir.GetFiles("*.*", SearchOption.AllDirectories).Where(file => file.Extension.Equals(".mp3") || file.Extension.Equals(".wav"))) {
Console.WriteLine();
Console.WriteLine(" --- " + file.Name + " ---");
TagLib.File TagFile = TagLib.File.Create(file.FullName);
// Create an Artist object.
if (TagFile.Tag.Performers.Length > 0) {
Artist artist = new Artist(TagFile.Tag.Performers[0], session);
// Display your current tags for system of a down.
List<string> tags = new List<string>();
try {
foreach (TopTag tag in artist.GetTopTags(20))
tags.Add(tag.Item.Name.ToString());
if (tags.Count == 0)
Console.WriteLine("No tags found");
} catch (Exception e) {
Console.WriteLine("Artist not found");
}
TagFile.Tag.Genres = tags.ToArray();
TagFile.Save();
foreach (string tag in TagFile.Tag.Genres) {
Console.Write(" " + tag);
}
} else {
Console.WriteLine("No artist found in tags");
}
Console.WriteLine();
}
Console.Write("press any key to exit");
Console.ReadLine();
}
}
}