C#使用参数执行外部可执行文件

2024-09-25 18:14:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试将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);
            }  
        }
    }
}

Tags: path代码nameinoutputstringifos
1条回答
网友
1楼 · 发布于 2024-09-25 18:14:56

我不懂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);
        }
    }
}

相关问题 更多 >