Search code examples
javaswingyoutube-dl

youtube-dl converts "C:\" to "C#\" when setting save location


I am creating a Java based GUI for the Windows exe version of youtube-dl .
The OUTPUT TEMPLATE portion of the README says that -o can be used to set the save location and file name while downloading.
When I use the program through command line, I can set the download location as normal using youtube-dl.exe -o "C:\Users\<user>\Videos\%(title)s.%(ext)s" <youtube-link> and it downloads as normal to the specified folder.
However, when I am calling the process through Java, using ProcessBuilder as follows:

output = "-o \"" + save_path + "\\%(title)s.%(ext)s\"";
Process process = new ProcessBuilder("lib\\youtube-dl.exe", output, url.getText()).start();

I keep getting the following output:

-o "C:\Users\nightstalker\Videos\youtube-dl\%(title)s.%(ext)s"
Thread Start
[youtube] wnc77S-g0qQ: Downloading webpage
[youtube] wnc77S-g0qQ: Extracting video information
[youtube] wnc77S-g0qQ: Downloading js player en_US-vfljL8ofl
[youtube] wnc77S-g0qQ: Downloading DASH manifest
[download] Destination:  C#\Users\nightstalker\Videos\youtube-dl\Some Video.mp4

This is what save_path looks like

File save_path = new File("C:\\Users\\"+System.getProperty("user.name")+"\\Videos\\youtube-dl");

This basically creates a folder called C#\Users\nightstalker\Videos\youtube-dl and continues to download there. Any reason why?


Solution

  • I'm going to sidestep the formatting problem and hopefully provide an answer that still works for you.

    ProcessBuilder lets you set the working directory as follows:

    Process p = null;
    ProcessBuilder pb = new ProcessBuilder("do_foo.sh");
    pb.directory("/home");
    p = pb.start();
    

    Source: https://stackoverflow.com/a/8405745/154527

    Instead of putting the whole path into the -o option passed to youtube-dl you can set the directory() on the ProcessBuilder to **C:\Users\<user>\Videos** as follows:

    output = "\"%(title)s.%(ext)s\"";
    Process process = new ProcessBuilder("lib\\youtube-dl.exe", "-o", output, url.getText()).directory("save_path").start();