我一直在尝试将我的python代码转换为c#代码。出于某种原因,c#代码获取DirectoryInfo声明并说明找不到路径。如果有人能告诉我原因,我们将不胜感激。
这是原始的python代码:
def encode(path, dest):
for root_dir, dirs, files in os.walk(path, topdown=False):
for name in files:
(base, ext)=os.path.splitext(name)
input_file = os.path.join(root_dir,name)
output_file = os.path.join(dest_dir, base+".mkv")
if (os.path.exists(output_file)):
print ("skipped")
else:
subprocess.call( ["HandBrakeCLI.exe", "-i", input_file, "-o", output_file, "-e", "x264", "--aencoder", "ac3", "-s", "1", "--subtitle-default", "1" ])
这是我目前的c#代码:
string qpath = Path.GetFullPath((Environment.CurrentDirectory + "\\Queue\\"));
if (Directory.Exists(Path.GetFullPath(qpath)))
{
var DirMKV = (Directory.GetFiles(qpath, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".mkv") || s.EndsWith(".mp4")).ToArray());
foreach (string file in DirMKV)
{
DirectoryInfo dirinfo = new DirectoryInfo(file);
if (dirinfo.Parent.Parent.ToString().Contains("S"))
{
string ipath = Environment.CurrentDirectory;
string dpath = ipath + @"\Queue\" + dirinfo.Parent.Parent.ToString() + @"\" + Path.GetFileName(file);
string opath = ipath + @"\Finished\" + dirinfo.Parent.Parent.ToString() + @"\" + Path.GetFileName(file);
string arg = "-i " +dpath + " -o " +opath +" -e x264 "+ " --aencoder ac3 "+ "-s 1 "+ "--subtitle-default 1";
if (!File.Exists(opath))
{
Process.Start(ipath + @"\handbrakeCLI.exe", arg);
}
}
}
}
答案 0 :(得分:0)
我不懂Python,但从我看到的,你的c#代码和python代码肯定会做不同的事情。有很多事情都不清楚。我已经用我的建议评论了你的代码。希望这将解决您的问题
以下评论代码
string qpath = Path.GetFullPath((Environment.CurrentDirectory + "\\Queue\\"));
if (Directory.Exists(Path.GetFullPath(qpath))) // No need to do Path.GetFullPath again
{
var DirMKV = (Directory.GetFiles(qpath, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".mkv") || s.EndsWith(".mp4")).ToArray()); // no need to do ToArray. List all files in Queue folder
foreach (string file in DirMKV)
{
DirectoryInfo dirinfo = new DirectoryInfo(file); // I don't get any Path not found exception here
if (dirinfo.Parent.Parent.ToString().Contains("S")) // Check if GrandParent directory name contains the letter S. Don't see any such thing in python code
{
string ipath = Environment.CurrentDirectory;
string dpath = ipath + @"\\Queue\\" + dirinfo.Parent.Parent.ToString() + @"\" + Path.GetFileName(file); // Set input file to something like Queue\*S*\File.mkv. Does this file exist??? It should use single backslash instead of double backslash
string opath = ipath + @"\\Finished\\" + dirinfo.Parent.Parent.ToString() + @"\" + Path.GetFileName(file); // Set output file to something like Finished\*S*\File.mkv. It should use single backslash instead of double backslash
string arg = "-i " +dpath + " -o " +opath +" -e x264 "+ " --aencoder ac3 "+ "-s 1 "+ "--subtitle-default 1";
if (!File.Exists(opath))
{
Process.Start(ipath + @"\handbrakeCLI.exe", arg); // does the handbrakeCLI.exe exist in the current directory????
}
}
}
}
以下代码基于我对您想要的内容的猜测
/*
* Expects a directory structure like below
* S
* |-> bin
* |-> Queue
* |-> test1.mp4
* |-> test2.mkv
* |-> Finished
* |-> test3.mkv
* |-> executable (this code)
* |-> handbrakeCLI.exe
*
*/
DirectoryInfo qpath = new DirectoryInfo("Queue");
if (qpath.Exists) {
var mkvFiles = qpath.GetFiles("*.*", SearchOption.AllDirectories).Where(s => s.Extension == ".mkv" || s.Extension == ".mp4");
foreach (var mkvFile in mkvFiles) {
var gParent = mkvFile.Directory.Parent.ToString();
if (gParent.Contains("S")) {
string opath = Path.Combine(mkvFile.Directory.Parent.FullName, "Finished", mkvFile.Name);
string arg = "-i " + mkvFile.FullName + " -o " + opath + " -e x264 " + " --aencoder ac3 " + "-s 1 " + "--subtitle-default 1";
if (!File.Exists(opath))
Process.Start(Environment.CurrentDirectory+ @"\handbrakeCLI.exe", arg);
}
}
}