服务器之家

服务器之家 > 正文

java获取优酷视频地址示例

时间:2019-11-12 14:20     来源/作者:java教程网

代码如下:


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

 

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;


public class VideoTools
{
 public static void main(String[] args) throws Exception
 {
  VideoTools manager = new VideoTools();
  String jsonUrl = manager.getVideoUrl("http://v.youku.com/v_show/id_XMTgzNDA5OTMy.html");
  System.out.println(jsonUrl.split("#").length);
 }

 public String getVideoUrl(String htmlUrl) throws JSONException{
  //正则表达式解析地址,取id
  Pattern p = Pattern.compile(".*id_(\\w+)\\.html");
  String u = htmlUrl;
  Matcher m = p.matcher(u);
  String id = "";
  if (m.find()) {
   id = m.group(1);
  }
  System.out.println(id);

//  String id="XMTgzNDA5OTMy";
  String s = getContent("http://v.youku.com/player/getPlayList/VideoIDS/"+id);
  JSONObject jsobj = new JSONObject();
  jsobj =jsobj.fromObject(s);
  JSONArray jsonarr = jsobj.getJSONArray("data");
  System.out.println(jsobj);
  JSONObject obj1 = jsonarr.getJSONObject(0);
  String title= obj1.getString("title");
  double seed = obj1.getDouble("seed");
  JSONObject obj2 = obj1.getJSONObject("streamfileids");
  String mp4id = null;
  String flvid = null;
  String format = null;
  try
  {
   mp4id = obj2.getString("mp4");
   format = "mp4";
  } catch (JSONException e)
  {
   System.out.println("没有MP4格式");
   try
   {
    flvid = obj2.getString("flv");
    format = "flv";
   } catch (JSONException e1)
   {
    System.out.println("没有FLV格式");
    return "";
   }
  }
  String realfileid = null;
  if (format.equals("mp4"))
  {
   realfileid = getFileID(mp4id, seed);
  } else
  {
   realfileid = getFileID(flvid, seed);
  }

  String idLeft = realfileid.substring(0, 8);
  String idRight = realfileid.substring(10);

  String sid = genSid();
  JSONObject obj3 = obj1.getJSONObject("segs");
  JSONArray mp4arr = obj3.getJSONArray(format);
  String VideoUrl="";
  for (int i = 0; i < mp4arr.size(); i++)
  {
   JSONObject o = mp4arr.getJSONObject(i);
   String k = o.getString("k");
   String url = "http://f.youku.com/player/getFlvPath/sid/" + sid + "_" + String.format("%1$02X", i) + "/st/" + format
     + "/fileid/" + idLeft + String.format("%1$02X", i) + idRight + "?K=" + k;
   System.out.println(url);
   VideoUrl+=url+"#";
  }
  return VideoUrl;
 }

 private static String getFileID(String fileid, double seed)
 {
  String mixed = getFileIDMixString(seed);
  String[] ids = fileid.split("\\*");
  StringBuilder realId = new StringBuilder();
  int idx;
  for (int i = 0; i < ids.length; i++)
  {
   idx = Integer.parseInt(ids[i]);
   realId.append(mixed.charAt(idx));
  }
  return realId.toString();
 }

 public static String genSid()
 {
  int i1 = (int) (1000 + Math.floor(Math.random() * 999));
  int i2 = (int) (1000 + Math.floor(Math.random() * 9000));
  return System.currentTimeMillis() + "" + i1 + "" + i2;
 }

 private static String getFileIDMixString(double seed)
 {
  StringBuilder mixed = new StringBuilder();
  StringBuilder source = new StringBuilder("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\\:._-1234567890");
  int index, len = source.length();
  for (int i = 0; i < len; ++i)
  {
   seed = (seed * 211 + 30031) % 65536;
   index = (int) Math.floor(seed / 65536 * source.length());
   mixed.append(source.charAt(index));
   source.deleteCharAt(index);
  }
  return mixed.toString();
 }

 public static String getContent(String strUrl) {
  try {

   URL url = new URL(strUrl);
   BufferedReader br = new BufferedReader(new InputStreamReader(
     url.openStream()));
   String s = "";
   StringBuffer sb = new StringBuffer("");
   while ((s = br.readLine()) != null) {
    sb.append(s);
   }
   br.close();
   return sb.toString();
  } catch (Exception e) {
   return "error open url:" + strUrl;
  }

 }
}

 

标签:

相关文章

热门资讯

玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
Nginx服务器究竟是怎么执行PHP项目
Nginx服务器究竟是怎么执行PHP项目 2019-05-24
配置IIS网站web服务器的安全策略配置解决方案
配置IIS网站web服务器的安全策略配置解决方案 2019-05-23
返回顶部