C
C#7mo ago
BigBoyConst

✅ Editing audio file metadata with TagLib Sharp

Hey there! I'm trying to make a program that streamlines the process of editing file metadata for my music while also giving me a chance to learn something, and I'm running into this problem when I want to set the artist of the track: The way it's stored in TagLib is using a property TagLib.File.Tag.Performers, which is an array of strings. It's initialized to be of length zero, which makes sense, but when I try to set it to a new nonempty array of strings, it just doesn't work, which then leads me to getting an IndexOutOfRangeException when I inevitably want to set the first value in that array. I may be making a very very stupid mistake but nonetheless I thought i'd ask. I don't want to post my code unless someone asks because it's actual programming gore and I know it. Thanks in advance!
8 Replies
canton7
canton77mo ago
BigBoyConst
BigBoyConst7mo ago
that's what I'm doing
canton7
canton77mo ago
Can you share your code?
BigBoyConst
BigBoyConst7mo ago
alright, it's horrible tho haha
Console.Write("Input the path of the file you want to edit: ");

string path = Console.ReadLine() ?? throw new Exception();
path = path.Substring(1, path.Length - 2);
Console.WriteLine("Path: {0}", path);

Console.WriteLine("Current metadata: ");
Console.WriteLine(new string('-', 30));
GetMetadata(path);
Console.WriteLine(new string('-', 30));

ChangeMetadata(path);

Console.WriteLine("New metadata: ");
Console.WriteLine(new string('-', 30));
GetMetadata(path);
Console.WriteLine(new string('-', 30));

static void GetMetadata(string path)
{
var tFile = TagLib.File.Create(path);
string title, artist, album, genre;
uint trackNumber, year;
title = tFile.Tag.Title;
artist = tFile.Tag.FirstPerformer;
album = tFile.Tag.Album;
trackNumber = tFile.Tag.Track;
genre = tFile.Tag.FirstGenre;
year = tFile.Tag.Year;

foreach (var item in new string[] { title, artist, album, genre, trackNumber.ToString(), year.ToString() })
{
Console.WriteLine(string.IsNullOrEmpty(item) ? "none" : item);
}
}
// Change Artist, Album, Track no., Genre, Title, Year
static void ChangeMetadata(string path)
{
var tFile = TagLib.File.Create(path);

string[] uintMessages = { "Insert track no.: ", "Insert year: " };

Console.Write("Insert song title: ");
tFile.Tag.Title = Console.ReadLine();

Console.Write("Insert artist name: ");
tFile.Tag.Performers = new string[1];
tFile.Tag.Performers[0] = Console.ReadLine();

Console.Write("Insert album name: ");
tFile.Tag.Album = Console.ReadLine();

Console.Write("Insert genre: ");
tFile.Tag.Genres = new string[1];
tFile.Tag.Genres[0] = Console.ReadLine();

tFile.Tag.Track = ReadUInt(uintMessages[0]);

tFile.Tag.Year = ReadUInt(uintMessages[1]);

tFile.Save();
}

static uint ReadUInt(string message)
{
uint val;
do
Console.Write(message);
while (!uint.TryParse(Console.ReadLine(), out val));
return val;
}
Console.Write("Input the path of the file you want to edit: ");

string path = Console.ReadLine() ?? throw new Exception();
path = path.Substring(1, path.Length - 2);
Console.WriteLine("Path: {0}", path);

Console.WriteLine("Current metadata: ");
Console.WriteLine(new string('-', 30));
GetMetadata(path);
Console.WriteLine(new string('-', 30));

ChangeMetadata(path);

Console.WriteLine("New metadata: ");
Console.WriteLine(new string('-', 30));
GetMetadata(path);
Console.WriteLine(new string('-', 30));

static void GetMetadata(string path)
{
var tFile = TagLib.File.Create(path);
string title, artist, album, genre;
uint trackNumber, year;
title = tFile.Tag.Title;
artist = tFile.Tag.FirstPerformer;
album = tFile.Tag.Album;
trackNumber = tFile.Tag.Track;
genre = tFile.Tag.FirstGenre;
year = tFile.Tag.Year;

foreach (var item in new string[] { title, artist, album, genre, trackNumber.ToString(), year.ToString() })
{
Console.WriteLine(string.IsNullOrEmpty(item) ? "none" : item);
}
}
// Change Artist, Album, Track no., Genre, Title, Year
static void ChangeMetadata(string path)
{
var tFile = TagLib.File.Create(path);

string[] uintMessages = { "Insert track no.: ", "Insert year: " };

Console.Write("Insert song title: ");
tFile.Tag.Title = Console.ReadLine();

Console.Write("Insert artist name: ");
tFile.Tag.Performers = new string[1];
tFile.Tag.Performers[0] = Console.ReadLine();

Console.Write("Insert album name: ");
tFile.Tag.Album = Console.ReadLine();

Console.Write("Insert genre: ");
tFile.Tag.Genres = new string[1];
tFile.Tag.Genres[0] = Console.ReadLine();

tFile.Tag.Track = ReadUInt(uintMessages[0]);

tFile.Tag.Year = ReadUInt(uintMessages[1]);

tFile.Save();
}

static uint ReadUInt(string message)
{
uint val;
do
Console.Write(message);
while (!uint.TryParse(Console.ReadLine(), out val));
return val;
}
canton7
canton77mo ago
Which line throws the exception? Ah, I see
BigBoyConst
BigBoyConst7mo ago
well it throws at line 50 but the error is actually at line 48
canton7
canton77mo ago
Mutating the performers array isn't going to work: look at the link I posted. I just does string.Join to turn the array back into a ; - separated string And when it reads it back into an array from the string, it's going to give you an empty array So do .Peformers = new[] { Console.ReadLine() }, or:
var performers = new string[1];
performers[0] = Console.ReadLine();
tFile.Tag.Performers = peformers;
var performers = new string[1];
performers[0] = Console.ReadLine();
tFile.Tag.Performers = peformers;
If you really want to be verbose
BigBoyConst
BigBoyConst7mo ago
ah i see that makes sense well thanks a lot :D
Want results from more Discord servers?
Add your server