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

130 lines
5.5 KiB
C#

using System.Buffers.Text;
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(".mp3"))
{
TagLib.File file = TagLib.File.Create(fi.FullName);
TagLib.Id3v2.Tag tag = (TagLib.Id3v2.Tag)file.GetTag(TagLib.TagTypes.Id3v2);
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\\lame.exe";
processStartInfo.Arguments = String.Format("\"{0}\"", wavFile.FullName);
processStartInfo.WorkingDirectory = wavFile.Directory.FullName;
Process? process = Process.Start(processStartInfo);
process.WaitForExit();
FileInfo flacFile = new FileInfo(Path.ChangeExtension(wavFile.FullName, ".mp3"));
if (!flacFile.Exists)
{
MessageBox.Show("Failed to generate MP3 file.");
return;
}
TagLib.File taglibFile = TagLib.File.Create(flacFile.FullName);
TagLib.Id3v2.Tag tag2 = (TagLib.Id3v2.Tag)taglibFile.GetTag(TagLib.TagTypes.Id3v2);
foreach (FileInfo fi in fileInfos)
{
string extension = Path.GetExtension(fi.FullName).ToLowerInvariant();
TagLib.Id3v2.UserTextInformationFrame frame;
switch (extension)
{
case ".cue":
frame = new TagLib.Id3v2.UserTextInformationFrame("AZUSA_CLEAN_CUE");
frame.Text = new string[] { File.ReadAllText(fi.FullName) };
break;
case ".md5":
frame = new TagLib.Id3v2.UserTextInformationFrame("AZUSA_MD5");
frame.Text = new string[] { File.ReadAllText(fi.FullName) };
break;
case ".ibg":
frame = new TagLib.Id3v2.UserTextInformationFrame("AZUSA_IBG");
frame.Text = new string[] { File.ReadAllText(fi.FullName) };
break;
case ".cdt":
byte[] ibgBytes = File.ReadAllBytes(fi.FullName);
frame = new TagLib.Id3v2.UserTextInformationFrame("AZUSA_CDT");
frame.Text = new string[] { Convert.ToBase64String(ibgBytes) };
break;
default:
MessageBox.Show(String.Format("Don't know what to do with a {0} file.", fi.FullName));
return;
}
frame.TextEncoding = TagLib.StringType.UTF8;
tag2.AddFrame(frame);
}
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;
TagLib.Id3v2.UserTextInformationFrame azupackFrame = new TagLib.Id3v2.UserTextInformationFrame("AZUPACK");
azupackFrame.Flags = TagLib.Id3v2.FrameFlags.None;
azupackFrame.Text = new string[] { new AzupackMeta().ToString() };
tag2.AddFrame(azupackFrame);
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;
}
}
}