Since fucking Google/Youtube change the get_video_info again, it add a new parameter "signature", and it cause the old method not work anymore,
now here is the solution.
package vimeok.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import android.util.Log;
public class VideoParser {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<VideoInfoObj> videoList= getVideoList("jd2-Nf86jvA");
for(VideoInfoObj vo: videoList){
System.err.println(vo.getVideoType());
System.err.println(vo.getDwName());
System.err.println(vo.getUrl());
File f=new File(vo.getDwName());
try {
org.apache.commons.io.FileUtils.copyURLToFile(new URL(vo.getUrl()),f);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static List<VideoInfoObj> getVideoList(String vid){
String[] fmt_str = new String[103];
fmt_str[0] = "(FLV, 320 x 240, Mono 22KHz MP3)"; // delete ?
fmt_str[5] = "(FLV, 400 x 240, Mono 44KHz MP3)";
fmt_str[6] = "(FLV, 480 x 360, Mono 44KHz MP3)"; // delete ?
fmt_str[34] = "(FLV, 640 x 360, Stereo 44KHz AAC)";
fmt_str[35] = "(FLV, 854 x 480, Stereo 44KHz AAC)";
fmt_str[13] = "(3GP, 176 x 144, Stereo 8KHz)"; // delete ?
fmt_str[17] = "(3GP, 176 x 144, Stereo 44KHz AAC)";
fmt_str[36] = "(3GP, 320 x 240, Stereo 44KHz AAC)";
fmt_str[18] = "(MP4(H.264), 640 x 360, Stereo 44KHz AAC)";
fmt_str[22] = "(MP4(H.264), 1280 x 720, Stereo 44KHz AAC)";
fmt_str[37] = "(MP4(H.264), 1920 x 1080, Stereo 44KHz AAC)";
fmt_str[38] = "(MP4(H.264), 4096 x 3072, Stereo 44KHz AAC)";
fmt_str[83] = "(MP4(H.264), 854 x 240, Stereo 44KHz AAC)";
fmt_str[82] = "(MP4(H.264), 640 x 360, Stereo 44KHz AAC)";
fmt_str[85] = "(MP4(H.264), 1920 x 520, Stereo 44KHz AAC)";
fmt_str[84] = "(MP4(H.264), 1280 x 720, Stereo 44KHz AAC)";
fmt_str[43] = "(WebM(VP8), 640 x 360, Stereo 44KHz Vorbis)";
fmt_str[44] = "(WebM(VP8), 854 x 480, Stereo 44KHz Vorbis)";
fmt_str[45] = "(WebM(VP8), 1280 x 720, Stereo 44KHz Vorbis)";
fmt_str[100] = "(WebM(VP8), 640 x 360, Stereo 44KHz Vorbis)";
fmt_str[101] = "(WebM(VP8), 854 x 480, Stereo 44KHz Vorbis)";
fmt_str[46] = "(WebM(VP8), 1920 x 540, Stereo 44KHz Vorbis)";
fmt_str[102] = "(WebM(VP8), 1280 x 720, Stereo 44KHz Vorbis)";
String[] ft_str = new String[103];
ft_str[0] = "flv";//"(FLV, 320 x 240, Mono 22KHz MP3)";
ft_str[5] = "flv";//"(FLV, 400 x 240, Mono 44KHz MP3)";
ft_str[6] = "flv";//"(FLV, 480 x 360, Mono 44KHz MP3)";
ft_str[34] = "flv";//"(FLV, 640 x 360, Stereo 44KHz AAC)";
ft_str[35] = "flv";//"(FLV, 854 x 480, Stereo 44KHz AAC)";
ft_str[13] = "3gp";//"(3GP, 176 x 144, Stereo 8KHz)"; // delete ?
ft_str[17] = "3gp";//"(3GP, 176 x 144, Stereo 44KHz AAC)";
ft_str[36] = "3gp";//"(3GP, 320 x 240, Stereo 44KHz AAC)";
ft_str[18] = "mp4";//"(MP4(H.264), 640 x 360, Stereo 44KHz AAC)";
ft_str[22] = "mp4";//"(MP4(H.264), 1280 x 720, Stereo 44KHz AAC)";
ft_str[37] = "mp4";//"(MP4(H.264), 1920 x 1080, Stereo 44KHz AAC)";
ft_str[38] = "mp4";//"(MP4(H.264), 4096 x 3072, Stereo 44KHz AAC)";
ft_str[83] = "mp4";//"(MP4(H.264), 854 x 240, Stereo 44KHz AAC)";
ft_str[82] = "mp4";//"(MP4(H.264), 640 x 360, Stereo 44KHz AAC)";
ft_str[85] = "mp4";//"(MP4(H.264), 1920 x 520, Stereo 44KHz AAC)";
ft_str[84] = "mp4";//"(MP4(H.264), 1280 x 720, Stereo 44KHz AAC)";
ft_str[43] = "webm";//"(WebM(VP8), 640 x 360, Stereo 44KHz Vorbis)";
ft_str[44] = "webm";//"(WebM(VP8), 854 x 480, Stereo 44KHz Vorbis)";
ft_str[45] = "webm";//"(WebM(VP8), 1280 x 720, Stereo 44KHz Vorbis)";
ft_str[100] = "webm";//"(WebM(VP8), 640 x 360, Stereo 44KHz Vorbis)";
ft_str[101] = "webm";//"(WebM(VP8), 854 x 480, Stereo 44KHz Vorbis)";
ft_str[46] = "webm";//"(WebM(VP8), 1920 x 540, Stereo 44KHz Vorbis)";
ft_str[102] = "webm";//"(WebM(VP8), 1280 x 720, Stereo 44KHz Vorbis)";
List<VideoInfoObj> list=new ArrayList<VideoInfoObj>();
try{
int succ = 0;
//String vid="jd2-Nf86jvA";
String url="http://www.youtube.com/get_video_info?video_id="+vid;//+"&eurl=http://10.1.2.104";
//String rdata= org.apache.commons.io.FileUtils.readFileToString(new File("d:\\get_video_info.php"));
String rdata=getMessageFromUrl(url);
Log.w("***************************",rdata);
URLDecoder dec = new URLDecoder();
//String tmp_result = java.net.URLDecoder.decode(content, "UTF-8");
String[] rdataArray = rdata.split("&");
for(int i=0; i< rdataArray.length; i++){
if(rdataArray[i].length()>26){
String r0 = rdataArray[i].substring(0, 26);
if(r0.equals("url_encoded_fmt_stream_map")){
String r1 = dec.decode(rdataArray[i].substring(27),"UTF-8");
String[] temp1 = r1.split(",");
ArrayList<Integer> fmt = new ArrayList<Integer>();
ArrayList fmt_url = new ArrayList();
ArrayList fmt_sig = new ArrayList();
for(int j = 0; j < temp1.length; j++){
String[] temp2 = temp1[j].split("&");
for(int jj = 0; jj < temp2.length; jj++){
Integer temp_itag = -1;
String temp_type = "";
System.err.println(temp2[jj]);
if(temp2[jj].substring(0, 5).equals("itag=")){
temp_itag = new Integer(Integer.parseInt(temp2[jj].substring(5), 10));
fmt.add(temp_itag);
}else if(temp2[jj].substring(0, 4).equals("url=")){
fmt_url.add(temp2[jj].substring(4));
}else if(temp2[jj].substring(0, 4).equals("sig=")){
fmt_sig.add(temp2[jj].substring(4));
}else if(temp2[jj].substring(0, 5).equals("type=")){
temp_type = '(' + dec.decode(temp2[jj].substring(5),"UTF-8") + ')';
}
System.err.println("temp_itag="+temp_itag);
System.err.println(fmt_str[44]);
if(temp_itag>-1){
if(fmt_str[temp_itag].equals("undefined")){
fmt_str[temp_itag] = temp_type;
}
}
}
}
String dllinks = "";
String webmlinks = "";
System.err.println("fmt.size()="+fmt.size());
for(int k=0; k< fmt.size(); k++){
System.err.println(k);
if(fmt.get(k) == 43 || fmt.get(k) == 44 || fmt.get(k) == 45 || fmt.get(k) == 46 || fmt.get(k) == 100 || fmt.get(k) == 101 || fmt.get(k) == 102){
// if(webmlinks.length() > 0){
// webmlinks += "<br />";
// }
//webmlinks += "<a href=" + dec.decode(fmt_url.get(k).toString(),"UTF-8") + "&signature=" + fmt_sig.get(k) + " target=_blank><b>Watch online " + fmt_str[fmt.get(k)]+ "</b></a>";
//System.err.println("webmlinks="+webmlinks);
}else{
// if(dllinks.length() > 0){
// dllinks += "<br />";
// }
// dllinks += "<a href=" + dec.decode(fmt_url.get(k).toString(),"UTF-8") + "&signature=" + fmt_sig.get(k) + " target=_blank><b>Download " + fmt_str[fmt.get(k)] + "</b></a>";
Log.w("***************",dec.decode(fmt_url.get(k).toString(),"UTF-8") + "&signature=" + fmt_sig.get(k));
//I just need mp4 file
if(fmt.get(k) ==18 ||fmt.get(k) ==22 ||fmt.get(k) ==37 ||fmt.get(k) ==38 ||fmt.get(k) ==82 ||fmt.get(k) ==83 ||fmt.get(k) ==84 ||fmt.get(k) ==85 ){
String dl=dec.decode(fmt_url.get(k).toString(),"UTF-8") + "&signature=" + fmt_sig.get(k);
String fileName="d:\\video"+System.currentTimeMillis()+"."+ft_str[fmt.get(k)];
// File f=new File(fileName);
// org.apache.commons.io.FileUtils.copyURLToFile(new URL(dl),f);
// System.err.println("dllinks="+dl);
VideoInfoObj vi= new VideoInfoObj();
vi.setDwName(fileName);
vi.setUrl(dl);
vi.setVideoType(fmt_str[fmt.get(k)]);
list.add(vi);
}
}
}
System.err.println(r1);
}
//System.err.println(r0);
}
}
}catch(Exception e){
e.printStackTrace();
}
return list;
}
public static String getMessageFromUrl(String msgUrl) throws IOException {
URL url = new URL(msgUrl);
BufferedReader in = new BufferedReader(new InputStreamReader(
url.openStream()));
String inputLine;
StringBuffer sb = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
}
in.close();
return sb.toString();
}
}
public class VideoInfoObj{
private String url="";
private String dwName="";
private String videoType="";
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDwName() {
return dwName;
}
public void setDwName(String dwName) {
this.dwName = dwName;
}
public String getVideoType() {
return videoType;
}
public void setVideoType(String videoType) {
this.videoType = videoType;
}
}