I recently had the need to organize a number of audio files of type mp3. This generated a small but successful research about how these files store information about the audio they contain. This post is about the results of my tests and some test code to show how to handle this type of information in our favorite language C #.
My requirement was to manage as I said before audio files in mp3 format, the Pirma step of my search took me to the source par excellence, ID3 , this is one of the most popular (almost a de facto standard) labeling format audio files. This format is supported and used by applications like iTunes, Windows Media Player and WinAmp and players like iPod, Creative Zen, Sony Walkman, among others.
The current version of this standard is the ID3V2.4.0 that inherits from the ID3V1, this format allows us to store information rich (as it allows images) on mp3 files such as album name, author's name, gender and many other very useful when adding information to an audio file. Here we can see how you save information in an audio file.
However, this is great theory, I invite anyone who wants to read all the specification of the standard and then make an implementation of it. NET, but there are already implementations and I'm going to refer to which I have personally used and tested, I mean Taglib # (TagLib Sharp), will leave the link here to download . This is a library originally written in C + + and has migrated to C #, if you want you can download the source code and see how this done and modify it as it is free, its use is very simple and makes easy the task of dealing with information in audio files (not only mp3, but it also supports many other formats). Although after to use this library I discovered that there were others, have not tried the others.
Ok, no more words and leave them here code example of using this fantastic library.
Tag
First read the very simple and completely intuitive (Single Tag) My requirement was to manage as I said before audio files in mp3 format, the Pirma step of my search took me to the source par excellence, ID3 , this is one of the most popular (almost a de facto standard) labeling format audio files. This format is supported and used by applications like iTunes, Windows Media Player and WinAmp and players like iPod, Creative Zen, Sony Walkman, among others.
The current version of this standard is the ID3V2.4.0 that inherits from the ID3V1, this format allows us to store information rich (as it allows images) on mp3 files such as album name, author's name, gender and many other very useful when adding information to an audio file. Here we can see how you save information in an audio file.
However, this is great theory, I invite anyone who wants to read all the specification of the standard and then make an implementation of it. NET, but there are already implementations and I'm going to refer to which I have personally used and tested, I mean Taglib # (TagLib Sharp), will leave the link here to download . This is a library originally written in C + + and has migrated to C #, if you want you can download the source code and see how this done and modify it as it is free, its use is very simple and makes easy the task of dealing with information in audio files (not only mp3, but it also supports many other formats). Although after to use this library I discovered that there were others, have not tried the others.
Ok, no more words and leave them here code example of using this fantastic library.
Tag
namespace TagLibSharpExample_Console
{
class Program
{
static void Main ( string [] args )
{
try
{
TagLib . File Mp3file = TagLib . File . Create ( @ "c: \\ prueba.mp3" )
console. WriteLine ( "Procesing File )
console. WriteLine ( File Name: {0} " , Mp3file . Name )
console. WriteLine ( "MIME Type: {0}" , Mp3file . Mimetype );
Console . WriteLine ( "Audio Bitrate: {0} [Kbps]" , Mp3file . Properties . AudioBitrate . ToString ());
Console . WriteLine ( "Chanels: {0}" , Mp3file . Properties . AudioChannels . ToString ());
Console . WriteLine ( "Sample Rate: {0} [Hz]" , Mp3file . Properties . AudioSampleRate . ToString ());
Console . WriteLine ( "Description: {0}" , Mp3file . Properties . Description );
Console . WriteLine ( "Duration: {0}" , Mp3file . Properties . Duration . ToString ());
Console . WriteLine ( "Song Title: {0}" , Mp3file . Tag . Title );
Console . WriteLine ( "Album: {0}" , Mp3file . Tag . Album )
/ / Artist is a collection of strings, so we can have more of an artist if we wanted
/ / work with this collection should be displayed with a foreach, or select a particular index .
Console. WriteLine ( "Artist: {0}" , mp3file . Tag . FirstAlbumArtist )
} catch ( Exception ex)
{
Console. WriteLine (ex . Message )
} finally {
/ / Wait for user entry
Console. ReadLine ();}
}}}
The result looks like this
With the following code we have access the ID3v2 tag, which means more flexibility in that you can archive information in this labeling format that includes the cover artwork for the albums (pictures embedded in the tag)
using System ;
using System . Collections . Generic ;
using System . Linq ;
using System . Text ;
using TagLib ;
namespace TagLibSharpExample_Console
{
class Program
{
static void Main ( string [] args )
{
try
{
TagLib . File Mp3file = TagLib . File . Create
( @"C:01-catwash--groovabilisme-siberia.mp3" );
TagLib . Tag Mp3ID3V2Tag = Mp3file . GetTag ( TagTypes . ID3v2 )
console. WriteLine ( "Procesing File )
console. WriteLine ( File Name: {0} " , Mp3file . Name )
console. WriteLine ( "MIME Type: {0}" , Mp3file . MimeType );
Console . WriteLine ( "Audio Bitrate: {0} [Kbps]" , Mp3file . Properties . AudioBitrate . ToString ());
Console . WriteLine ( "Chanels: {0}" , Mp3file . Properties . AudioChannels . ToString ());
Console . WriteLine ( "Sample Rate: {0} [Hz]" , Mp3file . Properties . AudioSampleRate . ToString ());
Console . WriteLine ( "Procesing ID3v2 Tag in {0}" , Mp3file . Name );
Console . WriteLine ( "Album {0}" , Mp3ID3V2Tag . Album)
Console. WriteLine ( "Artist Collection" )
foreach (string Artist in Mp3ID3V2Tag . AlbumArtists ) {
Console . WriteLine ( "Artist:" + Artist ); }
foreach ( string Genre in Mp3ID3V2Tag . Genres )
{ Console . WriteLine ( "Genre: " + Genre ); }
}
catch ( Exception ex )
{
Console . WriteLine (ex . Message )
} finally {
/ / Wait for user entry
Console. ReadLine ();}
}}}
The result can be seen here:
As we see the tag management audio files with this library is very intuitive and simple.
Links Interesting:
ID3.org
TagLib # (TagLibSharp)
I hope they serve. Happy Programming!
0 comments:
Post a Comment