Пример #1
0
        private bool WriteMp3Metadata()
        {
            using (TagLib.File file = TagLib.File.Create(_filepath, "taglib/mp3", ReadStyle.Average))
            {
                var tags = (Tag)file.GetTag(TagTypes.Id3v2);

                if (_coverBytes.Length > 0)
                {
                    var byteVector   = new ByteVector(_coverBytes);
                    var coverPicture = new Picture
                    {
                        Description = "Cover",
                        Data        = byteVector,
                        MimeType    = MediaTypeNames.Image.Jpeg,
                        Type        = PictureType.FrontCover
                    };
                    var attachedPictureFrame = new AttachedPictureFrame(coverPicture);

                    tags.Pictures = new IPicture[] { attachedPictureFrame };
                }

                tags.AddFrame(MetadataHelpers.BuildTextInformationFrame("TMED", "Digital Media"));

                if (_trackInfo?.TrackTags != null)
                {
                    tags.Title        = _trackInfo.TrackTags.Title;
                    tags.Performers   = _trackInfo.TrackTags.Artists.Select(x => x.Name).ToArray();
                    tags.AlbumArtists = _albumInfo.AlbumTags.Artists.Select(x => x.Name).ToArray(); // i don't like just using ART_NAME as the only album artist
                    tags.Composers    = _trackInfo.TrackTags.Contributors.Composers;

                    if (_trackInfo.TrackTags.TrackNumber != null)
                    {
                        tags.Track = uint.Parse(_trackInfo.TrackTags.TrackNumber);
                    }

                    if (_trackInfo.TrackTags.DiscNumber != null)
                    {
                        tags.Disc = uint.Parse(_trackInfo.TrackTags.DiscNumber);
                    }

                    if (_trackInfo.TrackTags.Bpm != null)
                    {
                        string bpm = _trackInfo.TrackTags.Bpm;

                        if (double.TryParse(bpm, out double bpmParsed))
                        {
                            bpmParsed = Math.Round(bpmParsed);
                            bpm       = bpmParsed.ToString(CultureInfo.InvariantCulture);
                        }

                        tags.BeatsPerMinute = uint.Parse(bpm);
                    }

                    tags.AddFrame(MetadataHelpers.BuildTextInformationFrame("TSRC", _trackInfo.TrackTags.Isrc));
                    tags.AddFrame(MetadataHelpers.BuildTextInformationFrame("TPUB", _trackInfo.TrackTags.Contributors.Publishers));
                    tags.AddFrame(MetadataHelpers.BuildTextInformationFrame("TLEN", _trackInfo.TrackTags.Length));
                    tags.AddFrame(MetadataHelpers.BuildUserTextInformationFrame("EXPLICIT", _trackInfo.TrackTags.ExplicitLyrics));
                    tags.AddFrame(MetadataHelpers.BuildUserTextInformationFrame("REPLAYGAIN_TRACK_GAIN", _trackInfo.TrackTags.Gain));
                    tags.AddFrame(MetadataHelpers.BuildUserTextInformationFrame("WRITERS", _trackInfo.TrackTags.Contributors.Writers));
                    tags.AddFrame(MetadataHelpers.BuildUserTextInformationFrame("AUTHORS", _trackInfo.TrackTags.Contributors.Authors));
                    tags.AddFrame(MetadataHelpers.BuildUserTextInformationFrame("TIPL", "PRODUCERS", _trackInfo.TrackTags.Contributors.Publishers));
                    tags.AddFrame(MetadataHelpers.BuildUserTextInformationFrame("TIPL", "ENGINEERS", _trackInfo.TrackTags.Contributors.Engineers));
                    tags.AddFrame(MetadataHelpers.BuildUserTextInformationFrame("TIPL", "MIXERS", _trackInfo.TrackTags.Contributors.Mixers));
                }

                if (_albumInfo?.AlbumTags != null)
                {
                    tags.Album  = _albumInfo.AlbumTags.Title;
                    tags.Genres = _albumInfo.AlbumTags.Genres.GenreData.Select(x => x.Name).ToArray();

                    if (_albumInfo.AlbumTags.NumberOfTracks != null)
                    {
                        tags.TrackCount = uint.Parse(_albumInfo.AlbumTags.NumberOfTracks);
                    }

                    if (_albumInfo.AlbumTags.NumberOfDiscs != null)
                    {
                        tags.DiscCount = uint.Parse(_albumInfo.AlbumTags.NumberOfDiscs);
                    }

                    if (_albumInfo.AlbumTags.Type == "Compilation" || _albumInfo.AlbumTags.Type == "Playlist")
                    {
                        tags.IsCompilation = true;
                    }

                    tags.Copyright = _albumInfo.AlbumTags.Copyright;

                    string year = _albumInfo.AlbumTags.ReleaseDate;

                    if (!string.IsNullOrWhiteSpace(year))
                    {
                        string[] yearSplit = year.Split("-");

                        if (yearSplit[0].Length == 4 && uint.TryParse(yearSplit[0], out uint yearParsed))
                        {
                            tags.Year = yearParsed;
                        }
                    }

                    tags.AddFrame(MetadataHelpers.BuildTextInformationFrame("TPUB", _albumInfo.AlbumTags.Label));
                    tags.AddFrame(MetadataHelpers.BuildTextInformationFrame("TDOR", _albumInfo.AlbumTags.ReleaseDate));
                    tags.AddFrame(MetadataHelpers.BuildUserTextInformationFrame("UPC", _albumInfo.AlbumTags.Upc));
                }

                if (_trackInfo?.Lyrics != null)
                {
                    tags.Lyrics = _trackInfo.Lyrics.UnSyncedLyrics;
                    WriteLyricsFile();
                }

                try
                {
                    file.Save();
                }
                catch (IOException ex)
                {
                    Console.WriteLine(ex.Message);
                    return(false);
                }
            }

            return(true);
        }
Пример #2
0
        private void LoadFiles(string folderpath)
        {
            List <TagTypes> tags = new List <TagTypes>()
            {
                TagTypes.Ape, TagTypes.Apple, TagTypes.Asf, TagTypes.AudibleMetadata, TagTypes.DivX,
                TagTypes.FlacMetadata, TagTypes.GifComment, TagTypes.IPTCIIM, TagTypes.Id3v1, TagTypes.Id3v2,
                TagTypes.JpegComment, TagTypes.MovieId, TagTypes.Png, TagTypes.RiffInfo, TagTypes.TiffIFD,
                TagTypes.XMP, TagTypes.Xiph
            };

            List <string> additionalTags = new List <string>()
            {
                "album", "artists", "comment", "lyrics", "composers", "disc",
                "disccount", "genres", "performers", "title", "trackcount",
                "year"
            };

            Mp3Files.SelectionMode = SelectionMode.Multiple;
            Mp3Files.Items.Clear();
            var files = GetFilesInFolder(folderpath);

            foreach (var file in files)
            {
                using (TagLib.File f = TagLib.File.Create(file))
                {
                    string name = Path.GetFileName(file);

                    Trace.WriteLine("Some info: " + name);
                    foreach (var tag in tags)
                    {
                        try
                        {
                            var info = f.GetTag(tag);
                            if (info != null)
                            {
                                Trace.WriteLine("  " + tag + ": " + info);
                            }
                        }
                        catch
                        {
                            // ignore
                        }
                    }

                    foreach (var tagname in additionalTags)
                    {
                        try
                        {
                            ShowTag(f, tagname);
                        }
                        catch
                        {
                            // ignore
                        }
                    }

                    //string tagPerfomer = string.Join(",", f.Tag.PerformersSort);
                    string tagAlbum = f.Tag.Album;
                    string tagTitle = f.Tag.Title;

                    bool hasId3Tag = !string.IsNullOrEmpty(tagAlbum) || !string.IsNullOrEmpty(tagTitle);

                    string additionalInformation = string.Format("{0} - {1}",
                                                                 string.IsNullOrEmpty(tagAlbum) ? "x" : tagAlbum,
                                                                 string.IsNullOrEmpty(tagTitle) ? "x" : tagTitle
                                                                 );

                    var item = new FileItem()
                    {
                        Name      = string.Format("{0} ({1})", name, additionalInformation),
                        Filename  = file,
                        HasId3Tag = hasId3Tag
                    };

                    Mp3Files.Items.Add(item);
                }
            }
            Mp3Files.SelectAll();
        }