using System;
using
System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.IO;
namespace FLAC2ALAC {
public partial class MainForm:Form {
string
strDefaultDir;
string
strTargetName;
string
strTargetDir;
string[]
tagResult;
Process convProc;
ProcessStartInfo
metaInfo;
StreamReader
OutReader;
StreamReader
ErrReader;
ThreadStart ConvThread;
Thread t;
public MainForm() {
InitializeComponent();
strDefaultDir = System.Environment.CurrentDirectory;
tagResult = new string[15];
btnConvert.Enabled = false;
}
private void
btnFileOpen_Click(object sender,EventArgs e) {
OpenFileDialog
dig = new
OpenFileDialog();
dig.FileName = "";
dig.Filter = "Lossless
Audio Files (*.flac)|*.flac";
dig.Title = "Select
files";
if(dig.ShowDialog() == DialogResult.OK)
{
txtFilePath.Text = dig.FileName;
strTargetName = dig.SafeFileName;
strTargetDir =
dig.FileName.Remove(dig.FileName.Length-dig.SafeFileName.Length);
getFileInfo(dig.SafeFileName,
dig.FileName);
btnConvert.Enabled = true;
}
}
private void
getFileInfo(string SafeFileName, string FileName) {
string[] tags = {"--show-tag=ARTIST
",
"--show-tag=ALBUM
",
"--show-tag=TITLE
",
"--show-tag=GENRE
",
"--show-tag=DATE
",
"--show-tag=TRACKNUMBER
",
"--show-tag=TOTALTRACKS
",
"--show-tag=BPM
",
"--show-tag=COMMENT
",
"--show-tag=GROUPING
",
"--show-tag=COMPILATION
",
"--show-tag=COMPOSER
",
"--show-tag=DISCNUMBER
",
"--show-tag=TOTALDISCS
"};
int index = 0;
foreach(string tag in tags) {
SetProcessInfo(tag,
FileName);
convProc.StartInfo = metaInfo;
convProc.Start();
OutReader = convProc.StandardOutput;
tagResult[index] = OutReader.ReadToEnd();
index++;
}
tagResult[14] = "FILENAME="+SafeFileName;
for(index=0; index<15; index++)
txtStatus.Text +=
tagResult[index];
}
private void
SetProcessInfo(string tag, string FileName) {
convProc = new Process();
metaInfo = new ProcessStartInfo(strDefaultDir+"\\metaflac.exe", tag+"\""+FileName+"\"");
metaInfo.UseShellExecute = false;
metaInfo.RedirectStandardInput = false;
metaInfo.RedirectStandardOutput = true;
metaInfo.RedirectStandardError = false;
metaInfo.CreateNoWindow = true;
}
private void
SetConvWavProcess(string FileName) {
convProc = new Process();
metaInfo = new ProcessStartInfo(strDefaultDir+"\\flac.exe", "-o
\""+strDefaultDir+"\\tmp.wav\"
"+"-d \""+FileName+"\"");
metaInfo.UseShellExecute = false;
metaInfo.RedirectStandardInput = false;
metaInfo.RedirectStandardOutput = true;
metaInfo.RedirectStandardError = true;
metaInfo.CreateNoWindow = true;
}
private void
SetConvALACProcess() {
string strOption = "";
if(tagResult[0] != "") strOption
+= ("-a \""+tagResult[0].Split('=')[1].Remove(tagResult[0].Split('=')[1].Length-2)+"\"
");
if(tagResult[1] != "") strOption
+= ("-l \""+tagResult[1].Split('=')[1].Remove(tagResult[1].Split('=')[1].Length-2)+"\"
");
if(tagResult[2] != "") strOption
+= ("-t \""+tagResult[2].Split('=')[1].Remove(tagResult[2].Split('=')[1].Length-2)+"\"
");
if(tagResult[3] != "") strOption
+= ("-g \""+tagResult[3].Split('=')[1].Remove(tagResult[3].Split('=')[1].Length-2)+"\"
");
if(tagResult[4] != "") strOption
+= ("-y \""+tagResult[4].Split('=')[1].Remove(4)+"\"
");
if(tagResult[5] != "") strOption
+= ("-n \""+tagResult[5].Split('=')[1].Remove(tagResult[5].Split('=')[1].Length-2)+"\"
");
if(tagResult[6] != "") strOption
+= ("-m \""+tagResult[6].Split('=')[1].Remove(tagResult[6].Split('=')[1].Length-2)+"\"
");
if(tagResult[7] != "") strOption
+= ("-b \""+tagResult[7].Split('=')[1].Remove(tagResult[7].Split('=')[1].Length-2)+"\"
");
if(tagResult[8] != "") strOption
+= ("-c \""+tagResult[8].Split('=')[1].Remove(tagResult[8].Split('=')[1].Length-2)+"\"
");
if(tagResult[9] != "") strOption
+= ("-u \""+tagResult[9].Split('=')[1].Remove(tagResult[9].Split('=')[1].Length-2)+"\"
");
if(tagResult[10] != "") strOption
+= ("-x \""+tagResult[10].Split('=')[1].Remove(tagResult[10].Split('=')[1].Length-2)+"\"
");
if(tagResult[11] != "") strOption
+= ("-p \""+tagResult[11].Split('=')[1].Remove(tagResult[11].Split('=')[1].Length-2)+"\"
");
if(tagResult[12] != "") strOption
+= ("-j \""+tagResult[12].Split('=')[1].Remove(tagResult[12].Split('=')[1].Length-2)+"\"
");
if(tagResult[13] != "") strOption
+= ("-k \""+tagResult[13].Split('=')[1].Remove(tagResult[13].Split('=')[1].Length-2)+"\"
");
if(tagResult[14] != "") strOption
+= ("-r \""+tagResult[14].Split('=')[1]+"\"
");
if(chkRegist.Checked==false) strOption += "-d
";
convProc = new Process();
metaInfo = new ProcessStartInfo(strDefaultDir+"\\iTunesEncode.exe", strOption+"-e \"Lossless Encoder\" -i \""+strDefaultDir+"\\tmp.wav\" -o \""+strTargetDir+tagResult[14].Split('=')[1].Remove(tagResult[14].Split('=')[1].Length-5)+".m4a\"");
metaInfo.UseShellExecute =
false;
metaInfo.RedirectStandardInput = false;
metaInfo.RedirectStandardOutput = true;
metaInfo.RedirectStandardError = true;
metaInfo.CreateNoWindow = true;
}
private void
btnConvert_Click(object sender,EventArgs e) {
ConvThread = new ThreadStart(ConvFLAC2ALAC);
t = new
Thread(ConvThread);
t.Start();
}
private void
ConvFLAC2ALAC() {
SetConvWavProcess(strTargetDir+strTargetName);
convProc.StartInfo = metaInfo;
convProc.Start();
OutReader = convProc.StandardOutput;
ErrReader = convProc.StandardError;
txtProgress.AppendText(OutReader.ReadToEnd());
txtProgress.AppendText(ErrReader.ReadToEnd());
txtProgress.ScrollToCaret();
SetConvALACProcess();
convProc.StartInfo = metaInfo;
convProc.Start();
OutReader = convProc.StandardOutput;
ErrReader = convProc.StandardError;
txtProgress.AppendText(OutReader.ReadToEnd());
txtProgress.AppendText(ErrReader.ReadToEnd());
txtProgress.ScrollToCaret();
if(System.IO.File.Exists(strDefaultDir+"\\tmp.wav")) {
System.IO.File.Delete(strDefaultDir+"\\tmp.wav");
}
txtProgress.AppendText("\r\r\nFLAC --> ALAC\r\r\nDONE!");
txtProgress.ScrollToCaret();
btnConvert.Enabled = false;
txtFilePath.Text = "";
txtStatus.Text = "";
}
private void
MainForm_FormClosed(object sender,FormClosedEventArgs e) {
if(t != null) {
if(t.IsAlive)
t.Abort();
}
}
}
}
|