Exemplo n.º 1
0
        public void ApplyMetadata(FileInfo mediaFile, SongProperties properties)
        {
            var formatter = new TitleFormatters.TitleFormatter(properties);

            var trackData = formatter.GetProperties();

            TagLib.Id3v2.Tag.DefaultVersion = 3;
            TagLib.Id3v2.Tag.ForceDefaultVersion = true;

            using (TagLib.File file = new AudioFile(mediaFile.FullName))
            {
                var albumCover = new AttachedPictureFrame(trackData.Picture)
                    {
                        Type = PictureType.FrontCover,
                    };

                file.Tag.Pictures = new IPicture[1]{albumCover};
                file.Tag.Album = trackData.AlbumTitle;
                file.Tag.Title = trackData.Title;
                file.Tag.Year = trackData.Year;
                file.Tag.AlbumArtists = trackData.Artists;
                file.Tag.Comment = trackData.Comment;
                file.Tag.Genres = new[] {"Pop, Gospel, Motown"};
                file.Save();
            }
        }
Exemplo n.º 2
0
        public static AttachedPictureFrame Get(Tag tag, string description, PictureType type, bool create)
        {
            AttachedPictureFrame apic;

            foreach (Frame frame in tag.GetFrames(FrameType.APIC))
            {
                apic = frame as AttachedPictureFrame;
                if (apic == null)
                {
                    continue;
                }
                if (description != null && apic.Description != description)
                {
                    continue;
                }
                if (type != PictureType.Other && apic.Type != type)
                {
                    continue;
                }
                return(apic);
            }
            if (!create)
            {
                return(null);
            }
            apic             = new AttachedPictureFrame();
            apic.Description = description;
            apic.Type        = type;
            tag.AddFrame(apic);
            return(apic);
        }
Exemplo n.º 3
0
        public static void addPictureNoSave(TagLib.File targetMp3File, byte[] imageData)
        {
            if (imageData == null)
            {
                return;
            }

            if (imageData.Length == 0)
            {
                return;
            }

            // define picture
            TagLib.Id3v2.AttachedPictureFrame pic = new TagLib.Id3v2.AttachedPictureFrame();
            pic.TextEncoding = TagLib.StringType.Latin1;
            pic.MimeType     = System.Net.Mime.MediaTypeNames.Image.Jpeg;
            pic.Type         = TagLib.PictureType.FrontCover;
            pic.Data         = new TagLib.ByteVector(imageData, imageData.Length);


            // save picture to file
            targetMp3File.Tag.Pictures = new TagLib.IPicture[1] {
                pic
            };
        }
Exemplo n.º 4
0
        private void mi_addcover_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
            ofd.Title  = "Bilddatei laden...";
            ofd.Filter = "Bild-Dateien|*.jpg; *.png";
            ofd.ShowDialog();

            if (File.Exists(ofd.FileName))
            {
                new Thread(() =>
                {
                    try
                    {
                        TagLib.Id3v2.AttachedPictureFrame pic = new TagLib.Id3v2.AttachedPictureFrame();
                        pic.TextEncoding = TagLib.StringType.Latin1;
                        pic.MimeType     = System.Net.Mime.MediaTypeNames.Image.Jpeg;
                        pic.Type         = TagLib.PictureType.FrontCover;
                        pic.Data         = TagLib.ByteVector.FromPath(ofd.FileName);

                        TagFile.Tag.Pictures = new TagLib.IPicture[1] {
                            pic
                        };
                        TagFile.Save();

                        Dispatcher.Invoke(() =>
                        {
                            i_cover.Source = new BitmapImage(new Uri(ofd.FileName));
                        });
                    }
                    catch
                    {
                        MessageBox.Show("Es ist ein Fehler beim speichern aufgetreten. Ist die Datei in Verwendung?", "Speicherfehler!", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }).Start();
            }
            else
            {
                if (ofd.FileName == "")
                {
                    return;
                }
                MessageBox.Show("Die Datei \"" + ofd.FileName + "\" kann nicht geöffnet werden, da sie nicht existiert!", "Fehler!", MessageBoxButton.OK, MessageBoxImage.Error);
                IsEnabled = true;
            }
        }
Exemplo n.º 5
0
        /// <summary>
        ///    Creates a deep copy of the current instance.
        /// </summary>
        /// <returns>
        ///    A new <see cref="Frame" /> object identical to the
        ///    current instance.
        /// </returns>
        public override Frame Clone()
        {
            AttachedPictureFrame frame = new AttachedPictureFrame();

            frame.text_encoding = text_encoding;
            frame.mime_type     = mime_type;
            frame.type          = type;
            frame.description   = description;
            if (data != null)
            {
                frame.data = new ByteVector(data);
            }
            if (raw_data != null)
            {
                frame.data = new ByteVector(raw_data);
            }
            frame.raw_version = raw_version;
            return(frame);
        }
 public override Frame Clone()
 {
     AttachedPictureFrame frame = new AttachedPictureFrame {
         text_encoding = this.text_encoding,
         mime_type = this.mime_type,
         type = this.type,
         description = this.description
     };
     if (this.data != null)
     {
         frame.data = new ByteVector(this.data);
     }
     if (this.raw_data != null)
     {
         frame.data = new ByteVector(this.raw_data);
     }
     frame.raw_version = this.raw_version;
     return frame;
 }
 /// <summary>
 ///    Creates a deep copy of the current instance.
 /// </summary>
 /// <returns>
 ///    A new <see cref="Frame" /> object identical to the
 ///    current instance.
 /// </returns>
 public override Frame Clone()
 {
     AttachedPictureFrame frame = new AttachedPictureFrame ();
     frame.text_encoding = text_encoding;
     frame.mime_type = mime_type;
     frame.type = type;
     frame.description = description;
     if (data != null)
         frame.data = new ByteVector (data);
     if (raw_data != null)
         frame.data = new ByteVector (raw_data);
     frame.raw_version = raw_version;
     return frame;
 }
        /// <summary>
        ///    Gets a specified picture frame from the specified tag,
        ///    optionally creating it if it does not exist.
        /// </summary>
        /// <param name="tag">
        ///    A <see cref="Tag" /> object to search in.
        /// </param>
        /// <param name="description">
        ///    A <see cref="string" /> specifying the description to
        ///    match.
        /// </param>
        /// <param name="type">
        ///    A <see cref="PictureType" /> specifying the picture type
        ///    to match.
        /// </param>
        /// <param name="create">
        ///    A <see cref="bool" /> specifying whether or not to create
        ///    and add a new frame to the tag if a match is not found.
        /// </param>
        /// <returns>
        ///    A <see cref="AttachedPictureFrame" /> object containing
        ///    the matching frame, or <see langword="null" /> if a match
        ///    wasn't found and <paramref name="create" /> is <see
        ///    langword="false" />.
        /// </returns>
        /// <example>
        ///    <para>Sets a cover image with a description. Because <see
        ///    cref="Get(Tag,string,PictureType,bool)" /> is used, if
        ///    the program is called again with the same audio file and
        ///    desciption, the picture will be overwritten with the new
        ///    one.</para>
        ///    <code lang="C#">
        /// using TagLib;
        /// using TagLib.Id3v2;
        ///
        /// public static class SetId3v2Cover
        /// {
        /// 	public static void Main (string [] args)
        /// 	{
        /// 		if (args.Length != 3)
        /// 			throw new ApplicationException (
        /// 				"USAGE: SetId3v2Cover.exe AUDIO_FILE PICTURE_FILE DESCRIPTION");
        ///
        /// 		// Create the file. Can throw file to TagLib# exceptions.
        /// 		File file = File.Create (args [0]);
        ///
        /// 		// Get or create the ID3v2 tag.
        /// 		TagLib.Id3v2.Tag tag = file.GetTag (TagTypes.Id3v2, true) as TagLib.Id3v2.Tag;
        /// 		if (tag == null)
        /// 			throw new ApplicationException ("File does not support ID3v2 tags.");
        ///
        /// 		// Create a picture. Can throw file related exceptions.
        ///		TagLib.Picture picture = TagLib.Picture.CreateFromPath (args [1]);
        ///
        /// 		// Get or create the picture frame.
        /// 		AttachedPictureFrame frame = AttachedPictureFrame.Get (
        /// 			tag, args [2], PictureType.FrontCover, true);
        ///
        /// 		// Set the data from the picture.
        /// 		frame.MimeType = picture.MimeType;
        /// 		frame.Data     = picture.data;
        /// 		
        /// 		// Save the file.
        /// 		file.Save ();
        /// 	}
        /// }
        ///    </code>
        /// </example>
        public static AttachedPictureFrame Get(Tag tag,
            string description,
            PictureType type,
            bool create)
        {
            AttachedPictureFrame apic;
            foreach (Frame frame in tag.GetFrames (FrameType.APIC)) {
                apic = frame as AttachedPictureFrame;

                if (apic == null)
                    continue;

                if (description != null && apic.Description != description)
                    continue;

                if (type != PictureType.Other && apic.Type != type)
                    continue;

                return apic;
            }

            if (!create)
                return null;

            apic = new AttachedPictureFrame ();
            apic.Description = description;
            apic.Type = type;

            tag.AddFrame (apic);

            return apic;
        }
Exemplo n.º 9
0
		public void TestAttachedPictureFrame ()
		{
			AttachedPictureFrame frame = new AttachedPictureFrame ();
			
			string mime = "image/png";
			string desc = "description";
			PictureType type = PictureType.FrontCover;
			ByteVector data = val_sing;
			data.Add (data); data.Add (data); data.Add (data);
			data.Add (data); data.Add (data); data.Add (data);
			data.Add (data); data.Add (data); data.Add (data);
			data.Add (data); data.Add (data); data.Add (data);
			// data.Add (data); data.Add (data); data.Add (data);
			
			frame.MimeType = mime;
			frame.Description = desc;
			frame.Type = type;
			frame.Data = data;
			
			FrameTest (frame, 2,
				delegate (Frame f, StringType e) {
					(f as AttachedPictureFrame).TextEncoding = e;
				},
				
				delegate (ByteVector d, byte v) {
					return new AttachedPictureFrame (d, v);
				},
				
				delegate (Frame f, string m) {
					AttachedPictureFrame g = (f as AttachedPictureFrame);
					Assert.AreEqual (mime, g.MimeType, m);
					Assert.AreEqual (desc, g.Description, m);
					Assert.AreEqual (data, g.Data, m);
					Assert.AreEqual (type, g.Type, m);
				});
		}
 public static AttachedPictureFrame Get(TagLib.Id3v2.Tag tag, string description, PictureType type, bool create)
 {
     AttachedPictureFrame frame;
     IEnumerator<Frame> enumerator = tag.GetFrames(FrameType.APIC).GetEnumerator();
     try
     {
         while (enumerator.MoveNext())
         {
             Frame current = enumerator.Current;
             frame = current as AttachedPictureFrame;
             if (((frame != null) && ((description == null) || (frame.Description == description))) && ((type == PictureType.Other) || (frame.Type == type)))
             {
                 return frame;
             }
         }
     }
     finally
     {
         if (enumerator == null)
         {
         }
         enumerator.Dispose();
     }
     if (!create)
     {
         return null;
     }
     frame = new AttachedPictureFrame {
         Description = description,
         Type = type
     };
     tag.AddFrame(frame);
     return frame;
 }