public static bool SaveTrack(Track track)
        {
            var tags = new ID3v2Tag(track.Filename)
            {
                Genre = track.Genre.Replace(NoValue, ""),
                Album = track.Album.Replace(NoValue, ""),
                TrackNumber = track.TrackNumber.ToString(),
                LengthMilliseconds = Convert.ToInt32(track.FullLength*1000M),
                BPM = track.Bpm.ToString("0.00"),
                InitialKey = track.Key
            };

            if (track.Artist == track.AlbumArtist)
            {
                tags.Artist = track.Artist.Replace(NoValue, "");
                tags.Title = track.Title.Replace(NoValue, "");
            }
            else
            {
                tags.Artist = track.AlbumArtist.Replace(NoValue, "");
                tags.Title = track.Artist.Replace(NoValue, "") + " / " + track.Title.Replace(NoValue, "");
            }
            try
            {
                tags.Save(track.Filename);
            }
            catch
            {
                return false;
            }

            track.OriginalDescription = track.Description;

            return true;
        }
Example #2
0
        static void Main(string[] args)
        {
            bool success = ParseArguments(args);
            if (!success)
                return;

            DateTime start = DateTime.Now;

            if (_isTest && _verbosity >= Verbosity.Default)
            {
                WriteLine("TEST - NO ACTUAL FILES WILL BE CHANGED");
                WriteLine();
            }

            if (_verbosity >= Verbosity.Default)
            {
                if (_directory != null)
                    WriteLine(string.Format("Directory: \"{0}\"", _directory));
                else
                    WriteLine(string.Format("File: \"{0}\"", _fileName));

                string options = "Options: Convert to " + _newTagVersion;
                if (_isTest)
                    options += ", test";
                if (_isRecursive)
                    options += ", recurisve";
                if (_upgradeOnly)
                    options += ", upgrade only";

                WriteLine(options);
            }

            string[] files;
            if (_directory != null)
            {
                WriteLine(string.Format("Getting '{0}' files...", _directory), Verbosity.Full);

                files = Directory.GetFiles(_directory, "*.mp3", _isRecursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
            }
            else
            {
                files = new[] { _fileName };
            }

            int updated = 0, skipped = 0, failed = 0;
            int id3v22Count = 0, id3v23Count = 0, id3v24Count = 0, noid3v2Count = 0;
            for (int i = 0; i < files.Length; i++)
            {
                string file = files[i];

                try
                {
                    WriteLine(string.Format("File {0:#,0}/{1:#,0}: '{2}'", i + 1, files.Length, file), Verbosity.Full);

                    ID3v2Tag id3v2 = new ID3v2Tag(file);
                    if (id3v2.Header != null)
                    {
                        switch (id3v2.Header.TagVersion)
                        {
                            case ID3v2TagVersion.ID3v22:
                                id3v22Count++;
                                break;
                            case ID3v2TagVersion.ID3v23:
                                id3v23Count++;
                                break;
                            case ID3v2TagVersion.ID3v24:
                                id3v24Count++;
                                break;
                        }

                        if (id3v2.Header.TagVersion != _newTagVersion.Value)
                        {
                            if (!_upgradeOnly || _newTagVersion.Value > id3v2.Header.TagVersion)
                            {
                                ID3v2TagVersion oldTagVersion = id3v2.Header.TagVersion;

                                if (!_isTest)
                                {
                                    id3v2.Header.TagVersion = _newTagVersion.Value;
                                    id3v2.Save(file);
                                }

                                WriteLine(string.Format("- Converted {0} to {1}", oldTagVersion, _newTagVersion.Value), Verbosity.Full);

                                updated++;
                            }
                            else
                            {
                                WriteLine(string.Format("- Skipped, existing tag {0} > {1}", id3v2.Header.TagVersion, _newTagVersion.Value), Verbosity.Full);

                                skipped++;
                            }
                        }
                        else
                        {
                            WriteLine("- Skipped, ID3v2 tag version equal to requested version", Verbosity.Full);

                            skipped++;
                        }
                    }
                    else
                    {
                        WriteLine("- Skipped, ID3v2 tag does not exist", Verbosity.Full);

                        skipped++;
                        noid3v2Count++;
                    }
                }
                catch (Exception ex)
                {
                    if (_verbosity >= Verbosity.Default)
                    {
                        string header = string.Format("File: {0}", file);
                        WriteLine(header);
                        WriteLine(new string('=', header.Length));
                        WriteLine(ex.ToString());
                        WriteLine();
                    }

                    failed++;
                }
            }

            if (_verbosity >= Verbosity.Default)
            {
                DateTime end = DateTime.Now;
                TimeSpan duration = end - start;

                if (_hasWritten)
                    WriteLine();
                WriteLine(string.Format("ID3v2.2:   {0:#,0}", id3v22Count));
                WriteLine(string.Format("ID3v2.3:   {0:#,0}", id3v23Count));
                WriteLine(string.Format("ID3v2.4:   {0:#,0}", id3v24Count));
                WriteLine(string.Format("No ID3v2:  {0:#,0}", noid3v2Count));
                WriteLine();
                WriteLine(string.Format("Start:     {0:HH:mm:ss}", start));
                WriteLine(string.Format("End:       {0:HH:mm:ss}", end));
                WriteLine(string.Format("Duration:  {0:hh}:{0:mm}:{0:ss}.{0:fff}", duration));
                WriteLine();
                WriteLine(string.Format("Updated:   {0:#,0}", updated));
                WriteLine(string.Format("Skipped:   {0:#,0}", skipped));
                WriteLine(string.Format("Failed:    {0:#,0}", failed));
            }
        }
        private void CreateAudioFile(string artist, string album, string song)
        {
            var outputDirectoryPath = Path.Combine(OutputPath, artist, album);
            var outputFilePath = Path.Combine(outputDirectoryPath, song + ".mp3");

            if (File.Exists(outputFilePath))
            {
                return;
            }

            if (!Directory.Exists(outputDirectoryPath))
            {
                Directory.CreateDirectory(outputDirectoryPath);
            }

            //if (File.Exists(outputFilePath))
            //{
            //    File.Delete(outputFilePath);
            //}

            File.Copy(SourceFile, outputFilePath);

            var tag = new ID3v2Tag(outputFilePath);
            tag.Artist = artist;
            tag.Album = album;
            tag.AlbumArtist = artist;
            tag.Title = song;

            tag.Save(outputFilePath);
        }
Example #4
0
        /// <summary>Saves the tag to the specified path.</summary>
        /// <param name="path">The full path of the file.</param>
        private void Save(string path)
        {
            if (string.IsNullOrEmpty(path))
                throw new ArgumentNullException("path");

            if (VorbisComment.VorbisComment.IsFlac(path))
            {
                VorbisComment.VorbisComment vc = new VorbisComment.VorbisComment(path);
                vc.Title = Title;
                vc.Artist = Artist;
                vc.Album = Album;
                vc.Year = Year;
                vc.Genre = Genre;
                vc.TrackNumber = TrackNumber;
                vc.Comment = Comment;
                vc.Save(path);

                ID3v2Tag.RemoveTag(path);
                ID3v1Tag.RemoveTag(path);
            }
            else
            {
                ID3v2Tag id3v2 = new ID3v2Tag(path);
                id3v2.Title = Title;
                id3v2.Artist = Artist;
                id3v2.Album = Album;
                id3v2.Year = Year;
                id3v2.Genre = Genre;
                id3v2.TrackNumber = TrackNumber;

                if (id3v2.CommentsList.Count > 0)
                {
                    id3v2.CommentsList[0].Description = Comment;
                }
                else
                {
                    if (!string.IsNullOrEmpty(Comment))
                        id3v2.CommentsList.AddNew().Description = Comment;
                }

                ID3v1Tag id3v1 = new ID3v1Tag(path);
                id3v1.Title = Title;
                id3v1.Artist = Artist;
                id3v1.Album = Album;
                id3v1.Year = Year;
                id3v1.GenreIndex = GenreHelper.GetGenreIndex(Genre);
                id3v1.Comment = Comment;
                int trackNumber;
                if (int.TryParse(TrackNumber, out trackNumber))
                {
                    id3v1.TrackNumber = trackNumber;
                }
                else
                {
                    // Handle ##/## format
                    if (TrackNumber.Contains("/"))
                    {
                        if (int.TryParse(TrackNumber.Split('/')[0], out trackNumber))
                            id3v1.TrackNumber = trackNumber;
                        else
                            id3v1.TrackNumber = 0;
                    }
                    else
                    {
                        id3v1.TrackNumber = 0;
                    }
                }

                id3v2.Save(path);
                id3v1.Save(path);
            }

            Read(path);
        }
        /// <summary>
        ///     Sets the track album cover.
        /// </summary>
        /// <param name="track">The track.</param>
        /// <param name="image">The image.</param>
        public static void SetTrackAlbumCover(Track track, Image image)
        {
            if (track == null) return;
            if (image == null) return;
            if (!ID3v2Tag.DoesTagExist(track.Filename)) return;

            var tags = new ID3v2Tag(track.Filename);
            if (tags.PictureList.Count > 0) tags.PictureList.Clear();

            var picture = tags.PictureList.AddNew();

            if (picture != null)
            {
                picture.PictureType = PictureType.CoverFront;
                picture.MimeType = "image/jpeg";

                using (var stream = new MemoryStream())
                {
                    ImageHelper.SaveJpg(stream, image);
                    picture.PictureData = stream.ToArray();
                }
            }
            tags.Save(track.Filename);
        }
Example #6
0
        public static Boolean Savev2tag(Track mytrack)
        {
            if (!File.Exists(mytrack.Filename))
            {
                MessageBox.Show("File Does not exist " + mytrack.Filename);
                return false;
            }
            var trackxml = new XMLutils(appPath + "\\trackxml.xml");
            removeID3v2(mytrack.Filename);
            var id3 = new ID3v2Tag(mytrack.Filename);
            if (!String.IsNullOrEmpty(mytrack.Filename) && File.Exists(mytrack.Filename))
            {
                id3.Album = mytrack.Album;
                id3.Artist = mytrack.Artist;
                id3.Title = mytrack.Title;
                id3.TrackNumber = mytrack.Trackno;
                id3.Year = mytrack.Year;
                id3.Genre = mytrack.Genre;

                if (mytrack.coverimage !=null)
                {
                    id3.PictureList.Clear();
                    IAttachedPicture picture = id3.PictureList.AddNew();

                    picture.PictureData = ConvertBitMapToByteArray(mytrack.coverimage);
                    picture.PictureType = PictureType.CoverFront;

                }
                id3.Save(mytrack.Filename);
                trackxml.ModifyRecord(mytrack);
            }
            if (ID3v2Tag.DoesTagExist(mytrack.Filename))
            {
                trackxml.Updaterecord(mytrack.Filename);
                return (true);
            }
            else return (false);
        }