我知道通过 VGet 可以轻松下载YouTube视频。但是,我只想获得下载链接以在我的应用程序的其余部分(Java Swing)中使用它。可以使用VGet完成吗?
请允许任何人帮助我。?
谢谢。
答案 0 :(得分:1)
这是我的解决方案,(更好的解决方案非常感谢)
在参考VGet之后,我编写了自己的方法来获取youtube网址(例如,https:www.youtube.com/watch?v = ID)并提供下载链接(例如,https:r2 --- sn -ipoxu-u2xl.googlevideo.com/videoplayback?upn=l0CJXtFDQog&itag=43&signature=0C6...&mt=1436339343)。这是代码,
List<String> urlList = new ArrayList<String>();
Pattern urlencod = Pattern.compile("\"url_encoded_fmt_stream_map\":\"([^\"]*)\"");
Matcher urlencodMatch = urlencod.matcher(**html**);
if (urlencodMatch.find()) {
String url_encoded_fmt_stream_map;
url_encoded_fmt_stream_map = urlencodMatch.group(1);
Pattern encod = Pattern.compile("url=(.*)");
Matcher encodMatch = encod.matcher(url_encoded_fmt_stream_map);
if (encodMatch.find()) {
String sline = encodMatch.group(1);
String[] urlStrings = sline.split("url=");
for (String urlString : urlStrings) {
String url = null;
urlString = StringEscapeUtils.unescapeJava(urlString);
Pattern link = Pattern.compile("([^&,]*)[&,]");
Matcher linkMatch = link.matcher(urlString);
if (linkMatch.find()) {
url = linkMatch.group(1);
url = URLDecoder.decode(url, UTF8);
}
urlList.add(url);
}
}
}
其中,html是使用的字符串,
HttpURLConnection conn = null;
StringBuilder contents = new StringBuilder();
try {
conn = (HttpURLConnection)new URL("https://www.youtube.com/watch?v=SYOarZKipnU").openConnection();
conn.setConnectTimeout(CONNECT_TIMEOUT);
conn.setReadTimeout(READ_TIMEOUT);
InputStream is = conn.getInputStream();
String enc = conn.getContentEncoding();
if (enc == null) {
Pattern p = Pattern.compile("charset=(.*)");
Matcher m = p.matcher(conn.getHeaderField("Content-Type"));
if (m.find()) {
enc = m.group(1);
}
}
if (enc == null)
enc = "UTF-8";
BufferedReader br = new BufferedReader(new InputStreamReader(is, enc));
String line = null;
while ((line = br.readLine()) != null) {
contents.append(line);
contents.append("\n");
}
}catch(IOException e){
}
return contents.toString();
}
谢谢你们......
答案 1 :(得分:0)
来自https://github.com/axet/vget
package com.github.axet.vget;
import java.io.File;
import java.net.URL;
public class DirectDownload {
public static void main(String[] args) {
try {
// ex: http://www.youtube.com/watch?v=Nj6PFaDmp6c
String url = args[0];
// ex: "/Users/axet/Downloads"
String path = args[1];
VGet v = new VGet(new URL(url), new File(path));
v.download();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
还来自https://github.com/axet/vget
import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import com.github.axet.vget.info.VGetParser;
import com.github.axet.vget.info.VideoInfo;
import com.github.axet.vget.vhs.VimeoInfo;
import com.github.axet.vget.vhs.YoutubeInfo;
import com.github.axet.wget.info.DownloadInfo;
import com.github.axet.wget.info.DownloadInfo.Part;
import com.github.axet.wget.info.DownloadInfo.Part.States;
public class AppManagedDownload {
VideoInfo info;
long last;
public void run(String url, File path) {
try {
AtomicBoolean stop = new AtomicBoolean(false);
Runnable notify = new Runnable() {
@Override
public void run() {
VideoInfo i1 = info;
DownloadInfo i2 = i1.getInfo();
// notify app or save download state
// you can extract information from DownloadInfo info;
switch (i1.getState()) {
case EXTRACTING:
case EXTRACTING_DONE:
case DONE:
if (i1 instanceof YoutubeInfo) {
YoutubeInfo i = (YoutubeInfo) i1;
System.out.println(i1.getState() + " " + i.getVideoQuality());
} else if (i1 instanceof VimeoInfo) {
VimeoInfo i = (VimeoInfo) i1;
System.out.println(i1.getState() + " " + i.getVideoQuality());
} else {
System.out.println("downloading unknown quality");
}
break;
case RETRYING:
System.out.println(i1.getState() + " " + i1.getDelay());
break;
case DOWNLOADING:
long now = System.currentTimeMillis();
if (now - 1000 > last) {
last = now;
String parts = "";
List<Part> pp = i2.getParts();
if (pp != null) {
// multipart download
for (Part p : pp) {
if (p.getState().equals(States.DOWNLOADING)) {
parts += String.format("Part#%d(%.2f) ", p.getNumber(), p.getCount()
/ (float) p.getLength());
}
}
}
System.out.println(String.format("%s %.2f %s", i1.getState(),
i2.getCount() / (float) i2.getLength(), parts));
}
break;
default:
break;
}
}
};
URL web = new URL(url);
// [OPTIONAL] limit maximum quality, or do not call this function if
// you wish maximum quality available.
//
// if youtube does not have video with requested quality, program
// will raise en exception.
VGetParser user = null;
// create proper html parser depends on url
user = VGet.parser(web);
// download maximum video quality from youtube
// user = new YouTubeQParser(YoutubeQuality.p480);
// download mp4 format only, fail if non exist
// user = new YouTubeMPGParser();
// create proper videoinfo to keep specific video information
info = user.info(web);
VGet v = new VGet(info, path);
// [OPTIONAL] call v.extract() only if you d like to get video title
// or download url link
// before start download. or just skip it.
v.extract(user, stop, notify);
System.out.println("Title: " + info.getTitle());
System.out.println("Download URL: " + info.getInfo().getSource());
v.download(user, stop, notify);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
AppManagedDownload e = new AppManagedDownload();
// ex: http://www.youtube.com/watch?v=Nj6PFaDmp6c
String url = args[0];
// ex: /Users/axet/Downloads/
String path = args[1];
e.run(url, new File(path));
}
}
答案 2 :(得分:0)
我找到的最简单的解决方案如下, YouTube视频的HTML页面中包含该文件的URL。
在该页面中搜索代码类似&#39; url = https%3A%2F%2Fr12 --- sn-5hne6nse.googlevideo.com%2&#39;
这不是完整的网址,但它指向某个分辨率的一个条目。对于从可用的最高分辨率开始的每个分辨率,重复此代码。我做了一些测试,找到了使文件播放所需的最低URL,如下所示
所以如果你想要720p版本,那么
我发现代码包含在&#39; quality = hd720 \&#39;并且&#39; ratebypass%3Dyes&#39;如果粘贴到浏览器中播放所需的文件,那么如果你再使用,
String result = str.substring(str.indexOf(&#34; quality = hd720&#34;),str.indexOf(&#34; ratebypass%3Dyes&#34;));
假设有720可用将拉出播放文件所需的URL。
使用Uri.decode(url)解码URL以删除这些%代码
嘿presto,在Java中用作URL的是什么会下载文件。