Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
package youtubedownloader;

import javafx.application.Application;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.ActionEvent;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.text.Font;
import javafx.scene.control.ProgressBar;
import javafx.stage.Stage;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;


public class youtubedownloader extends Application {

private Button downloadBtn = new Button();
private TextField youtubeUrlField = new TextField();
private TextField fileName = new TextField();
private Label label1 = new Label();
private Label label2 = new Label();
private Label about = new Label("Created and Developed by SB");
private ProgressBar pbar = new ProgressBar(0);
private URL downloadURL;


@Override
public void start(Stage primaryStage) throws Exception{


Font arial = new Font("arial", 16);
Font arial1 = new Font("arial1", 13);


downloadBtn.setText("Download");
downloadBtn.setFont(arial);
downloadBtn.setLayoutX(10);
downloadBtn.setLayoutY(200);

youtubeUrlField.setFont(arial); youtubeUrlField.setLayoutX(10);
youtubeUrlField.setLayoutY(80);
youtubeUrlField.setPrefColumnCount(23);
fileName.setFont(arial);
fileName.setLayoutX(10);
fileName.setLayoutY(150);
fileName.setPrefColumnCount(23);
label1.setFont(arial);
label1.setText("Youtube URL");
label1.setLayoutX(10);
label1.setLayoutY(50);
label2.setFont(arial);
label2.setText("File Name (Optional)");
label2.setLayoutX(10);
label2.setLayoutY(120);
pbar.setVisible(false);
pbar.setPrefWidth(350);

pbar.setLayoutX(100); pbar.setLayoutY(200);
about.setFont(arial1);
about.setLayoutX(20);
about.setLayoutY(260);

downloadBtn.setOnAction((ActionEvent event) -> {
sendHTTPRequest.restart(); });

sendHTTPRequest.setOnSucceeded((WorkerStateEvent we) -> { try { downloadURL = new URL(getURLS(sendHTTPRequest.getValue()));
pbar.progressProperty().unbind();
pbar.setProgress(0);
pbar.progressProperty().bind(VideoDownload.progressProperty());
pbar.setVisible(true);
VideoDownload.restart(); } catch (MalformedURLException ex) { Alert msg = new Alert(Alert.AlertType.INFORMATION);
msg.setTitle("Message from Youtube Downloader");
msg.setContentText("Invalid Url");
msg.showAndWait(); } });

VideoDownload.setOnSucceeded((WorkerStateEvent we) -> { boolean val = VideoDownload.getValue();
System.out.println(val);
if (val) { Alert msg = new Alert(Alert.AlertType.INFORMATION);
msg.setTitle("Message from Youtube Downloader");
msg.setContentText("Download complete");
msg.showAndWait(); } else { Alert msg = new Alert(Alert.AlertType.INFORMATION);
msg.setTitle("Message from Youtube Downloader");
msg.setContentText("Download Failed");
msg.showAndWait(); }
pbar.setVisible(false);
});

Group root = new Group(); root.getChildren().add(downloadBtn);
root.getChildren().add(youtubeUrlField);
root.getChildren().add(fileName);
root.getChildren().add(label1);
root.getChildren().add(label2);
root.getChildren().add(pbar);
root.getChildren().add(about);
Scene scene = new Scene(root, 500, 280);
primaryStage.setTitle("Youtube Downloader");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}

private String getVideoID(String url) { int index = url.indexOf("v=");
index += 2;
String id = new String();
for (int i = index; i < url.length(); i++) id+= url.charAt(i);
return id;
}

final private Service< StringBuilder > sendHTTPRequest = new Service < StringBuilder > () {
@Override
protected Task< StringBuilder > createTask() { return new Task < StringBuilder > () {
@Override
protected StringBuilder call() { String response;
StringBuilder res = new StringBuilder();
StringBuilder refinedres = new StringBuilder();
try { URL url = new URL("https://www.youtube.com/get_video_info?&video_id=" + getVideoID(youtubeUrlField.getText())); System.out.println(url.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET");
System.out.println(conn.getResponseMessage());
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); while ((response = in .readLine()) != null) res.append(response); refinedres.append(URLDecoder.decode(URLDecoder.decode(res.toString(), "UTF-8"), "UTF-8")); in .close(); return refinedres; } catch (MalformedURLException ex) {} catch (IOException ex) {}
return null;
}
};
}
};

Service < Boolean > VideoDownload = new Service < Boolean > () {
@Override
protected Task < Boolean > createTask() { return new Task < Boolean > () {
@Override
protected Boolean call() throws Exception { long length;
boolean completed = false;
int count = 0;
try (BufferedInputStream bis = new BufferedInputStream(downloadURL.openStream()); FileOutputStream fos = new FileOutputStream(fileName.getText().length() == 0 ? "video.mp4" : fileName.getText().concat(".mp4"))) { length = downloadURL.openConnection().getContentLength();
int i = 0;
final byte[] data = new byte[1024];
while ((count = bis.read(data)) != -1) { i += count;
fos.write(data, 0, count);
updateProgress(i, length); }
completed = true; } catch (IOException ex) {}
return completed;
}
};
}
};

private String getURLS(StringBuilder response) { StringBuilder temp1 = new StringBuilder();
String[] temp2, temp3, temp4;
try { int index = response.indexOf("url_encoded_fmt_stream_map");
for (int i = index; i < response.length(); i++) { temp1.append(response.charAt(i)); }
temp2 = temp1.toString().split("&url=");
if (temp2.length > 0) { temp3 = temp2[1].split(";");
if (temp3.length > 0) { temp4 = temp3[0].split(",");
if (temp4.length > 0) return temp4[0];
else return temp3[0]; } else return temp2[1]; } } catch (Exception e) { Alert msg = new Alert(Alert.AlertType.INFORMATION);
msg.setTitle("Message form youtube Downloader");
msg.setContentText("Error in downloading");
msg.showAndWait(); }
return null;
}



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

}

}

What I have tried:

Hi guys...I developed this You Tube Downloader program, but it doesn't work,,,can someone tell me what i did wrong,please...
Posted
Updated 27-Mar-20 23:11pm
Comments
Richard MacCutchan 28-Mar-20 4:38am    
"can someone tell me what i did wrong,please..."
Yes, you just dumped a load of unformatted code, but did not bother to explain what the problem is.

1 solution

There are many, many things you did wrong, starting right here:
Quote:
I developed this You Tube Downloader program

Downloading YouTube videos is against YouTube terms and conditions, and counts as malicious activity. We do not condone, support, or assist in the production of malicious code in any way, form, or manner. This is a professional site for professional developers.

If you want to know how to create such things, you need to visit a hacking site: but be sure to disable all firewalls and antivirus products first or they won't trust you enough to tell you.

The second thing you did wrong is be rude: dumping 180 lines of unformatted code and saying "fix that" is not considered polite.

The third thing you did wrong is simple: you didn't bother to ask yourself "does YouTube protect it's material at all?"
And the answer is "Yes". "Yes it does".
YouTube works very hard to prevent downloaders from working: that's why there are so few out there, and most of those you do find currently don't work any more, because YouTube has found the "hole" they were using and closed it. It's their IP, it's their revenue stream - and they protect it with gusto!
 
Share this answer
 
Comments
Member 14784364 28-Mar-20 5:51am    
Hi guys, Well i am not a professional in programming, and i know a thing or two about these kind of terms and conditions, and I certainly didn't mean to be rude. this is only for my own educational purposes and not for commercial use... please accept my apology because i am new in the programming field. My goal was just a challenge for my self to do this, and if you don't want to answer is fine,,, no need for hostility.
OriginalGriff 28-Mar-20 6:08am    
That's not hostility, trust me ...

You used Google to find code to steal IP from YouTube ... without ever thinking "who owns YouTube?".
Since 2006, it's been ... Google.
So, they know who you are, where you are, what you are doing - or trying to do ... and they take heavy measures to prevent IP being stolen. Are you sure this is a good idea? If you think I'm "hostile" you have clearly never experienced a pack of rabid lawyers in full war paint ...

I'd strongly suggest you find a project more suitable for beginners!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900