Search code examples
javayoutubeyoutube-dl

Java youtube-dl no such file or directory


Im trying to download the audio from a youtube video & save it in the working directory as a.mp3. Im using a youtube-dl java wrapper I fond here: https://github.com/sapher/youtubedl-java

While from what I can tell, it is set up correctly, & I do provide a directory that works, I keep getting:

Exception in thread "main" com.sapher.youtubedl.YoutubeDLException: Cannot run program "youtube-dl" (in directory "/Users/stephenogden/workspace/Gary"): error=2, No such file or directory
    at com.sapher.youtubedl.YoutubeDL.execute(YoutubeDL.java:69)
    at TestingFunctions.functionTotest(TestingFunctions.java:43)
    at TestingFunctions.main(TestingFunctions.java:15)

I have tried moving the destination directory, but the issue still persists.

Here is the code I was working with:

import java.io.File;
import java.io.IOException;

import com.sapher.youtubedl.YoutubeDL;
import com.sapher.youtubedl.YoutubeDLException;
import com.sapher.youtubedl.YoutubeDLRequest;
import com.sapher.youtubedl.YoutubeDLResponse;

public class TestingFunctions {

    public static void main(String[] args) throws InterruptedException, YoutubeDLException {
        try {

            functionTotest("https://www.youtube.com/watch?v=DECxluN8OZM");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void functionTotest(String url) throws IOException, InterruptedException, YoutubeDLException {

        File a = new File("a.mp3");
        if (a.exists()) {
            a.delete();
        }

        // This is the command to run

        String videoUrl = url;
        String directory = System.getProperty("user.dir");

        YoutubeDLRequest request = new YoutubeDLRequest(videoUrl, directory);

        request.setOption("no-mark-watched");
        request.setOption("ignore-errors");
        request.setOption("no-playlist");
        request.setOption("extract-audio");
        request.setOption("audio-format \"mp3\"");
        request.setOption("output \"a.%(ext)s\"");

        YoutubeDLResponse response = YoutubeDL.execute(request);

        String stdOut = response.getOut();

        System.out.println(stdOut);
        System.out.println("File downloaded!");

    }

}

Solution

  • I figured it out.

    I had to add:

    YoutubeDL.setExecutablePath("/usr/local/Cellar/youtube-dl/2017.09.15/bin/youtube-dl");