1

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?

Kanishka Ganguly
  • 1,252
  • 4
  • 18
  • 38

1 Answers1

1

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();
Community
  • 1
  • 1
Alain O'Dea
  • 21,033
  • 1
  • 58
  • 84
  • The thing with `youtube-dl` is that it will start downloading directly to the folder in which it is kept. So, this method will just change working directory and not save path for `youtube-dl`. Still, let me give it a try. – Kanishka Ganguly Jun 29 '15 at 21:47
  • 1
    Are you being bitten by this Windows-specific filename encoding bug: https://github.com/rg3/youtube-dl/issues/4787 – Alain O'Dea Jun 29 '15 at 21:53
  • 1
    your answer was perfect. Thanks for enlightening me about `ProcessBuilder`. Did away with `-o` parameter completely, just using the `directory()` option of `ProcessBuilder`. Much thanks! – Kanishka Ganguly Jun 29 '15 at 21:57
  • Great stuff. I'm glad I could help. – Alain O'Dea Jun 29 '15 at 22:01
  • Downvoter, come explain your grievance. I can't fix what I don't know. I addressed an issue with handing the -o switch and its value in a single parameter to the process. That could potentially have been your issue. – Alain O'Dea Jan 09 '16 at 02:02