Majed
Majed

Reputation: 1

How can i get the download link using yt-dlp

this java code will download the youtube video direct to the selectedDirectory. i am trying to figure out if it is possible to get instead a download link print it out. if i paste this link to a browser it will ask me where to save it then it will start download the selected format video or audio.

package com.chillyfacts.com;

import java.io.PrintWriter;
import javafx.application.Application;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;

public class my_main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        String dlp = "D:\youtube_db_java\yt-dlP ";

        DirectoryChooser directoryChooser = new DirectoryChooser();


        java.io.File selectedDirectory = directoryChooser.showDialog(primaryStage);


        if (selectedDirectory != null) {
            String chosenPath = selectedDirectory.getAbsolutePath();
            String url = "https://www.youtube.com/watch?v=BaW_jenozKc";
            String[] command = {
                "cmd",
            };
            Process p;
            try {
                p = Runtime.getRuntime().exec(command);
                new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();
                new Thread(new SyncPipe(p.getInputStream(), System.out)).start();
                PrintWriter stdin = new PrintWriter(p.getOutputStream());
                stdin.println("cd "" + chosenPath + """);
                stdin.println(dlp + url + " -f best/bestvideo+bestaudio " );
                stdin.close();
                p.waitFor();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("No directory selected.");
        }
    }
}

i want to get the video or audio as link to download

Upvotes: 0

Views: 7063

Answers (1)

pierpy
pierpy

Reputation: 1322

The right command line to get the download link is:
yt-dlp --get-url [...]

So, in your case, maybe
stdin.println(dlp + "--get-url " + url + " -f best/bestvideo+bestaudio " ); does the job.
Please note, I'm not familiar with Java, and I don't know if this is the best way to pass parameters to an external executable

Upvotes: 2

Related Questions