如何在java上从youtube下载video?

如何在Java上从youtube下载video? 需要类(或代码片段)来描述如何做到这一点。 谢谢。

import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.StringWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.logging.Formatter; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; import java.util.regex.Pattern; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.CookieStore; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.protocol.ClientContext; import org.apache.http.client.utils.URIUtils; import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.HttpContext; public class JavaYoutubeDownloader { public static String newline = System.getProperty("line.separator"); private static final Logger log = Logger.getLogger(JavaYoutubeDownloader.class.getCanonicalName()); private static final Level defaultLogLevelSelf = Level.FINER; private static final Level defaultLogLevel = Level.WARNING; private static final Logger rootlog = Logger.getLogger(""); private static final String scheme = "http"; private static final String host = "www.youtube.com"; private static final Pattern commaPattern = Pattern.compile(","); private static final Pattern pipePattern = Pattern.compile("\\|"); private static final char[] ILLEGAL_FILENAME_CHARACTERS = { '/', '\n', '\r', '\t', '\0', '\f', '`', '?', '*', '\\', '<', '>', '|', '\"', ':' }; private static void usage(String error) { if (error != null) { System.err.println("Error: " + error); } System.err.println("usage: JavaYoutubeDownload VIDEO_ID DESTINATION_DIRECTORY"); System.exit(-1); } public static void main(String[] args) { if (args == null || args.length == 0) { usage("Missing video id. Extract from http://www.youtube.com/watch?v=VIDEO_ID"); } try { setupLogging(); log.fine("Starting"); String videoId = null; String outdir = "."; // TODO Ghetto command line parsing if (args.length == 1) { videoId = args[0]; } else if (args.length == 2) { videoId = args[0]; outdir = args[1]; } int format = 18; // http://en.wikipedia.org/wiki/YouTube#Quality_and_codecs String encoding = "UTF-8"; String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13"; File outputDir = new File(outdir); String extension = getExtension(format); play(videoId, format, encoding, userAgent, outputDir, extension); } catch (Throwable t) { t.printStackTrace(); } log.fine("Finished"); } private static String getExtension(int format) { // TODO return "mp4"; } private static void play(String videoId, int format, String encoding, String userAgent, File outputdir, String extension) throws Throwable { log.fine("Retrieving " + videoId); List qparams = new ArrayList(); qparams.add(new BasicNameValuePair("video_id", videoId)); qparams.add(new BasicNameValuePair("fmt", "" + format)); URI uri = getUri("get_video_info", qparams); CookieStore cookieStore = new BasicCookieStore(); HttpContext localContext = new BasicHttpContext(); localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(uri); httpget.setHeader("User-Agent", userAgent); log.finer("Executing " + uri); HttpResponse response = httpclient.execute(httpget, localContext); HttpEntity entity = response.getEntity(); if (entity != null && response.getStatusLine().getStatusCode() == 200) { InputStream instream = entity.getContent(); String videoInfo = getStringFromInputStream(encoding, instream); if (videoInfo != null && videoInfo.length() > 0) { List infoMap = new ArrayList(); URLEncodedUtils.parse(infoMap, new Scanner(videoInfo), encoding); String token = null; String downloadUrl = null; String filename = videoId; for (NameValuePair pair : infoMap) { String key = pair.getName(); String val = pair.getValue(); log.finest(key + "=" + val); if (key.equals("token")) { token = val; } else if (key.equals("title")) { filename = val; } else if (key.equals("fmt_url_map")) { String[] formats = commaPattern.split(val); for (String fmt : formats) { String[] fmtPieces = pipePattern.split(fmt); if (fmtPieces.length == 2) { // in the end, download somethin! downloadUrl = fmtPieces[1]; int pieceFormat = Integer.parseInt(fmtPieces[0]); if (pieceFormat == format) { // found what we want downloadUrl = fmtPieces[1]; break; } } } } } filename = cleanFilename(filename); if (filename.length() == 0) { filename = videoId; } else { filename += "_" + videoId; } filename += "." + extension; File outputfile = new File(outputdir, filename); if (downloadUrl != null) { downloadWithHttpClient(userAgent, downloadUrl, outputfile); } } } } private static void downloadWithHttpClient(String userAgent, String downloadUrl, File outputfile) throws Throwable { HttpGet httpget2 = new HttpGet(downloadUrl); httpget2.setHeader("User-Agent", userAgent); log.finer("Executing " + httpget2.getURI()); HttpClient httpclient2 = new DefaultHttpClient(); HttpResponse response2 = httpclient2.execute(httpget2); HttpEntity entity2 = response2.getEntity(); if (entity2 != null && response2.getStatusLine().getStatusCode() == 200) { long length = entity2.getContentLength(); InputStream instream2 = entity2.getContent(); log.finer("Writing " + length + " bytes to " + outputfile); if (outputfile.exists()) { outputfile.delete(); } FileOutputStream outstream = new FileOutputStream(outputfile); try { byte[] buffer = new byte[2048]; int count = -1; while ((count = instream2.read(buffer)) != -1) { outstream.write(buffer, 0, count); } outstream.flush(); } finally { outstream.close(); } } } private static String cleanFilename(String filename) { for (char c : ILLEGAL_FILENAME_CHARACTERS) { filename = filename.replace(c, '_'); } return filename; } private static URI getUri(String path, List qparams) throws URISyntaxException { URI uri = URIUtils.createURI(scheme, host, -1, "/" + path, URLEncodedUtils.format(qparams, "UTF-8"), null); return uri; } private static void setupLogging() { changeFormatter(new Formatter() { @Override public String format(LogRecord arg0) { return arg0.getMessage() + newline; } }); explicitlySetAllLogging(Level.FINER); } private static void changeFormatter(Formatter formatter) { Handler[] handlers = rootlog.getHandlers(); for (Handler handler : handlers) { handler.setFormatter(formatter); } } private static void explicitlySetAllLogging(Level level) { rootlog.setLevel(Level.ALL); for (Handler handler : rootlog.getHandlers()) { handler.setLevel(defaultLogLevelSelf); } log.setLevel(level); rootlog.setLevel(defaultLogLevel); } private static String getStringFromInputStream(String encoding, InputStream instream) throws UnsupportedEncodingException, IOException { Writer writer = new StringWriter(); char[] buffer = new char[1024]; try { Reader reader = new BufferedReader(new InputStreamReader(instream, encoding)); int n; while ((n = reader.read(buffer)) != -1) { writer.write(buffer, 0, n); } } finally { instream.close(); } String result = writer.toString(); return result; } } /** * 
 * Exploded results from get_video_info: * * fexp=90... * allow_embed=1 * fmt_stream_map=35|http://v9.lscache8... * fmt_url_map=35|http://v9.lscache8... * allow_ratings=1 * keywords=Stefan Molyneux,Luke Bessey,anarchy,stateless society,giant stone cow,the story of our unenslavement,market anarchy,voluntaryism,anarcho capitalism * track_embed=0 * fmt_list=35/854x480/9/0/115,34/640x360/9/0/115,18/640x360/9/0/115,5/320x240/7/0/0 * author=lukebessey * muted=0 * length_seconds=390 * plid=AA... * ftoken=null * status=ok * watermark=http://s.ytimg.com/yt/swf/logo-vfl_bP6ud.swf,http://s.ytimg.com/yt/swf/hdlogo-vfloR6wva.swf * timestamp=12... * has_cc=False * fmt_map=35/854x480/9/0/115,34/640x360/9/0/115,18/640x360/9/0/115,5/320x240/7/0/0 * leanback_module=http://s.ytimg.com/yt/swfbin/leanback_module-vflJYyeZN.swf * hl=en_US * endscreen_module=http://s.ytimg.com/yt/swfbin/endscreen-vflk19iTq.swf * vq=auto * avg_rating=5.0 * video_id=S6IZP3yRJ9I * token=vPpcFNh... * thumbnail_url=http://sofzh.miximages.com/java/default.jpg * title=The Story of Our Unenslavement - Animated * 

*/

我知道我迟到了。 但是这段代码可能对某些人有用。 所以我在这里附上它。

使用以下java代码从YouTube下载video。

 package com.mycompany.ytd; import java.io.File; import java.net.URL; import com.github.axet.vget.VGet; /** * * @author Manindar */ public class YTD { public static void main(String[] args) { try { String url = "https://www.youtube.com/watch?v=s10ARdfQUOY"; String path = "D:\\Manindar\\YTD\\"; VGet v = new VGet(new URL(url), new File(path)); v.download(); } catch (Exception e) { throw new RuntimeException(e); } } } 

POM.XML文件中添加以下依赖

   com.github.axet vget 1.1.33  

希望这会有用。

您可以在此处查看API

关于格式(mp4或flv)决定您要使用的URL 。 然后使用本教程下载video并将其保存到本地目录中。

ytd2是一款function齐全的YouTubevideo下载器。 如果您想了解它是如何完成的,请查看其源代码 。

参考: Youtubevideo下载(Android / Java)

 new YouTubePageStreamUriGetter().execute("https://www.youtube.com/watch?v=4GuqB1BQVr4"); 

initTypeMap(); 先叫

 private static final HashMap typeMap = new HashMap(); class Meta { public String num; public String type; public String ext; Meta(String num, String ext, String type) { this.num = num; this.ext = ext; this.type = type; } } class Video { public String ext = ""; public String type = ""; public String url = ""; Video(String ext, String type, String url) { this.ext = ext; this.type = type; this.url = url; } } public ArrayList 

有youtube数据api https://developers.google.com/youtube/v3/code_samples/java

你可以用它来通过java,php和更多技术下载youtubevideo。