获取歌曲标签并添加到ID3标签列表

时间:2012-11-10 16:03:58

标签: c# json xml id3 last.fm

我解决了我的问题^^

以下代码扫描文件夹及其子目录,以及每个.mp3& .wav使用其ID3标记来查找last.fm中的所有标记。

我需要添加更多文字来解释我的代码:

你可以使用main中的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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

请参阅第一篇文章,了解相同的事情xD

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 al mp3's moar tags
    /// </summary>
    internal class Program {

        public static void Main(string[] args) {
            Session session = new Session("94b659f1b129a3f110a0faa269b4be68", "649461398a675e38870c07b1614085e4");

            // Authenticate it with a username and password to be able
            // to perform write operations and access this user's profile
            // private data.
            session.Authenticate("cskiwi", Lastfm.Utilities.md5("Cskiwi147963"));

            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();
        }
    }
}