2012-09-15 55 views
4

我想製作的程序,可以下載YouTube視頻作爲MP3文件。我用這個網站youtube-mp3.org爲了達到目的。所以,我下載了www.youtube-mp3.org/?c#v=sTbd2e2EyTk的內容,其中sTbd2e2EyTk是視頻ID,現在我必須鏈接到mp3文件(在這種情況下爲http://www.youtube-mp3.org/get?video_id.....),但下載的內容中沒有鏈接。我注意到chrome開發人員工具(ctrl + shift + j,tab Elements)顯示chrome中的鏈接和查看源(ctrl + u)選項給了我使用java下載頁面的相同結果。我如何獲得該鏈接? 我試圖使用JSoap獲取數據,但我需要的那些數據沒有立即加載到頁面上,所以我無法獲取它們。YouTube使用網站的mp3轉換

下一個代碼是用於下載網頁的內容...

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()); 

我用這個方法得到的文件,但我需要鏈接..

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(); 
} 

回答

-1

我做這個使用YouTube -mp3.org。 你可以在這裏看看我的代碼:YoutubeMp3

所有你需要做的是以下幾點:

DownloadManager d = new DownloadManager(); 
    d.download(YoutubeMp3.builder("http://www.youtube.com/watch?v=ebcrEqm5FFg"), "C:\\Users\\loikkk\\Music\\testDownload"); 
    d.setDownloadListener(new DownloadListener() { 

       @Override 
       public void onDownloadStart(long totalFileSize) { 
        // TODO Auto-generated method stub 

       } 

       @Override 
       public void onDownloadProcess(long currentFileSize, long totalFileSize) { 
        // TODO Auto-generated method stub 

       } 

       @Override 
       public void onDownloadFinished() { 
        // TODO Auto-generated method stub 
       } 

       @Override 
       public void onDownloadError(int arg0) { 
        // TODO Auto-generated method stub 

       } 

    }); 

希望它能幫助,

@hanry和Josh L:

我在Github上刪除了我的帳戶。但是,如果您使用Google Chrome,則可以從網絡角度使用開發人員控制檯。所以你可以反向設計下載鏈接 這就是我已經做到的。

  1. 提交我的YouTube網址Youtube-mp-3.org
  2. 檢查從WebService
  3. 響應構建donwload鏈接自己以下這些正在使用的模式。

實施例:

YouTube網址: http://www.youtube.com/watch?v=KMU0tzLwhbE;

請求在內容主體web服務:KMU0tzLwhbE從web服務

響應:信息= { 「標題」: 「開發」, 「圖像」: 「http://i.ytimg.com/vi/KMU0tzLwhbE/default.jpg」, 「長度」: 「3」,「狀態「:」serving「,」progress_speed「:」「,」progress「:」「,」ads「:」「,」pf「:」http://ping.aclst.com/ping.php/10754233/KMU0tzLwhbE?h=401301「,」h「:」e8e446dbd937a0d8f636fdc8d8bb9874「};

下載鏈接:http://www.youtube-mp3.org/get?ab=128&video_id=KMU0tzLwhbE&h=e8e446dbd937a0d8f636fdc8d8bb9874&r=1388680822878.1534658195

希望它能幫助,

+5

在GitHub上無編號:-( – Hanry

+0

你是如何通過自己的YouTube視頻的網址,該網站以獲取此響應信息? – Markoe7

+0

代碼不再可用,您是否還有您的代碼? –

0

發現這個

package main.java.com.thezujev.theyoutubepld.logic; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.HashMap; 
import java.util.Map; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.HttpVersion; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.params.HttpParams; 
import org.apache.http.params.HttpProtocolParams; 
import org.apache.http.params.SyncBasicHttpParams; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
/** 
* @author azujev 
* 
*/ 
public class YouTubeMP3 { 
    public static String[] getLink(String url) throws ClientProtocolException, IOException { 
     boolean passCode = false; 
     String h = ""; 
     String title = ""; 
     String result = ""; 
     String[] returnVal = {"",""}; 
     Map<String, String> jsonTable; 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpGet httpInitialGet = new HttpGet("http://www.youtube-mp3.org/api/pushItem/?item=http%3A//www.youtube.com/watch%3Fv%3D" + url + "&xy=_"); 
     httpInitialGet.addHeader("Accept-Location", "*"); 
     httpInitialGet.addHeader("Referrer", "http://www.youtube-mp3.org"); 
     HttpParams params = new SyncBasicHttpParams(); 
     HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
     HttpProtocolParams.setUserAgent(params, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"); 
     httpInitialGet.setParams(params); 
     HttpResponse firstResponse = httpClient.execute(httpInitialGet); 

     try { 
      if (firstResponse.getStatusLine().toString().contains("200")) { 
       passCode = true; 
      } 
     } finally { 
      httpInitialGet.releaseConnection(); 
     } 

     if (passCode) { 
      while (true) { 
       HttpGet httpStatusGet = new HttpGet("http://www.youtube-mp3.org/api/itemInfo/?video_id=" + url + "&adloc="); 
       httpStatusGet.addHeader("Accept-Location", "*"); 
       httpStatusGet.addHeader("Referrer", "http://www.youtube-mp3.org"); 
       httpStatusGet.setParams(params); 
       HttpResponse secondResponse = httpClient.execute(httpStatusGet); 
       HttpEntity secondEntity = secondResponse.getEntity(); 
       InputStream is = secondEntity.getContent(); 
       try { 
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); 
        StringBuilder sb = new StringBuilder(); 
        String line = null; 
        while ((line = reader.readLine()) != null) { 
         sb.append(line + "\n"); 
         } 
         is.close(); 
         result = sb.toString(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

       httpStatusGet.releaseConnection(); 
       result = result.replaceAll("\\}.*", "}"); 
       result = result.replaceAll(".*?\\{", "{"); 
       try { 
        JSONObject jsonData = new JSONObject(result); 
        JSONArray jsonArray = jsonData.names(); 
        JSONArray valArray = jsonData.toJSONArray(jsonArray); 
        jsonTable = new HashMap<String, String>(jsonArray.length()); 
        for (int i = 0; i < jsonArray.length(); i++) { 
         jsonTable.put(jsonArray.get(i).toString(), valArray.get(i).toString()); 
        } 
        if (jsonTable.get("status").equals("serving")) { 
         h = jsonTable.get("h"); 
         title = jsonTable.get("title"); 
         break; 
        } 
       } catch (JSONException e) { 
       // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

      } 
      returnVal[0] = "http://www.youtube-mp3.org/get?video_id=" + url + "&h=" + h; 
      returnVal[1] = title; 
      return returnVal; 
     } else { 
      //TODO: Error, vid not downloadable 
     } 
     return null; 
    } 
}