0

I have been trying to convert my python code to c# code. For some reason, the c# code gets to the DirectoryInfo declaration and says the path is not found. If someone can tell me why, it would be appreciated.

This is the original python code:

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" ])

This is my current c# code:

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);
            }  
        }
    }
}
4
  • What line is it failing on? What path is it trying to use? Commented Dec 22, 2015 at 2:01
  • 1
    stackoverflow.com/questions/8094428/… Commented Dec 22, 2015 at 2:10
  • Without knowing which error you get, is pretty impossible to solve this. BUT, you are escaping characters twice, like here: ipath + @"\\Finished\\" it should be ` \\ ` or ` @"\" ` Commented Dec 22, 2015 at 2:18
  • Ok. Update: when executed the processes are launched then close immediately. Commented Dec 22, 2015 at 3:02

1 Answer 1

0

I don't understand Python, but from what I see, your c# code and python code definitely do different things. There are bunch of things that are not clear. I have commented your code with my advice. Hopefully that will solve your issues

Commented code below

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????
            }  
        }
    }
}

Below code is based on my guess about what you want

/*
    * 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);
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I just need to fix the errors I'm getting with handbrake.
I am guessing that the input file does not exist for handbrake which is causing the early exit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.