//Collects the stacks saved in the mp3 file. saves them in the printouts folder. public static string ReturnCloudCoinStack(TagLib.File Mp3File) { TagLib.Ape.Tag ApeTag = Mp3Methods.CheckApeTag(Mp3File); TagLib.Ape.Item CCS = ApeTag.GetItem("CloudCoinStack"); TagLib.Ape.Item StackN = ApeTag.GetItem("StackName"); if (CCS != null) { string filename = StackN.ToString(); string message = "Press enter to extract the Cloudcoin stack from " + filename + "."; if (getEnter(message)) { Console.Out.WriteLine("Stack: " + filename + " has been found"); string CloudCoinAreaValues = CCS.ToString(); string path = "./Printouts/" + filename; try { System.IO.File.WriteAllText(path, CloudCoinAreaValues); //Create a document containing Mp3 ByteFile (debugging). } catch (Exception e) { Console.Out.WriteLine("Failed to save CloudCoin data {0}", e); } Console.Out.WriteLine("CCS: " + CloudCoinAreaValues); return(path); } return("null"); } else { Console.Out.WriteLine("no stack in file" + CCS); return("no .stack in file"); } }
/// <summary> /// Copies the values from the current instance to another /// <see cref="TagLib.Tag" />, optionally overwriting /// existing values. /// </summary> /// <param name="target"> /// A <see cref="TagLib.Tag" /> object containing the target /// tag to copy values to. /// </param> /// <param name="overwrite"> /// A <see cref="bool" /> specifying whether or not to copy /// values over existing one. /// </param> /// <remarks> /// <para>If <paramref name="target" /> is of type <see /// cref="TagLib.Ape.Tag" /> a complete copy of all values /// will be performed. Otherwise, only standard values will /// be copied.</para> /// </remarks> /// <exception cref="ArgumentNullException"> /// <paramref name="target" /> is <see langword="null" />. /// </exception> public override void CopyTo(TagLib.Tag target, bool overwrite) { if (target == null) { throw new ArgumentNullException("target"); } TagLib.Ape.Tag match = target as TagLib.Ape.Tag; if (match == null) { base.CopyTo(target, overwrite); return; } foreach (Item item in items) { if (!overwrite && match.GetItem(item.Key) != null) { continue; } match.items.Add(item.Clone()); } }
///Stores the CloudCoin.stack file as the value tied to the CloudCoinStack key. public static TagLib.Ape.Tag SetApeTagValue(TagLib.Ape.Tag ApeTag, string MyCloudCoin, string stackName) { // Get the APEv2 tag if it exists. try{ TagLib.Ape.Item currentStacks = ApeTag.GetItem("CloudCoinStack"); ApeTag.SetValue("CloudCoinStack", MyCloudCoin); ApeTag.SetValue("StackName", stackName); return(ApeTag); } catch (Exception e) { Console.Out.WriteLine("The process failed: {0}", e.ToString()); return(ApeTag); } }
public static string[] GetMiscTag(TagLib.File file, string name) { //TagLib.Mpeg4.AppleTag apple = (TagLib.Mpeg4.AppleTag)file.GetTag(TagLib.TagTypes.Apple); //TagLib.Id3v2.Tag id3v2 = (TagLib.Id3v2.Tag)file.GetTag(TagLib.TagTypes.Id3v2); TagLib.Ogg.XiphComment xiph = (TagLib.Ogg.XiphComment)file.GetTag(TagLib.TagTypes.Xiph); TagLib.Ape.Tag ape = (TagLib.Ape.Tag)file.GetTag(TagLib.TagTypes.Ape); //if (apple != null) //{ // string[] text = apple.GetText(name); // if (text.Length != 0) // return text; //} //if (id3v2 != null) // foreach (TagLib.Id3v2.Frame f in id3v2.GetFrames()) // if (f is TagLib.Id3v2.TextInformationFrame && ((TagLib.Id3v2.TextInformationFrame)f).Text != null) // return ((TagLib.Id3v2.TextInformationFrame)f).Text; if (xiph != null) { string[] l = xiph.GetField(name); if (l != null && l.Length != 0) { return(l); } } if (ape != null) { TagLib.Ape.Item item = ape.GetItem(name); if (item != null) { return(item.ToStringArray()); } } return(null); }
/// <summary> /// Read the Tags from the File /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static TrackData Create(string fileName) { TrackData track = new TrackData(); TagLib.File file = null; bool error = false; try { TagLib.ByteVector.UseBrokenLatin1Behavior = true; file = TagLib.File.Create(fileName); } catch (CorruptFileException) { log.Warn("File Read: Ignoring track {0} - Corrupt File!", fileName); error = true; } catch (UnsupportedFormatException) { log.Warn("File Read: Ignoring track {0} - Unsupported format!", fileName); error = true; } catch (FileNotFoundException) { log.Warn("File Read: Ignoring track {0} - Physical file no longer existing!", fileName); error = true; } catch (Exception ex) { log.Error("File Read: Error processing file: {0} {1}", fileName, ex.Message); error = true; } if (error) { return(null); } TagLib.Id3v2.Tag id3v2tag = null; try { if (file.MimeType.Substring(file.MimeType.IndexOf("/") + 1) == "mp3") { id3v2tag = file.GetTag(TagTypes.Id3v2, false) as TagLib.Id3v2.Tag; } } catch (Exception ex) { log.Error("File Read: Error retrieving id3tag: {0} {1}", fileName, ex.Message); return(null); } #region Set Common Values FileInfo fi = new FileInfo(fileName); try { track.Id = Guid.NewGuid(); track.FullFileName = fileName; track.FileName = Path.GetFileName(fileName); track.Readonly = fi.IsReadOnly; track.TagType = file.MimeType.Substring(file.MimeType.IndexOf("/") + 1); } catch (Exception ex) { log.Error("File Read: Error setting Common tags: {0} {1}", fileName, ex.Message); return(null); } #endregion #region Set Tags try { // Artist track.Artist = String.Join(";", file.Tag.Performers); if (track.Artist.Contains("AC;DC")) { track.Artist = track.Artist.Replace("AC;DC", "AC/DC"); } track.ArtistSortName = String.Join(";", file.Tag.PerformersSort); if (track.ArtistSortName.Contains("AC;DC")) { track.ArtistSortName = track.ArtistSortName.Replace("AC;DC", "AC/DC"); } track.AlbumArtist = String.Join(";", file.Tag.AlbumArtists); if (track.AlbumArtist.Contains("AC;DC")) { track.AlbumArtist = track.AlbumArtist.Replace("AC;DC", "AC/DC"); } track.AlbumArtistSortName = String.Join(";", file.Tag.AlbumArtistsSort); if (track.AlbumArtistSortName.Contains("AC;DC")) { track.AlbumArtistSortName = track.AlbumArtistSortName.Replace("AC;DC", "AC/DC"); } track.Album = file.Tag.Album ?? ""; track.AlbumSortName = file.Tag.AlbumSort ?? ""; track.BPM = (int)file.Tag.BeatsPerMinute; track.Compilation = id3v2tag == null ? false : id3v2tag.IsCompilation; track.Composer = string.Join(";", file.Tag.Composers); track.Conductor = file.Tag.Conductor ?? ""; track.Copyright = file.Tag.Copyright ?? ""; track.DiscNumber = file.Tag.Disc; track.DiscCount = file.Tag.DiscCount; track.Genre = string.Join(";", file.Tag.Genres); track.Grouping = file.Tag.Grouping ?? ""; track.Title = file.Tag.Title ?? ""; track.TitleSortName = file.Tag.TitleSort ?? ""; track.ReplayGainTrack = file.Tag.ReplayGainTrack ?? ""; track.ReplayGainTrackPeak = file.Tag.ReplayGainTrackPeak ?? ""; track.ReplayGainAlbum = file.Tag.ReplayGainAlbum ?? ""; track.ReplayGainAlbumPeak = file.Tag.ReplayGainAlbumPeak ?? ""; track.TrackNumber = file.Tag.Track; track.TrackCount = file.Tag.TrackCount; track.Year = (int)file.Tag.Year; // Pictures foreach (IPicture picture in file.Tag.Pictures) { MPTagThat.Core.Common.Picture pic = new MPTagThat.Core.Common.Picture { Type = picture.Type, MimeType = picture.MimeType, Description = picture.Description }; pic.Data = picture.Data.Data; track.Pictures.Add(pic); } // Comments if (track.IsMp3 && id3v2tag != null) { foreach (CommentsFrame commentsframe in id3v2tag.GetFrames <CommentsFrame>()) { track.ID3Comments.Add(new Comment(commentsframe.Description, commentsframe.Language, commentsframe.Text)); } } else { track.Comment = file.Tag.Comment; } // Lyrics track.Lyrics = file.Tag.Lyrics; if (track.IsMp3 && id3v2tag != null) { foreach (UnsynchronisedLyricsFrame lyricsframe in id3v2tag.GetFrames <UnsynchronisedLyricsFrame>()) { // Only add non-empty Frames if (lyricsframe.Text != "") { track.LyricsFrames.Add(new Lyric(lyricsframe.Description, lyricsframe.Language, lyricsframe.Text)); } } } // Rating if (track.IsMp3) { TagLib.Id3v2.PopularimeterFrame popmFrame = null; // First read in all POPM Frames found if (id3v2tag != null) { foreach (PopularimeterFrame popmframe in id3v2tag.GetFrames <PopularimeterFrame>()) { // Only add valid POPM Frames if (popmframe.User != "" && popmframe.Rating > 0) { track.Ratings.Add(new PopmFrame(popmframe.User, (int)popmframe.Rating, (int)popmframe.PlayCount)); } } popmFrame = TagLib.Id3v2.PopularimeterFrame.Get(id3v2tag, "MPTagThat", false); if (popmFrame != null) { track.Rating = popmFrame.Rating; } } if (popmFrame == null) { // Now check for Ape Rating TagLib.Ape.Tag apetag = file.GetTag(TagTypes.Ape, true) as TagLib.Ape.Tag; TagLib.Ape.Item apeItem = apetag.GetItem("RATING"); if (apeItem != null) { string rating = apeItem.ToString(); try { track.Rating = Convert.ToInt32(rating); } catch (Exception) { } } } } else if (track.TagType == "ape") { TagLib.Ape.Tag apetag = file.GetTag(TagTypes.Ape, true) as TagLib.Ape.Tag; TagLib.Ape.Item apeItem = apetag.GetItem("RATING"); if (apeItem != null) { string rating = apeItem.ToString(); try { track.Rating = Convert.ToInt32(rating); } catch (Exception) { } } } else if (track.TagType == "ogg" || track.TagType == "flac") { XiphComment xiph = file.GetTag(TagLib.TagTypes.Xiph, false) as XiphComment; string[] rating = xiph.GetField("RATING"); if (rating.Length > 0) { try { track.Rating = Convert.ToInt32(rating[0]); } catch (Exception) { } } } } catch (Exception ex) { log.Error("Exception setting Tags for file: {0}. {1}", fileName, ex.Message); } #endregion #region Set Properties try { track.DurationTimespan = file.Properties.Duration; int fileLength = (int)(fi.Length / 1024); track.FileSize = fileLength.ToString(); track.BitRate = file.Properties.AudioBitrate.ToString(); track.SampleRate = file.Properties.AudioSampleRate.ToString(); track.Channels = file.Properties.AudioChannels.ToString(); track.Version = file.Properties.Description; track.CreationTime = string.Format("{0:yyyy-MM-dd HH:mm:ss}", fi.CreationTime); track.LastWriteTime = string.Format("{0:yyyy-MM-dd HH:mm:ss}", fi.LastWriteTime); } catch (Exception ex) { log.Error("Exception setting Properties for file: {0}. {1}", fileName, ex.Message); } #endregion // Now copy all Text frames of an ID3 V2 try { if (track.IsMp3 && id3v2tag != null) { foreach (TagLib.Id3v2.Frame frame in id3v2tag.GetFrames()) { string id = frame.FrameId.ToString(); if (!Util.StandardFrames.Contains(id) && Util.ExtendedFrames.Contains(id)) { track.Frames.Add(new Frame(id, "", frame.ToString())); } else if (!Util.StandardFrames.Contains(id) && !Util.ExtendedFrames.Contains(id)) { if ((Type)frame.GetType() == typeof(UserTextInformationFrame)) { // Don't add Replaygain frames, as they are handled in taglib tags if (!Util.IsReplayGain((frame as UserTextInformationFrame).Description)) { track.UserFrames.Add(new Frame(id, (frame as UserTextInformationFrame).Description ?? "", (frame as UserTextInformationFrame).Text.Length == 0 ? "" : (frame as UserTextInformationFrame).Text[0])); } } else if ((Type)frame.GetType() == typeof(PrivateFrame)) { track.UserFrames.Add(new Frame(id, (frame as PrivateFrame).Owner ?? "", (frame as PrivateFrame).PrivateData == null ? "" : (frame as PrivateFrame).PrivateData.ToString())); } else if ((Type)frame.GetType() == typeof(UniqueFileIdentifierFrame)) { track.UserFrames.Add(new Frame(id, (frame as UniqueFileIdentifierFrame).Owner ?? "", (frame as UniqueFileIdentifierFrame).Identifier == null ? "" : (frame as UniqueFileIdentifierFrame).Identifier.ToString())); } else if ((Type)frame.GetType() == typeof(UnknownFrame)) { track.UserFrames.Add(new Frame(id, "", (frame as UnknownFrame).Data == null ? "" : (frame as UnknownFrame).Data.ToString())); } else { track.UserFrames.Add(new Frame(id, "", frame.ToString())); } } } track.ID3Version = id3v2tag.Version; } } catch (Exception ex) { log.Error("Exception getting User Defined frames for file: {0}. {1}", fileName, ex.Message); } return(track); }
/// <summary> /// This method is called by mediaportal when it wants information for a music file /// The method will check which tagreader supports the file and ask it to extract the information from it /// </summary> /// <param name="strFile">filename of the music file</param> /// <returns> /// MusicTag instance when file has been read /// null when file type is not supported or if the file does not contain any information /// </returns> public static MusicTag ReadTag(string strFile) { // Read Cue info if (CueUtil.isCueFakeTrackFile(strFile)) { try { return(CueUtil.CueFakeTrackFile2MusicTag(strFile)); } catch (Exception ex) { Log.Warn("TagReader: Exception reading file {0}. {1}", strFile, ex.Message); } } if (!IsAudio(strFile)) { return(null); } char[] trimChars = { ' ', '\x00' }; try { // Set the flag to use the standard System Encoding set by the user // Otherwise Latin1 is used as default, which causes characters in various languages being displayed wrong TagLib.ByteVector.UseBrokenLatin1Behavior = true; TagLib.File tag = TagLib.File.Create(strFile); if (tag == null) { Log.Warn("Tagreader: No tag in file - {0}", strFile); return(null); } MusicTag musictag = new MusicTag(); string[] artists = tag.Tag.Performers; if (artists.Length > 0) { musictag.Artist = String.Join(";", artists).Trim(trimChars); // The AC/DC exception if (musictag.Artist.Contains("AC;DC")) { musictag.Artist = musictag.Artist.Replace("AC;DC", "AC/DC"); } } musictag.Album = tag.Tag.Album == null ? "" : tag.Tag.Album.Trim(trimChars); musictag.HasAlbumArtist = false; string[] albumartists = tag.Tag.AlbumArtists; if (albumartists.Length > 0) { musictag.AlbumArtist = String.Join(";", albumartists).Trim(trimChars); musictag.HasAlbumArtist = true; // The AC/DC exception if (musictag.AlbumArtist.Contains("AC;DC")) { musictag.AlbumArtist = musictag.AlbumArtist.Replace("AC;DC", "AC/DC"); } } musictag.BitRate = tag.Properties.AudioBitrate; musictag.Comment = tag.Tag.Comment == null ? "" : tag.Tag.Comment.Trim(trimChars); string[] composer = tag.Tag.Composers; if (composer.Length > 0) { musictag.Composer = string.Join(";", composer).Trim(trimChars); } musictag.Conductor = tag.Tag.Conductor == null ? "" : tag.Tag.Conductor.Trim(trimChars); IPicture[] pics = new IPicture[] { }; pics = tag.Tag.Pictures; if (pics.Length > 0) { musictag.CoverArtImageBytes = pics[0].Data.Data; } musictag.Duration = (int)tag.Properties.Duration.TotalSeconds; musictag.FileName = strFile; musictag.FileType = tag.MimeType.Substring(tag.MimeType.IndexOf("/") + 1); string[] genre = tag.Tag.Genres; if (genre.Length > 0) { musictag.Genre = String.Join(";", genre).Trim(trimChars); } string lyrics = tag.Tag.Lyrics == null ? "" : tag.Tag.Lyrics.Trim(trimChars); musictag.Title = tag.Tag.Title == null ? "" : tag.Tag.Title.Trim(trimChars); // Prevent Null Ref execption, when Title is not set musictag.Track = (int)tag.Tag.Track; musictag.TrackTotal = (int)tag.Tag.TrackCount; musictag.DiscID = (int)tag.Tag.Disc; musictag.DiscTotal = (int)tag.Tag.DiscCount; musictag.Codec = tag.Properties.Description; if (tag.MimeType == "taglib/mp3") { musictag.BitRateMode = tag.Properties.Description.IndexOf("VBR") > -1 ? "VBR" : "CBR"; } else { musictag.BitRateMode = ""; } musictag.BPM = (int)tag.Tag.BeatsPerMinute; musictag.Channels = tag.Properties.AudioChannels; musictag.SampleRate = tag.Properties.AudioSampleRate; musictag.Year = (int)tag.Tag.Year; musictag.ReplayGainTrack = tag.Tag.ReplayGainTrack ?? ""; musictag.ReplayGainTrackPeak = tag.Tag.ReplayGainTrackPeak ?? ""; musictag.ReplayGainAlbum = tag.Tag.ReplayGainAlbum ?? ""; musictag.ReplayGainAlbumPeak = tag.Tag.ReplayGainAlbumPeak ?? ""; if (tag.MimeType == "taglib/mp3") { bool foundPopm = false; // Handle the Rating, which comes from the POPM frame TagLib.Id3v2.Tag id32_tag = tag.GetTag(TagLib.TagTypes.Id3v2) as TagLib.Id3v2.Tag; if (id32_tag != null) { // Do we have a POPM frame written by MediaPortal or MPTagThat? TagLib.Id3v2.PopularimeterFrame popmFrame = TagLib.Id3v2.PopularimeterFrame.Get(id32_tag, "MediaPortal", false); if (popmFrame == null) { popmFrame = TagLib.Id3v2.PopularimeterFrame.Get(id32_tag, "MPTagThat", false); } if (popmFrame != null) { musictag.Rating = popmFrame.Rating; foundPopm = true; } // Now look for a POPM frame written by WMP if (!foundPopm) { TagLib.Id3v2.PopularimeterFrame popm = TagLib.Id3v2.PopularimeterFrame.Get(id32_tag, "Windows Media Player 9 Series", false); if (popm != null) { // Get the rating stored in the WMP POPM frame int rating = popm.Rating; int i = 0; if (rating == 255) { i = 5; } else if (rating == 196) { i = 4; } else if (rating == 128) { i = 3; } else if (rating == 64) { i = 2; } else if (rating == 1) { i = 1; } musictag.Rating = i; foundPopm = true; } } if (!foundPopm) { // Now look for any other POPM frame that might exist foreach (TagLib.Id3v2.PopularimeterFrame popm in id32_tag.GetFrames <TagLib.Id3v2.PopularimeterFrame>()) { int rating = popm.Rating; int i = 0; if (rating > 205 || rating == 5) { i = 5; } else if (rating > 154 || rating == 4) { i = 4; } else if (rating > 104 || rating == 3) { i = 3; } else if (rating > 53 || rating == 2) { i = 2; } else if (rating > 0 || rating == 1) { i = 1; } musictag.Rating = i; foundPopm = true; break; // we only take the first popm frame } } if (!foundPopm) { // If we don't have any POPM frame, we might have an APE Tag embedded in the mp3 file TagLib.Ape.Tag apetag = tag.GetTag(TagTypes.Ape, false) as TagLib.Ape.Tag; if (apetag != null) { TagLib.Ape.Item apeItem = apetag.GetItem("RATING"); if (apeItem != null) { string rating = apeItem.ToString(); try { musictag.Rating = Convert.ToInt32(rating); } catch (Exception ex) { musictag.Rating = 0; Log.Warn("Tagreader: Unsupported APE rating format - {0} in {1} {2}", rating, strFile, ex.Message); } } } } } } else { if (tag.MimeType == "taglib/ape") { TagLib.Ape.Tag apetag = tag.GetTag(TagTypes.Ape, false) as TagLib.Ape.Tag; if (apetag != null) { TagLib.Ape.Item apeItem = apetag.GetItem("RATING"); if (apeItem != null) { string rating = apeItem.ToString(); try { musictag.Rating = Convert.ToInt32(rating); } catch (Exception ex) { musictag.Rating = 0; Log.Warn("Tagreader: Unsupported APE rating format - {0} in {1} {2}", rating, strFile, ex.Message); } } } } } // if we didn't get a title, use the Filename without extension to prevent the file to appear as "unknown" if (musictag.Title == "") { Log.Warn("TagReader: Empty Title found in file: {0}. Please retag.", strFile); musictag.Title = System.IO.Path.GetFileNameWithoutExtension(strFile); } return(musictag); } catch (UnsupportedFormatException) { Log.Warn("Tagreader: Unsupported File Format {0}", strFile); } catch (Exception ex) { Log.Warn("TagReader: Exception reading file {0}. {1}", strFile, ex.Message); } return(null); }