Skip to content

Instantly share code, notes, and snippets.

@Manuel-S
Created January 16, 2021 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Manuel-S/186853629fc08259b9a626c80877c5f2 to your computer and use it in GitHub Desktop.
Save Manuel-S/186853629fc08259b9a626c80877c5f2 to your computer and use it in GitHub Desktop.
WIP: Read the windows-tags from audio files using TagLib#
static IEnumerable<string> getCategories(TagLib.File file)
{
var ubox = (List<TagLib.Mpeg4.IsoUserDataBox>)file.GetType().GetProperty("UdtaBoxes", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(file);
return getCategories(ubox);
}
static IEnumerable<string> getCategories(List<TagLib.Mpeg4.IsoUserDataBox> userDataBoxes)
{
foreach (var dbox in userDataBoxes)
foreach (var result in recursiveExplore(dbox))
yield return result;
}
static IEnumerable<string> recursiveExplore(TagLib.Mpeg4.Box box)
{
if (box.Data != null)
{
if (box.BoxType == (ReadOnlyByteVector)"Xtra")
{
var category = box.Data.ToString(TagLib.StringType.Latin1);
var items = category.Split(new[] { /*"\u0010"*/ "\0\b" }, StringSplitOptions.RemoveEmptyEntries).Skip(1).ToArray();
foreach (var item in items)
{
string local = item;
var end = item.IndexOf("\0\0\0\0\0\0");
if (end != -1)
{
local = local.Substring(0, end);
}
local = local.Replace("\u0010", "").Replace("\u001c", "").Replace("\0", "").Replace("\b", "");
if (local.StartsWith("("))
local = local.Substring(1);
if (local.EndsWith("("))
local = local.Substring(0, local.Length - 1);
if (!string.IsNullOrWhiteSpace(local))
yield return local.Trim();
}
}
}
if (box.HasChildren)
foreach (var child in box.Children)
foreach (var result in recursiveExplore(child))
yield return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment