Beispiel #1
0
        // Read a media-file's metadata2 (tags) into a simple SongInfo2 struct that
        // uses little memory... use a static method to initialize it.
        //---------------------------------------------------------------------------
        public SongInfo2 Read2(string file)
        {
            string ext = Path.GetExtension(file).ToLower();
            SongInfo2 si = new SongInfo2();

            si.FilePath = file;

            if (ext == ".wma" || ext == ".asf" || ext == ".wmv" || ext == ".wm")
            {
                try
                {
                    si.FileType = SongInfo2.FT_WMA; // wma tag info

                    MediaDataManager mdm = new MediaDataManager();

                    if (mdm == null)
                    {
                        si.bException = true; // Error
                        return si;
                    }

                    TrackInfo ti;

                    try { ti = mdm.RetrieveTrackInfo(file); }
                    catch
                    {
                        si.bException = true;
                        return si;
                    }

                    si.Duration = ti.Duration; if (si.Duration.TotalSeconds > 0) si.bDurationTag = true;
                    if (ti.Title != null) { si.Title = ti.Title.Trim(); if (!string.IsNullOrEmpty(si.Title)) si.bTitleTag = true; }
                    if (ti.AlbumArtist != null) { si.Artist = ti.AlbumArtist.Trim(); if (!string.IsNullOrEmpty(si.Artist)) si.bArtistTag = true; }

                    // ti.Author: The Author attribute is the name of a media artist or actor associated with the content.
                    // This attribute may have multiple values. To retrieve all of the values for a multi-valued attribute, you must use
                    // the Media.getItemInfoByType method, not the Media.getItemInfo (WinMediaLib project) method.
                    // Same as FirstPerformer in a mp3 file
                    if (ti.Author != null) { si.Performer = ti.Author.Trim(); if (!string.IsNullOrEmpty(si.Performer)) si.bPerformerTag = true; }

                    if (ti.AlbumTitle != null) { si.Album = ti.AlbumTitle.Trim(); if (!string.IsNullOrEmpty(si.Album)) si.bAlbumTag = true; }
                }
                catch { si.bException = true; } // Threw an exception
            }
            else using (TagLib.File tagFile = TagLib.File.Create(file)) // Use taglib-sharp...
                {
                    try
                    {
                        si.FileType = SongInfo2.FT_MP3; // mp3 tag info

                        if (tagFile == null)
                        {
                            si.bException = true; // Error
                            return si;
                        }

                        si.Duration = tagFile.Properties.Duration; if (si.Duration.TotalSeconds > 0) si.bDurationTag = true;

                        // Try to read the v2 tag-frame...
                        TagLib.Id3v2.Tag tagv2 = (TagLib.Id3v2.Tag)tagFile.GetTag(TagLib.TagTypes.Id3v2);

                        if (tagv2 != null)
                        {
                            if (tagv2.Title != null) { si.Title = tagv2.Title.Trim(); if (!string.IsNullOrEmpty(si.Title)) si.bTitleTag = true; }
                            if (tagv2.FirstAlbumArtist != null) { si.Artist = tagv2.FirstAlbumArtist.Trim(); if (!string.IsNullOrEmpty(si.Artist)) si.bArtistTag = true; }
                            if (tagv2.FirstPerformer != null) { si.Performer = tagv2.FirstPerformer.Trim(); if (!string.IsNullOrEmpty(si.Performer)) si.bPerformerTag = true; }  // Same as Author in a wma file
                            if (tagv2.Album != null) { si.Album = tagv2.Album.Trim(); if (!string.IsNullOrEmpty(si.Album)) si.bAlbumTag = true; }
                        }
                        else // Try to read the old v1 tag
                        {
                            // Try to read the v1 tag-frame...
                            TagLib.Id3v1.Tag tagv1 = (TagLib.Id3v1.Tag)tagFile.GetTag(TagLib.TagTypes.Id3v1);

                            if (tagv1.Title != null) { si.Title = tagv1.Title.Trim(); if (!string.IsNullOrEmpty(si.Title)) si.bTitleTag = true; }
                            if (tagv1.FirstAlbumArtist != null) { si.Artist = tagv1.FirstAlbumArtist.Trim(); if (!string.IsNullOrEmpty(si.Artist)) si.bArtistTag = true; }
                            if (tagv1.FirstPerformer != null) { si.Performer = tagv1.FirstPerformer.Trim(); if (!string.IsNullOrEmpty(si.Performer)) si.bPerformerTag = true; }  // Same as Author in a wma file
                            if (tagv1.Album != null) { si.Album = tagv1.Album.Trim(); if (!string.IsNullOrEmpty(si.Album)) si.bAlbumTag = true; }
                        }
                    }
                    catch { si.bException = true; } // Threw an exception
                }

            return si;
        }
Beispiel #2
0
        // Write a media-file's metadata (tags) from a SongInfo2 struct...
        // Returns true if success.
        //---------------------------------------------------------------------------
        public bool Write2(SongInfo2 si, string file)
        {
            bool bRet = false; // Presume error

            // ext == ".wma" || ext == ".asf" || ext == ".wmv" || ext == ".wm"
            if (FilterInList(Path.GetExtension(file).ToLower()))
            {
                try
                {
                    using (MediaDataManager Editor = new MediaDataManager()) // WMA Editor...
                    {

                        // See the end of this file for a list of windows media attributes...
                        if (si.bAlbumTag) Editor.SetAttrib(file, 0, "WM/AlbumTitle", WmTypes.WM_TYPE_STRING, si.Album);
                        if (si.bTitleTag) Editor.SetAttrib(file, 0, "Title", WmTypes.WM_TYPE_STRING, si.Title);
                        if (si.bArtistTag) Editor.SetAttrib(file, 0, "WM/AlbumArtist", WmTypes.WM_TYPE_STRING, si.Artist);
                        if (si.bPerformerTag) Editor.SetAttrib(file, 0, "Author", WmTypes.WM_TYPE_STRING, si.Performer);

                        bRet = true;
                    } // end using
                }
                catch { }
            }
            else try
                {
                    using (TagLib.File f = TagLib.File.Create(file)) // Use taglib-sharp...
                    {
                        // Get the tag-frame and create it if necessary...
                        TagLib.Id3v2.Tag tag = (TagLib.Id3v2.Tag)f.GetTag(TagLib.TagTypes.Id3v2, true);

                        if (tag == null) return false;

                        if (si.bAlbumTag)
                            tag.Album = si.Album;
                        if (si.bTitleTag)
                            tag.Title = si.Title;

                        if (si.bArtistTag)
                        {
                            // Get list of artists, if any, and re-write
                            string[] Artists = tag.AlbumArtists;

                            if (Artists.Length == 0)
                                tag.AlbumArtists = new string[] { si.Artist };
                            else
                            {
                                Artists[0] = si.Artist;
                                tag.AlbumArtists = Artists;
                            }
                        }

                        if (si.bPerformerTag)
                        {
                            // Get list of performers, if any, and re-write
                            string[] Performers = tag.Performers;

                            if (Performers.Length == 0)
                                tag.Performers = new string[] { si.Performer };
                            else
                            {
                                Performers[0] = si.Performer;
                                tag.Performers = Performers;
                            }
                        }

                        tag.CopyTo(f.Tag, true); // Overwrite the song file's tag...
                        f.Save(); // Save tag

                        bRet = true;
                    } // end using
                }
                catch { }

            return bRet;
        }
Beispiel #3
0
        //---------------------------------------------------------------------------
        public SongInfo2 ParsePath(string path)
        {
            SongInfo2 si = new SongInfo2();

            try
            {
                si.Title = Path.GetFileNameWithoutExtension(path); // Title

                int comparelen = 3; // expected tokins: artist, album, song

                // If MediaTags g_root property was set, parse it out of the song-path
                if (!string.IsNullOrEmpty(g_root) && path.StartsWith(g_root)) path = path.Substring(g_root.Length, path.Length - g_root.Length);

                if (Path.IsPathRooted(path)) comparelen++; // Adjust # expected tokins up if drive-letter present...

                // Split into 0=drive, 1=artist, 2=album, 3=song
                // Split into 0=artist, 1=album, 2=song (if not rooted)
                string[] paths = path.Split(Path.DirectorySeparatorChar);

                int len = paths.Length;

                if (len >= comparelen)
                {
                    si.Artist = paths[len - 3]; // Artist
                    si.Album = paths[len - 2]; // Album
                }
                else if (len >= comparelen - 1) si.Album = paths[len - 2]; // Album
            }
            catch { si.bException = true; } // Threw an exception

            return si;
        }