Lollipop

Welcome to PcCare.com

Mp3 Changing Name Artist Album

 

//

// .net c# code to Change Mp3 Name Artist Album Comment and Year

//

// Some code from http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/ee65171a-a4e5-4d9a-b2be-337c8f2adeac/

//

using System;

 

using System.Collections.Generic;

 

using System.Linq;

 

using System.Text;

 

using System;

 

using System.IO;

 

using System.Text;

 

 

namespace mp3info

{

 

/// <summary>

/// Reads/writes Id3v1 tags. lots of help from Paul Lockwood's code

 

/// http://www.csharphelp.com/archives/archive226.html

 

/// </summary>

 

public class ID3v1

 

{

 

public TagValues ValuesToWrite = new TagValues();

public TagValues CurrentValues = new TagValues();

public bool hasTag;

public bool ReadTags = true;

public string filename;

 

static int Main(string[] args)

{

int ReturnStatus = 0;

int index = 0;

ID3v1 mp3File = new ID3v1();

string text;

string ArgName;

while (index < args.Length)

{

text = args[index].ToUpper();

ArgName = "/FILENAME:";

ArgName = ArgName.ToUpper();

if (text.StartsWith(ArgName))

{

mp3File.filename = args[index].Substring(ArgName.Length);

}

ArgName = "/TITLE:";

ArgName = ArgName.ToUpper();

if (text.StartsWith(ArgName))

{

mp3File.ValuesToWrite.Title = args[index].Substring(ArgName.Length);

}

ArgName = "/ARTIST:";

ArgName = ArgName.ToUpper();

if (text.StartsWith(ArgName))

{

mp3File.ValuesToWrite.Artist = args[index].Substring(ArgName.Length);

}

ArgName = "/Album:";

ArgName = ArgName.ToUpper();

if (text.StartsWith(ArgName))

{

mp3File.ValuesToWrite.Album = args[index].Substring(ArgName.Length);

}

ArgName = "/Year:";

ArgName = ArgName.ToUpper();

if (text.StartsWith(ArgName))

{

mp3File.ValuesToWrite.Year = args[index].Substring(ArgName.Length);

}

ArgName = "/Comment:";

ArgName = ArgName.ToUpper();

if (text.StartsWith(ArgName))

{

mp3File.ValuesToWrite.Comment = args[index].Substring(ArgName.Length);

}

ArgName = "/Write:";

ArgName = ArgName.ToUpper();

if (text.StartsWith(ArgName))

{

mp3File.ReadTags = (!(Convert.ToBoolean(args[index].Substring(ArgName.Length))));

}

index = index + 1;

}

if (mp3File.filename.Length == 0 || mp3File.filename.Length == 0)

{

Console.Error.WriteLine("\n\nMp3TagWrite invalid command line argument. usage: Mp3TagWrite /filename:'c:\\temp\\Dark Side Of the Moon.Mp3' /Write:True /Artist:'Pink Floyd' [Title,Album,Year,Comment]");

Console.Error.WriteLine("/Write:True writes tags to file, /Write:False read tags from file");

return (1);

}

try

 

{

// read current values from mp3 file

 

mp3File.Read();

if (!mp3File.ReadTags)

{

// read current values

 

mp3File.updateMP3Tag();

// Read to print new values

 

mp3File.Read();

}

 

Console.WriteLine("\n\nFilename: {0}", mp3File.filename);

Console.WriteLine("Title: {0}", mp3File.CurrentValues.Title);

Console.WriteLine("Artist: {0}", mp3File.CurrentValues.Artist);

Console.WriteLine("Album: {0}", mp3File.CurrentValues.Album);

Console.WriteLine("Year: {0}", mp3File.CurrentValues.Year);

Console.WriteLine("Comment: {0}", mp3File.CurrentValues.Comment);

return(ReturnStatus);

}

catch (Exception e)

{

Console.Error.WriteLine("\n\n{0}",e.ToString());

ReturnStatus = 1;

}

 

return (ReturnStatus);

}

 

private void Initialize_Components()

{

hasTag = false;

 

}

 

 

 

 

public ID3v1()

{

Initialize_Components();

}

 

 

public ID3v1( string filename )

{

Initialize_Components();

this.filename = filename;

}

 

 

 

public void Read ()

{

// Read the 128 byte ID3 tag into a byte array

 

FileStream oFileStream;

oFileStream = new FileStream( this.filename, FileMode.Open);

byte[] bBuffer = new byte[128];

oFileStream.Seek(-128, SeekOrigin.End);

oFileStream.Read(bBuffer,0, 128);

oFileStream.Close();

 

// Convert the Byte Array to a String

 

Encoding instEncoding = new ASCIIEncoding(); // NB: Encoding is an Abstract class

 

string id3Tag = instEncoding.GetString(bBuffer);

 

// If there is an attched ID3 v1.x TAG then read it

 

if (id3Tag .Substring(0,3) == "TAG")

{

this.CurrentValues.Title = id3Tag.Substring(3, 30).Trim();

this.CurrentValues.Artist = id3Tag.Substring(33, 30).Trim();

this.CurrentValues.Album = id3Tag.Substring(63, 30).Trim();

this.CurrentValues.Year = id3Tag.Substring(93, 4).Trim();

this.CurrentValues.Comment = id3Tag.Substring(97, 28).Trim();

 

// Get the track number if TAG conforms to ID3 v1.1

 

if (id3Tag[125]!=0)

//this.CurrentValues.Track = bBuffer[126];

 

//else

 

//this.CurrentValues.Track = 0;

 

//this.CurrentValues.GenreID = bBuffer[127];

 

this.hasTag = true;

// ********* IF USED IN ANGER: ENSURE to test for non-numeric year

 

}

else

{

this.hasTag = false;

}

}

 

public void updateMP3Tag ()

{

if (this.ValuesToWrite.Title.Length == 0) this.ValuesToWrite.Title = this.CurrentValues.Title;

if (this.ValuesToWrite.Artist.Length == 0) this.ValuesToWrite.Artist = this.CurrentValues.Artist;

if (this.ValuesToWrite.Album.Length == 0) this.ValuesToWrite.Album = this.CurrentValues.Album;

if (this.ValuesToWrite.Year.Length == 0) this.ValuesToWrite.Year = this.CurrentValues.Year;

if (this.ValuesToWrite.Comment.Length == 0) this.ValuesToWrite.Comment = this.CurrentValues.Comment;

// Trim any whitespace

 

this.ValuesToWrite.Title = this.ValuesToWrite.Title.Trim();

this.ValuesToWrite.Artist = this.ValuesToWrite.Artist.Trim();

this.ValuesToWrite.Album = this.ValuesToWrite.Album.Trim();

this.ValuesToWrite.Year = this.ValuesToWrite.Year.Trim();

this.ValuesToWrite.Comment = this.ValuesToWrite.Comment.Trim();

 

// Ensure all properties are correct size

 

if (this.ValuesToWrite.Title.Length > 30) this.ValuesToWrite.Title = this.ValuesToWrite.Title.Substring(0,30);

if (this.ValuesToWrite.Artist.Length > 30) this.ValuesToWrite.Artist = this.ValuesToWrite.Artist.Substring(0,30);

if (this.ValuesToWrite.Album.Length > 30) this.ValuesToWrite.Album = this.ValuesToWrite.Album.Substring(0,30);

if (this.ValuesToWrite.Year.Length > 4) this.ValuesToWrite.Year = this.ValuesToWrite.Year.Substring(0,4);

if (this.ValuesToWrite.Comment.Length > 28) this.ValuesToWrite.Comment = this.ValuesToWrite.Comment.Substring(0,28);

 

// Build a new ID3 Tag (128 Bytes)

 

byte[] tagByteArray = new byte[128];

for ( int i = 0; i < tagByteArray.Length; i++ ) tagByteArray[i] = 0; // Initialise array to nulls

 

 

// Convert the Byte Array to a String

 

Encoding instEncoding = new ASCIIEncoding(); // NB: Encoding is an Abstract class // ************ To DO: Make a shared instance of ASCIIEncoding so we don't keep creating/destroying it

 

// Copy "TAG" to Array

 

byte[] workingByteArray = instEncoding.GetBytes("TAG");

Array.Copy(workingByteArray, 0, tagByteArray, 0, workingByteArray.Length);

// Copy Title to Array

 

workingByteArray = instEncoding.GetBytes(this.ValuesToWrite.Title);

Array.Copy(workingByteArray, 0, tagByteArray, 3, workingByteArray.Length);

// Copy Artist to Array

 

workingByteArray = instEncoding.GetBytes(this.ValuesToWrite.Artist);

Array.Copy(workingByteArray, 0, tagByteArray, 33, workingByteArray.Length);

// Copy Album to Array

 

workingByteArray = instEncoding.GetBytes(this.ValuesToWrite.Album);

Array.Copy(workingByteArray, 0, tagByteArray, 63, workingByteArray.Length);

// Copy Year to Array

 

workingByteArray = instEncoding.GetBytes(this.ValuesToWrite.Year);

Array.Copy(workingByteArray, 0, tagByteArray, 93, workingByteArray.Length);

// Copy Comment to Array

 

workingByteArray = instEncoding.GetBytes(this.ValuesToWrite.Comment);

Array.Copy(workingByteArray, 0, tagByteArray, 97, workingByteArray.Length);

// Copy Track and Genre to Array

 

//tagByteArray[126] = System.Convert.ToByte(this.ValuesToWrite.Track);

 

//tagByteArray[127] = System.Convert.ToByte(this.ValuesToWrite.GenreID);

 

 

// SAVE TO DISK: Replace the final 128 Bytes with our new ID3 tag

 

FileStream oFileStream = new FileStream(this.filename , FileMode.Open);

if (this.hasTag)

oFileStream.Seek(-128, SeekOrigin.End);

else

 

oFileStream.Seek(0, SeekOrigin.End);

oFileStream.Write(tagByteArray,0, 128);

oFileStream.Close();

this.hasTag = true;

}

 

}

 

public class TagValues

 

{

 

public TagValues()

{

 

Title = "";

Artist = "";

Album = "";

Year = "";

Comment = "";

 

}

public string filename;

public string Title;

public string Artist;

public string Album;

public string Year;

public string Comment;

 

}

 

 

}

 

 

 

 

DISCLAIMER: It is assumed that users are familiar with the operating system they are using and comfortable with making the suggested changes. PcCare.com will not be held responsible if changes you make cause a system failure.

Please review our Terms of Service and Privacy statement before initiating service or using this site. Microsoft® and the Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. PcCare Site Map. About Us