I am trying to make a program that can download youtube videos as mp3 files. I used this youtube-mp3.org website to achieve this. So, I downloaded the contents of www.youtube-mp3.org/?c#v=sTbd2e2EyTk , where sTbd2e2EyTk is the identifier of the video, now I need to get a link to the mp3 file (in this case http://www.youtube-mp3.org / get? video_id ..... ), but there is no link in the downloaded content. I noticed that chrome developer tools (ctrl + shift + j, tab Elements) show that the link and view source option (ctrl + u) in chrome gives me the same result that I get when loading a page using java. How can I get this link? I tried to get the data using JSoap, but the data that I need is not loaded onto the page right away, so I canβt get it.
The following code is for loading the contents of a web page ...
URL tU = new URL("http://www.youtube-mp3.org/?c#v=sTbd2e2EyTk"); HttpURLConnection conn = (HttpURLConnection) tU.openConnection(); InputStream ins = conn.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(ins)); String line; StringBuffer content = new StringBuffer(); while ((line = rd.readLine()) != null) { content.append(line); } System.out.println(content.toString());
I used this method to get the file, but I need a link.
private static void downloadStreamData(String url, String fileName) throws Exception { URL tU = new URL(url); HttpURLConnection conn = (HttpURLConnection) tU.openConnection(); String type = conn.getContentType(); InputStream ins = conn.getInputStream(); FileOutputStream fout = new FileOutputStream(new File(fileName)); byte[] outputByte = new byte[4096]; int bytesRead; int length = conn.getContentLength(); int read = 0; while ((bytesRead = ins.read(outputByte, 0, 4096)) != -1) { read += bytesRead; System.out.println(read + " out of " + length); fout.write(outputByte, 0, bytesRead); } fout.flush(); fout.close(); }
source share