Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Youtube数据API:访问媒体流并播放(JAVA)_Java_Android_Youtube_Youtube Api - Fatal编程技术网

Youtube数据API:访问媒体流并播放(JAVA)

Youtube数据API:访问媒体流并播放(JAVA),java,android,youtube,youtube-api,Java,Android,Youtube,Youtube Api,我想访问youtube视频并使用自己的媒体播放器播放。我能够使用youtube数据API获取视频属性(标题、url等)。我可以访问视频流并使用我自己的媒体播放器(如Android媒体播放器)播放它吗。 我正在JAVA中尝试所有这些 提前感谢……) 你不能。请参阅此处,进一步了解API可以处理的内容: 如果你能从中获得一条输入流,谷歌根本不会从广告中获得任何收入。 但是,您可以解析从API获得的视频url页面,并在flash标记中查找真正的视频链接。/** /** * This work is

我想访问youtube视频并使用自己的媒体播放器播放。我能够使用youtube数据API获取视频属性(标题、url等)。我可以访问视频流并使用我自己的媒体播放器(如Android媒体播放器)播放它吗。 我正在JAVA中尝试所有这些


提前感谢……)

你不能。请参阅此处,进一步了解API可以处理的内容:
如果你能从中获得一条输入流,谷歌根本不会从广告中获得任何收入。

但是,您可以解析从API获得的视频url页面,并在flash标记中查找真正的视频链接。

/**
/**
 * This work is licensed under a Creative Commons Attribution 3.0 Unported
 * License (http://creativecommons.org/licenses/by/3.0/). This work is placed
 * into the public domain by the author.
 */
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.text.DecimalFormat;
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;

/**
 * Locally download a YouTube.com video.
 */
public class JavaYoutubeDownloader extends Formatter {

 private static final String scheme = "http";
 private static final String host = "www.youtube.com";
 private static final String YOUTUBE_WATCH_URL_PREFIX = scheme + "://" + host + "/watch?v=";
 private static final String ERROR_MISSING_VIDEO_ID = "Missing video id. Extract from " + YOUTUBE_WATCH_URL_PREFIX + "VIDEO_ID";
 private static final String DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";
 private static final String DEFAULT_ENCODING = "UTF-8";
 private static final String newline = System.getProperty("line.separator");
 private static final Logger log = Logger.getLogger(JavaYoutubeDownloader.class.getCanonicalName());
 private static final Logger rootlog = Logger.getLogger("");
 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 final int BUFFER_SIZE = 2048;
 private static final DecimalFormat commaFormatNoPrecision = new DecimalFormat("###,###");
 private static final double ONE_HUNDRED = 100;
 private static final double KB = 1024;

 private void usage(String error) {
  if (error != null) {
   System.err.println("Error: " + error);
  }
  System.err.println("usage: JavaYoutubeDownload VIDEO_ID");
  System.err.println();
  System.err.println("Options:");
  System.err.println("\t[-dir DESTINATION_DIR] - Specify output directory.");
  System.err.println("\t[-format FORMAT] - Format number" + newline + "\t\tSee http://en.wikipedia.org/wiki/YouTube#Quality_and_codecs");
  System.err.println("\t[-ua USER_AGENT] - Emulate a browser user agent.");
  System.err.println("\t[-enc ENCODING] - Default character encoding.");
  System.err.println("\t[-verbose] - Verbose logging for downloader component.");
  System.err.println("\t[-verboseall] - Verbose logging for all components (e.g. HttpClient).");
  System.exit(-1);
 }

 public static void main(String[] args) {
  try {
   new JavaYoutubeDownloader().run(args);
  } catch (Throwable t) {
   t.printStackTrace();
  }
 }

 private void run(String[] args) throws Throwable {
  setupLogging(Level.WARNING, Level.WARNING);

  String videoId = null;
  String outdir = ".";
  int format = 18;
  String encoding = DEFAULT_ENCODING;
  String userAgent = DEFAULT_USER_AGENT;

  for (int i = 0; i < args.length; i++) {
   String arg = args[i];

   // Options start with either -, --
   // Do not accept Windows-style args that start with / because of abs
   // paths on linux for file names.
   if (arg.charAt(0) == '-') {

    // For easier processing, convert any double dashes to
    // single dashes
    if (arg.length() > 1 && arg.charAt(1) == '-') {
     arg = arg.substring(1);
    }

    String larg = arg.toLowerCase();

    // Process the option
    if (larg.equals("-help") || larg.equals("-?") || larg.equals("-usage") || larg.equals("-h")) {
     usage(null);
    } else if (larg.equals("-verbose")) {
     setupLogging(Level.ALL, Level.WARNING);
    } else if (larg.equals("-verboseall")) {
     setupLogging(Level.ALL, Level.ALL);
    } else if (larg.equals("-dir")) {
     outdir = args[++i];
    } else if (larg.equals("-format")) {
     format = Integer.parseInt(args[++i]);
    } else if (larg.equals("-ua")) {
     userAgent = args[++i];
    } else if (larg.equals("-enc")) {
     encoding = args[++i];
    } else {
     usage("Unknown command line option " + args[i]);
    }
   } else {
    // Non-option (i.e. does not start with -, --

    videoId = arg;

    // Break if only the first non-option should be used.
    break;
   }
  }

  if (videoId == null) {
   usage(ERROR_MISSING_VIDEO_ID);
  }

  log.fine("Starting");

  if (videoId.startsWith(YOUTUBE_WATCH_URL_PREFIX)) {
   videoId = videoId.substring(YOUTUBE_WATCH_URL_PREFIX.length());
  }
  int a = videoId.indexOf('&');
  if (a != -1) {
   videoId = videoId.substring(0, a);
  }

  File outputDir = new File(outdir);
  String extension = getExtension(format);

  play(videoId, format, encoding, userAgent, outputDir, extension);

  log.fine("Finished");
 }

 private static String getExtension(int format) {
  switch (format) {
  case 18:
   return "mp4";
  default:
   throw new Error("Unsupported format " + format);
  }
 }

 private static void play(String videoId, int format, String encoding, String userAgent, File outputdir, String extension) throws Throwable {
  log.fine("Retrieving " + videoId);
  List<NameValuePair> qparams = new ArrayList<NameValuePair>();
  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);
  if (userAgent != null && userAgent.length() > 0) {
   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<NameValuePair> infoMap = new ArrayList<NameValuePair>();
    URLEncodedUtils.parse(infoMap, new Scanner(videoInfo), encoding);
    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("title")) {
      filename = val;
     } else if (key.equals("fmt_url_map")) {
      String[] formats = commaPattern.split(val);
      boolean found = false;
      for (String fmt : formats) {
       String[] fmtPieces = pipePattern.split(fmt);
       if (fmtPieces.length == 2) {
        int pieceFormat = Integer.parseInt(fmtPieces[0]);
        log.fine("Available format=" + pieceFormat);
        if (pieceFormat == format) {
         // found what we want
         downloadUrl = fmtPieces[1];
         found = true;
         break;
        }
       }
      }
      if (!found) {
       log.warning("Could not find video matching specified format, however some formats of the video do exist (use -verbose).");
      }
     }
    }

    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);
    } else {
     log.severe("Could not find video");
    }
   } else {
    log.severe("Did not receive content from youtube");
   }
  } else {
   log.severe("Could not contact youtube: " + response.getStatusLine());
  }
 }

 private static void downloadWithHttpClient(String userAgent, String downloadUrl, File outputfile) throws Throwable {
  HttpGet httpget2 = new HttpGet(downloadUrl);
  if (userAgent != null && userAgent.length() > 0) {
   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) {
   double length = entity2.getContentLength();
   if (length <= 0) {
    // Unexpected, but do not divide by zero
    length = 1;
   }
   InputStream instream2 = entity2.getContent();
   System.out.println("Writing " + commaFormatNoPrecision.format(length) + " bytes to " + outputfile);
   if (outputfile.exists()) {
    outputfile.delete();
   }
   FileOutputStream outstream = new FileOutputStream(outputfile);
   try {
    byte[] buffer = new byte[BUFFER_SIZE];
    double total = 0;
    int count = -1;
    int progress = 10;
    long start = System.currentTimeMillis();
    while ((count = instream2.read(buffer)) != -1) {
     total += count;
     int p = (int) ((total / length) * ONE_HUNDRED);
     if (p >= progress) {
      long now = System.currentTimeMillis();
      double s = (now - start) / 1000;
      int kbpers = (int) ((total / KB) / s);
      System.out.println(progress + "% (" + kbpers + "KB/s)");
      progress += 10;
     }
     outstream.write(buffer, 0, count);
    }
    outstream.flush();
   } finally {
    outstream.close();
   }
   System.out.println("Done");
  }
 }

 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<NameValuePair> qparams) throws URISyntaxException {
  URI uri = URIUtils.createURI(scheme, host, -1, "/" + path, URLEncodedUtils.format(qparams, DEFAULT_ENCODING), null);
  return uri;
 }

 private void setupLogging(Level myLevel, Level globalLevel) {
  changeFormatter(this);
  explicitlySetAllLogging(myLevel, globalLevel);
 }

 @Override
 public String format(LogRecord arg0) {
  return arg0.getMessage() + newline;
 }

 private static void changeFormatter(Formatter formatter) {
  Handler[] handlers = rootlog.getHandlers();
  for (Handler handler : handlers) {
   handler.setFormatter(formatter);
  }
 }

 private static void explicitlySetAllLogging(Level myLevel, Level globalLevel) {
  rootlog.setLevel(Level.ALL);
  for (Handler handler : rootlog.getHandlers()) {
   handler.setLevel(Level.ALL);
  }
  log.setLevel(myLevel);
  rootlog.setLevel(globalLevel);
 }

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

/**
 * <pre>
 * Exploded results from get_video_info:
 * 
 * fexp=909302
 * allow_embed=1
 * fmt_stream_map=35|http://v9.lscache8.c.youtube.com/videoplayback?ip=174.0.0.0&sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor&fexp=909302&algorithm=throttle-factor&itag=35&ipbits=8&burst=40&sver=3&expire=1294549200&key=yt1&signature=9E0A8E67154145BCADEBCF844CC155282548288F.2BBD0B2E125E3E533D07866C7AE91B38DD625D30&factor=1.25&id=4ba2193f7c9127d2||tc.v9.cache8.c.youtube.com,34|http://v6.lscache3.c.youtube.com/videoplayback?ip=174.0.0.0&sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor&fexp=909302&algorithm=throttle-factor&itag=34&ipbits=8&burst=40&sver=3&expire=1294549200&key=yt1&signature=6726793A7B041E6456B52C0972596D0D58974141.42B5A0573F62B85AEA7979E5EE1ADDD47EB9E909&factor=1.25&id=4ba2193f7c9127d2||tc.v6.cache3.c.youtube.com,18|http://v12.lscache7.c.youtube.com/videoplayback?ip=174.0.0.0&sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor&fexp=909302&algorithm=throttle-factor&itag=18&ipbits=8&burst=40&sver=3&expire=1294549200&key=yt1&signature=AE58398D4CC4D760C682D2A5B670B4047777FFF0.952E4FC4554E786FD937E7A89140E1F79B6DD8B7&factor=1.25&id=4ba2193f7c9127d2||tc.v12.cache7.c.youtube.com,5|http://v1.lscache7.c.youtube.com/videoplayback?ip=174.0.0.0&sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor&fexp=909302&algorithm=throttle-factor&itag=5&ipbits=8&burst=40&sver=3&expire=1294549200&key=yt1&signature=43434DCB6CFC463FF4522D9EE7CD019FE47237B1.C60A9522E361130938663AF2DAD83A5C2821AF5C&factor=1.25&id=4ba2193f7c9127d2||tc.v1.cache7.c.youtube.com
 * fmt_url_map=35|http://v9.lscache8.c.youtube.com/videoplayback?ip=174.0.0.0&sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor&fexp=909302&algorithm=throttle-factor&itag=35&ipbits=8&burst=40&sver=3&expire=1294549200&key=yt1&signature=9E0A8E67154145BCADEBCF844CC155282548288F.2BBD0B2E125E3E533D07866C7AE91B38DD625D30&factor=1.25&id=4ba2193f7c9127d2,34|http://v6.lscache3.c.youtube.com/videoplayback?ip=174.0.0.0&sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor&fexp=909302&algorithm=throttle-factor&itag=34&ipbits=8&burst=40&sver=3&expire=1294549200&key=yt1&signature=6726793A7B041E6456B52C0972596D0D58974141.42B5A0573F62B85AEA7979E5EE1ADDD47EB9E909&factor=1.25&id=4ba2193f7c9127d2,18|http://v12.lscache7.c.youtube.com/videoplayback?ip=174.0.0.0&sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor&fexp=909302&algorithm=throttle-factor&itag=18&ipbits=8&burst=40&sver=3&expire=1294549200&key=yt1&signature=AE58398D4CC4D760C682D2A5B670B4047777FFF0.952E4FC4554E786FD937E7A89140E1F79B6DD8B7&factor=1.25&id=4ba2193f7c9127d2,5|http://v1.lscache7.c.youtube.com/videoplayback?ip=174.0.0.0&sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor&fexp=909302&algorithm=throttle-factor&itag=5&ipbits=8&burst=40&sver=3&expire=1294549200&key=yt1&signature=43434DCB6CFC463FF4522D9EE7CD019FE47237B1.C60A9522E361130938663AF2DAD83A5C2821AF5C&factor=1.25&id=4ba2193f7c9127d2
 * 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=AASZXXGQtTEDKwAw
 * 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=1294526523
 * 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=vjVQa1PpcFNhI3jvw6hfEHivcKK-XY5gb-iszDMrooA=
 * thumbnail_url=http://i4.ytimg.com/vi/S6IZP3yRJ9I/default.jpg
 * title=The Story of Our Unenslavement - Animated
 * </pre>
 */
*本作品根据知识共享署名3.0 Unported获得许可 *许可证(http://creativecommons.org/licenses/by/3.0/). 这件作品被放置在 *由作者进入公共领域。 */ 导入java.io.BufferedReader; 导入java.io.File; 导入java.io.FileOutputStream; 导入java.io.IOException; 导入java.io.InputStream; 导入java.io.InputStreamReader; 导入java.io.Reader; 导入java.io.StringWriter; 导入java.io.UnsupportedEncodingException; 导入java.io.Writer; 导入java.net.URI; 导入java.net.URISyntaxException; 导入java.text.DecimalFormat; 导入java.util.ArrayList; 导入java.util.List; 导入java.util.Scanner; 导入java.util.logging.Formatter; 导入java.util.logging.Handler; 导入java.util.logging.Level; 导入java.util.logging.LogRecord; 导入java.util.logging.Logger; 导入java.util.regex.Pattern; 导入org.apache.http.HttpEntity; 导入org.apache.http.HttpResponse; 导入org.apache.http.NameValuePair; 导入org.apache.http.client.CookieStore; 导入org.apache.http.client.HttpClient; 导入org.apache.http.client.methods.HttpGet; 导入org.apache.http.client.protocol.ClientContext; 导入org.apache.http.client.utils.URIUtils; 导入org.apache.http.client.utils.URLEncodedUtils; 导入org.apache.http.impl.client.BasicCookieStore; 导入org.apache.http.impl.client.DefaultHttpClient; 导入org.apache.http.message.BasicNameValuePair; 导入org.apache.http.protocol.BasicHttpContext; 导入org.apache.http.protocol.HttpContext; /** *本地下载YouTube.com视频。 */ 公共类JavaYoutubeDownloader扩展格式化程序{ 私有静态最终字符串scheme=“http”; 私有静态最终字符串host=“www.youtube.com”; 私有静态最终字符串YOUTUBE_WATCH_URL_PREFIX=scheme+“:/“+host+”/WATCH?v=“; 私有静态最终字符串错误\u MISSING\u VIDEO\u ID=“MISSING VIDEO ID.摘录自”+YOUTUBE\u WATCH\u URL\u PREFIX+“VIDEO\u ID”; 私有静态最终字符串DEFAULT_USER_AGENT=“Mozilla/5.0(Windows;U;Windows NT 6.1;en-US;rv:1.9.2.13)Gecko/20101203 Firefox/3.6.13”; 私有静态最终字符串默认值_ENCODING=“UTF-8”; 私有静态最终字符串newline=System.getProperty(“line.separator”); 私有静态最终记录器log=Logger.getLogger(JavaYoutubeDownloader.class.getCanonicalName()); 私有静态最终记录器rootlog=Logger.getLogger(“”); 私有静态最终模式commaptern=Pattern.compile(“,”); 私有静态最终模式pipePattern=Pattern.compile(“\\\\”); 私有静态最终字符[]非法\u文件名\u字符={'/'、'\n'、'\r'、'\t'、'\0'、'\f'、''''、'?'、'*'、'\\'、'\'、''\'、'\'、'\'、':'}; 私有静态最终整数缓冲区大小=2048; 私有静态最终判定格式commaFormatNoPrecision=新判定格式(“####,####”); 私人静态最终双倍一百=100; 私有静态最终双KB=1024; 私有void用法(字符串错误){ if(错误!=null){ System.err.println(“错误:+错误”); } System.err.println(“用法:JavaYoutubeDownload VIDEO_ID”); System.err.println(); System.err.println(“选项:”); System.err.println(“\t[-dir DESTINATION\u dir]-指定输出目录”); System.err.println(“\t[-format-format]-格式编号“+换行符+”\t\tSeehttp://en.wikipedia.org/wiki/YouTube#Quality_and_codecs"); System.err.println(“\t[-ua USER\u AGENT]-模拟浏览器用户代理”); System.err.println(“\t[-enc ENCODING]-默认字符编码”); System.err.println(“\t[-verbose]-下载程序组件的详细日志记录”); System.err.println(“\t[-verboseall]-所有组件(例如HttpClient)的详细日志记录”); 系统退出(-1); } 公共静态void main(字符串[]args){ 试一试{ 新的JavaYoutubeDownloader().run(args); }捕获(可丢弃的t){ t、 printStackTrace(); } } 私有void运行(字符串[]args)抛出可丢弃的{ 设置日志记录(Level.WARNING,Level.WARNING); 字符串videoId=null; 字符串outdir=“.”; int格式=18; 字符串编码=默认的_编码; 字符串userAgent=默认的用户代理; 对于(int i=0;i1&&arg.charAt(1)='-'){ arg=arg.子字符串(1); } String larg=arg.toLowerCase(); //处理选项 if(larg.equals(“-help”)| larg.equals(“-?”)| larg.equals(“-usage”)| larg.equals(“-h”)){ 用法(空); }else if(larg.equals(“-verbose”)){ 设置日志记录(Level.ALL、Level.WARNING); }else if(larg.equals(“-verboseall”)){ 设置日志记录(Level.ALL,Level.ALL); }else if(大等于(“-dir”)){ outdir=args[++i]; }else if(大等于(“-format”)){ format=Integer.parseInt(args[++i]); }如果(大等于(“-ua”)){ userAgent=args[++i]; }else if(大等于(“-enc”)){ 编码=args[++i]; }否则{ 用法(“未知命令行选项”+args[i]); } }否则{ //非选项(即不以-开头-- videoId=arg; //打开时断开