added filename parsing to get music meta if missing from acutal file

This commit is contained in:
reven
2022-02-01 23:31:44 +13:00
parent 20237748e0
commit 5340354337
2 changed files with 70 additions and 2 deletions
+51 -1
View File
@@ -175,7 +175,6 @@ namespace FileFlows.MusicNodes
if (line.IndexOf(" stereo,") > 0)
mi.Channels = 2;
}
}
@@ -185,6 +184,12 @@ namespace FileFlows.MusicNodes
Logger.ELog(ex.Message, ex.StackTrace.ToString());
}
if (string.IsNullOrEmpty(mi.Artist) && string.IsNullOrEmpty(mi.Album) && mi.Track < 1)
{
// try parse the file
ParseFileNameInfo(filename, mi);
}
return mi;
}
@@ -215,5 +220,50 @@ namespace FileFlows.MusicNodes
return info;
}
public void ParseFileNameInfo(string filename, MusicInfo mi)
{
try
{
var fileInfo = new FileInfo(filename);
var cdMatch = Regex.Match(filename.Replace("\\", "/"), @"(?<=(/(cd|disc)))[\s]*([\d]+)(?!=(/))", RegexOptions.IgnoreCase);
if (cdMatch.Success && int.TryParse(cdMatch.Value.Trim(), out int disc))
mi.Disc = disc;
var trackMatch = Regex.Match(fileInfo.Name, @"[\-_\s\.]+([\d]{1,2})[\-_\s\.]+");
if (trackMatch.Success)
{
string trackString = trackMatch.Value;
if(int.TryParse(Regex.Match(trackString, @"[\d]+").Value, out int track))
mi.Track = track;
}
string album = fileInfo.Directory.Name;
var yearMatch = Regex.Match(album, @"(?<=(\())[\d]{4}(?!=\))");
if (yearMatch.Success)
{
album = album.Replace("(" + yearMatch.Value + ")", "").Trim();
if (int.TryParse(yearMatch.Value, out int year))
mi.Date = new DateTime(year, 1, 1);
}
mi.Album = album;
mi.Artist = fileInfo.Directory.Parent.Name;
// the title
int titleIndex = fileInfo.Name.LastIndexOf(" - ");
if(titleIndex > 0)
{
mi.Title = fileInfo.Name.Substring(titleIndex + 3);
if(string.IsNullOrEmpty(fileInfo.Extension) == false)
mi.Title = mi.Title.Replace(fileInfo.Extension, "");
}
}
catch (Exception ex)
{
Logger?.WLog("Failed parsing music info from filename: " + ex.Message + Environment.NewLine + ex.StackTrace);
}
}
}
}
+19 -1
View File
@@ -64,10 +64,28 @@ namespace FileFlows.MusicNodes.Tests
string folder = args.ReplaceVariables("{mi.ArtistThe} ({mi.Year})");
Assert.AreEqual($"{mi.Artist} ({mi.Date.Year})", folder);
string fname = args.ReplaceVariables("{mi.Artist} - {mi.Track:##} - {mi.Title}");
string fname = args.ReplaceVariables("{mi.Artist} - {mi.Album} - {mi.Track:##} - {mi.Title}");
Assert.AreEqual($"{mi.Artist} - {mi.Track.ToString("00")} - {mi.Title}", fname);
}
}
[TestMethod]
public void MusicInfo_FileNameMetadata()
{
const string ffmpegExe = @"C:\utils\ffmpeg\ffmpeg.exe";
var logger = new TestLogger();
string file = @"\\jor-el\music\Meat Loaf\Bat out of Hell II- Back Into Hell… (1993)\Meat Loaf - Bat out of Hell II- Back Into Hell… - 03 - Id Do Anything for Love (but I Wont Do That).flac";
var mi = new MusicInfo();
new MusicInfoHelper(ffmpegExe, logger).ParseFileNameInfo(file, mi);
Assert.AreEqual("Meat Loaf", mi.Artist);
Assert.AreEqual("Bat out of Hell II- Back Into Hell…", mi.Album);
Assert.AreEqual(1993, mi.Date.Year);
Assert.AreEqual("Id Do Anything for Love (but I Wont Do That)", mi.Title);
Assert.AreEqual(3, mi.Track);
}
}
}