Here is a class to download videos from youtube with java
package youtubemanager;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class YouTube {
public static void main(String[] args) throws Exception {
YouTube.download("http://www.youtube.com/watch?v=bEXnC1qLUZk", "test.flv");
}
/*
* this function downloads a video based on its url
*/
public static void download(String url, String fileName) throws Exception {
String id = url.split("v=")[1].split("&")[0];
System.out.println(url + " " + id);
String nid = getDownloadId(id);
if (nid == null) {
throw new Exception("Invalud url");
}
url = "http://www.youtube.com/get_video?video_id=" + id + "&t=" + nid;
downloadStreamData(url, fileName);
}
/*
* returns the id of the video data
*/
private static String getDownloadId(String videoId) throws MalformedURLException, IOException {
String url = "http://www.youtube.com/get_video_info?&video_id=" + videoId;
URL tU = new URL(url);
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);
}
String sContent = URLDecoder.decode(content.toString(), "UTF-8");
String[] tokens = sContent.split("&");
for (int i = 0; i < tokens.length - 1; i++) {
if (tokens[i] == null) {
continue;
}
String param1 = tokens[i].substring(0, tokens[i].indexOf("="));
String param2 = tokens[i].substring(param1.length() + 1);
if (param1.equals("token")) {
return param2;
}
}
return "";
}
/*
* this function will download the file after the stream is found
*/
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();
}
}
sir please tell what to do
ReplyDeletethanks in advance.
Hi raman
ReplyDeleteSome ids have no matching file info but this program works.
Sir,
ReplyDeleteEven i am encountering the same error "File NOT FOUND exception". Please help us to find the solution .
gouessej
ReplyDeleteI also get same problem file not found this code is not working.... in any id
The code is normally but the URL is not now accessible.You can copy this URL in Youtube and you can see.
ReplyDeleteChange URL in enter parameter.
I also get same problem .if any get rid of problem please suggest.
ReplyDeleteSame problem here as well. I changed the url with one that works, but it still gives me FileNotFoundException.
ReplyDeleteHelp need urgently with this.
hello how to download any video from url path like download manager
ReplyDeleteplz suggest...
this code sucks, nothing can be downloaded by this. i checked upto 10 video urls , but it gives the same error.
ReplyDeleteThis code does not works anymore.
ReplyDeletedid anyone find a proper solution .please i need ur help
ReplyDeleteMaven dependecy
ReplyDeletegroupId com.github.axet
artifactId vget
version1.1.33
Code :
VGet v = new VGet(new URL(url), new File("tmp/"));
VGetParser user = new YouTubeMPGParser();
v.download(user);
DownloadedFileLogger.addYoutubeLink(url, "tmp/" + v.getTarget().getName());
return v.getTarget();