我正在尝试制作可以将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元素)显示chrome中的链接和视图源(ctrl + u)选项给了我与使用java下载页面相同的结果。我怎样才能获得该链接? 我试图使用JSoap获取数据,但我需要的那些数据不会立即加载到页面上,所以我无法获取它们。
下一个代码用于下载网页内容......
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();
}
答案 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;
}
}
答案 1 :(得分:0)
关于YouTube API的服务条款
https://developers.google.com/youtube/terms/developer-policies
您不能:
分离,隔离或修改任何音频或视频组件 通过YouTube API提供的YouTube视听内容;
分别推广任何YouTube的音频或视频组件 通过YouTube API提供的视听内容;
(来源:https://stackoverflow.com/a/26552805/5645656)
可以使用其他服务。考虑下面的代码,该代码使用www.320youtube.com。方法var userClaims = await _userManager.GetClaimsAsync(user);
将以字节数组的形式返回视频声音的MP3文件:
youtubeToMP3
答案 2 :(得分:-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 M:
我在Github上删除了我的帐户。但是,如果您使用的是Google Chrome,则可以在网络透视图中使用开发者控制台。所以你可以反向设计下载链接 我就是这样做的。
示例:
Youtube网址: http://www.youtube.com/watch?v=KMU0tzLwhbE;
请求内容正文中的webservice:KMU0tzLwhbE
来自webservice的回复:info = {“title”:“开发者”,“图片”:“http://i.ytimg.com/vi/KMU0tzLwhbE/default.jpg”,“长度”:“3”,“状态”:“服务”,“progress_speed”: “”,“progress”:“”,“ads”:“”,“pf”:“http://ping.aclst.com/ping.php/10754233/KMU0tzLwhbE?h=401301”,“h”:“e8e446dbd937a0d8f636fdc8d8bb9874”};
希望它有所帮助,