2025-12-22 17:17:16 +01:00

134 lines
5.4 KiB
C#

using System.Diagnostics;
namespace azupack_flac
{
internal class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
{
MessageBox.Show("Hey, gimme something to work with!");
return;
}
if (args.Length == 1)
{
FileInfo fi = new FileInfo(args[0]);
string fileExtension = Path.GetExtension(fi.Name);
if (fileExtension.ToLower().Equals(".flac"))
{
TagLib.File file = TagLib.File.Create(fi.FullName);
TagLib.Ogg.XiphComment tag = (TagLib.Ogg.XiphComment)file.GetTag(TagLib.TagTypes.Xiph);
string[] strings = tag.ToArray();
string joined = String.Join("\n", strings);
MessageBox.Show(joined);
return;
}
else
{
MessageBox.Show("What am I supposed to do with a single FLAC file?");
return;
}
}
List<FileInfo> fileInfos = args.Select(x => new FileInfo(x)).ToList();
if (!fileInfos.Any(x => Path.GetExtension(x.Name).ToLower().Equals(".wav")))
{
MessageBox.Show("No wav file!");
return;
}
FileInfo wavFile = fileInfos.Find(x => Path.GetExtension(x.Name).ToLower().Equals(".wav"));
fileInfos.Remove(wavFile);
List<FileInfo> jpgFiles = fileInfos.Where(x => Path.GetExtension(x.FullName).ToLower().Equals(".jpg")).ToList();
jpgFiles.Sort((x, y) => x.Name.CompareTo(y.Name));
fileInfos.RemoveAll(x => jpgFiles.Contains(x));
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = "C:\\FT\\flac.exe";
processStartInfo.Arguments = String.Format("-8 \"{0}\"", wavFile.FullName);
processStartInfo.WorkingDirectory = wavFile.Directory.FullName;
Process? process = Process.Start(processStartInfo);
process.WaitForExit();
FileInfo flacFile = new FileInfo(Path.ChangeExtension(wavFile.FullName, ".flac"));
if (!flacFile.Exists)
{
MessageBox.Show("Failed to generate FLAC file.");
return;
}
TagLib.File taglibFile = TagLib.File.Create(flacFile.FullName);
TagLib.Ogg.XiphComment tag2 = (TagLib.Ogg.XiphComment)taglibFile.GetTag(TagLib.TagTypes.Xiph);
bool cueSet = false;
foreach (FileInfo fi in fileInfos)
{
string extension = Path.GetExtension(fi.FullName).ToLowerInvariant();
switch (extension)
{
case ".cue":
string v = File.ReadAllText(fi.FullName);
if (!cueSet)
{
tag2.SetField("CUESHEET", v);
tag2.SetField("AZUSA_CLEAN_CUE", v);
cueSet = true;
}
else
{
tag2.SetField("AZUSA_ALT_CUE", v);
}
break;
case ".md5":
string v1 = File.ReadAllText(fi.FullName);
tag2.SetField("AZUSA_MD5", v1);
break;
case ".ibg":
string v2 = File.ReadAllText(fi.FullName);
tag2.SetField("AZUSA_IBG", v2);
break;
case ".log":
string v3 = File.ReadAllText(fi.FullName);
tag2.SetField("AZUSA_LOG", v3);
break;
default:
MessageBox.Show(String.Format("Don't know what to do with a {0} file.", fi.FullName));
return;
}
}
TagLib.PictureType[] types = new TagLib.PictureType[] { TagLib.PictureType.FrontCover, TagLib.PictureType.BackCover, TagLib.PictureType.Artist, TagLib.PictureType.Media, TagLib.PictureType.FileIcon };
TagLib.Picture[] pictures = new TagLib.Picture[jpgFiles.Count];
for (int i = 0; i < jpgFiles.Count; i++)
{
TagLib.Picture picture = new TagLib.Picture(jpgFiles[i].FullName);
picture.MimeType = "image/jpeg";
picture.Type = types[i];
pictures[i] = picture;
}
taglibFile.Tag.Pictures = pictures;
tag2.SetField("AZUPACK", new AzupackMeta().ToString());
uint? cdNumber = GetCdNumber(flacFile.FullName);
if (cdNumber != null)
{
taglibFile.Tag.Disc = cdNumber.Value;
}
taglibFile.Save();
}
private static uint? GetCdNumber(string name)
{
for (uint i = 99; i > 0; i--)
{
string fname = String.Format("CD{0}", i);
if (name.Contains(fname))
return i;
}
return null;
}
}
}