0

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

Majed
  • 1
  • *i want to get the video or audio as link to download* So why not use your start link in a gui hyperink which downloads the file on click? – g00se Aug 10 '23 at 15:26
  • what do you mean ? – Majed Aug 10 '23 at 15:33
  • I'm not even sure what *you* mean ;) So i'll check - do you mean you want the user to be able to click on a link and *then* download the video? – g00se Aug 10 '23 at 15:46
  • yeah like i want to make after this multiple comboboxs to let the user choose like the media format and the resolution it would be great if you help me with this too. – Majed Aug 10 '23 at 16:17
  • I can't get into the intricacies of JavaFX guis (I don't even do it) but there's not much to this other than responding to user input. You'll make life easier for yourself if you use `ProcessBuilder` too – g00se Aug 10 '23 at 16:34
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Aug 11 '23 at 12:37

1 Answers1

0

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

pierpy
  • 897
  • 3
  • 14
  • 18