读书人

JAVA提取优酷等视频因特网址的视频信息

发布时间: 2012-10-08 19:54:56 作者: rapoo

JAVA提取优酷等视频网址的视频信息(持续更新中......)

支持以下网址:优酷、土豆、酷6、6间房、新浪、搜狐、56(我乐)、凤凰视频、音悦台MV播放页面的解析,解析出来的属性包括:视频标题、视频缩略图、视频简介、视频时长、视频来源、视频页面地址、视频FLASH地址、页面嵌入HTML代码。以后会继续更新支持更多的视频网站,附件中VideoExtract-1.0.jar为引入包,VideoExtract-sources-1.0.jar为源码包。

版本更新说明:
1、2011-09-12 创建版本,支持优酷、土豆、酷6、6间房、新浪、搜狐、56(我乐)视频播放页面的信息解析
2、2011-09-13 新加对凤凰视频的支持,链接地址不在支持的列表中时返回原链接地址以及链接的页面标题

3、2011-09-23 新加对音悦台MV播放也的解析支持http://www.yinyuetai.com

?

使用方法如下:

1、视频对象类org.osm.video.Video.java

package org.osm.video;import java.io.Serializable;public class Video implements Serializable {private static final long serialVersionUID = -6220973207083491817L;private String title = "";// 视频标题private String thumbnail = "";// 视频缩略图private String summary = "";// 视频简介private String time = "";// 视频时长private String source = "";// 视频来源private String pageUrl = "";// 视频页面地址private String flashUrl = "";// 视频FLASH地址private String htmlCode = "";// 视频HTML代码public String getTitle() {if (null == title) {return "";} else {return title;}}public void setTitle(String title) {this.title = title;}public String getThumbnail() {if (null == thumbnail) {return "";} else {return thumbnail;}}public void setThumbnail(String thumbnail) {this.thumbnail = thumbnail;}public String getSummary() {if (null == summary) {return "";} else {return summary;}}public void setSummary(String summary) {this.summary = summary;}public String getTime() {if (null == time) {return "";} else {return time;}}public void setTime(String time) {this.time = time;}public String getSource() {if (null == source) {return "";} else {return source;}}public void setSource(String source) {this.source = source;}public String getPageUrl() {if (null == pageUrl) {return "";} else {return pageUrl;}}public void setPageUrl(String pageUrl) {this.pageUrl = pageUrl;}public String getFlashUrl() {if (null == flashUrl) {return "";} else {return flashUrl;}}public void setFlashUrl(String flashUrl) {this.flashUrl = flashUrl;}public String getHtmlCode() {if (null == htmlCode) {return "";} else {return htmlCode;}}public void setHtmlCode(String htmlCode) {this.htmlCode = htmlCode;}}

?

2、测试类代码

?

String url = "http://v.youku.com/v_show/id_XMzAyMjE1Nzgw.html";// String url = "http://www.tudou.com/programs/view/Zkazx5iaUSQ/";// String url = "http://v.ku6.com/special/show_4024167/Dwq_4xKT5Go4f4F6.html";// String url = "http://v.ku6.com/show/bFx2VCEiF15U53E6.html";// String url = "http://6.cn/watch/14757577.html";// String url = "http://www.56.com/u37/v_NjE3OTQ4NTg.html";// String url = "http://video.sina.com.cn/p/news/s/v/2011-09-11/133861482545.html";// String url = "http://tv.sohu.com/20110912/n319076906.shtml";// String url = "http://v.ifeng.com/documentary/discovery/201109/ed52d159-a3f4-4969-972d-c9e6d51d00ff.shtml";// String url = "http://www.yinyuetai.com/video/271439";// String url = "http://www.baidu.com/";Video video = VideoUtil.getVideoInfo(url);System.out.println("视频标题:" + video.getTitle());System.out.println("视频地址:" + video.getFlashUrl());System.out.println("视频时长:" + video.getTime());System.out.println("视频来源:" + video.getSource());System.out.println("视频简介:" + video.getSummary());System.out.println("视频缩略图:" + video.getThumbnail());System.out.println("视频原始地址:" + video.getPageUrl());System.out.println("视频网页代码:" + video.getHtmlCode());
at org.osm.jsoup.helper.Validate.isTrue(Validate.java:45)
at org.osm.jsoup.examples.ListLinks.main(ListLinks.java:16)at org.osm.jsoup.helper.Validate.isTrue(Validate.java:45)
at org.osm.jsoup.examples.ListLinks.main(ListLinks.java:16)
视频地址贴一下 public static Video getNetEaseVideo(String url) throws Exception {Document doc = getURLContent(url);String content = doc.html();content = content.substring(content.indexOf("topicid"));content = content.substring(0,content.indexOf("</script>"));content = content.replaceAll("\"","").replaceAll("\n","").trim();String contents[] = content.split(";");/** * 视频标题 */String title = getScriptVarByName("title=",contents);String topicid = getScriptVarByName("topicid=",contents);String vid = getScriptVarByName("vid=",contents);/** * 视频缩略图 */String imgpath = getScriptVarByName("imgpath=",contents);/** * 视频地址 */String flash = "http://img1.cache.netease.com/flvplayer081128/~false~"+topicid+"_"+vid+"~"+imgpath.substring(7,imgpath.length()-4)+"~.swf";Video video = new Video();video.setTitle(title);video.setThumbnail(imgpath);video.setFlashUrl(flash);video.setSource("网易视频");video.setPageUrl(url);// video.setSummary(summary);video.setHtmlCode(getHtmlCode(flash));return video;}private static String getScriptVarByName(String name,String contents[]) {String text = "";for(String s:contents){if(s.contains(name)){text = s.substring(s.indexOf(name)+name.length());break;}}return text;}public static Video getNetEaseVideo(String url) throws Exception {Document doc = getURLContent(url);String content = doc.html();content = content.substring(content.indexOf("topicid"));content = content.substring(0,content.indexOf("</script>"));content = content.replaceAll("\"","").replaceAll("\n","").trim();String contents[] = content.split(";");/** * 视频标题 */String title = getScriptVarByName("title=",contents);String topicid = getScriptVarByName("topicid=",contents);String vid = getScriptVarByName("vid=",contents);/** * 视频缩略图 */String imgpath = getScriptVarByName("imgpath=",contents);/** * 视频地址 */String flash = "http://img1.cache.netease.com/flvplayer081128/~false~"+topicid+"_"+vid+"~"+imgpath.substring(7,imgpath.length()-4)+"~.swf";Video video = new Video();video.setTitle(title);video.setThumbnail(imgpath);video.setFlashUrl(flash);video.setSource("网易视频");video.setPageUrl(url);// video.setSummary(summary);video.setHtmlCode(getHtmlCode(flash));return video;}private static String getScriptVarByName(String name,String contents[]) {String text = "";for(String s:contents){if(s.contains(name)){text = s.substring(s.indexOf(name)+name.length());break;}}return text;}


再加腾讯视频支持

public static Video getQQVideo(String url) throws Exception {Document doc = getURLContent(url);/** * 视频标题 */String vid = url.substring(url.lastIndexOf("/")+1,url.lastIndexOf("."));System.out.println(vid);String title = doc.title().split("-")[0].trim();/** * 视频地址 */String flash = "http://imgcache.qq.com/tencentvideo_v1/player/TencentPlayer.swf?vid="+vid;Video video = new Video();video.setTitle(title);video.setSource("腾讯视频");video.setFlashUrl(flash);return video;}
public static Video getNetEaseVideo(String url) throws Exception {Document doc = getURLContent(url);String content = doc.html();content = content.substring(content.indexOf("topicid"));content = content.substring(0,content.indexOf("</script>"));content = content.replaceAll("\"","").replaceAll("\n","").trim();String contents[] = content.split(";");/** * 视频标题 */String title = getScriptVarByName("title=",contents);String topicid = getScriptVarByName("topicid=",contents);String vid = getScriptVarByName("vid=",contents);/** * 视频缩略图 */String imgpath = getScriptVarByName("imgpath=",contents);/** * 视频地址 */String flash = "http://img1.cache.netease.com/flvplayer081128/~false~"+topicid+"_"+vid+"~"+imgpath.substring(7,imgpath.length()-4)+"~.swf";Video video = new Video();video.setTitle(title);video.setThumbnail(imgpath);video.setFlashUrl(flash);video.setSource("网易视频");video.setPageUrl(url);// video.setSummary(summary);video.setHtmlCode(getHtmlCode(flash));return video;}private static String getScriptVarByName(String name,String contents[]) {String text = "";for(String s:contents){if(s.contains(name)){text = s.substring(s.indexOf(name)+name.length());break;}}return text;}


再加腾讯视频支持

public static Video getQQVideo(String url) throws Exception {Document doc = getURLContent(url);/** * 视频标题 */String vid = url.substring(url.lastIndexOf("/")+1,url.lastIndexOf("."));System.out.println(vid);String title = doc.title().split("-")[0].trim();/** * 视频地址 */String flash = "http://imgcache.qq.com/tencentvideo_v1/player/TencentPlayer.swf?vid="+vid;Video video = new Video();video.setTitle(title);video.setSource("腾讯视频");video.setFlashUrl(flash);return video;}


忘了两个参数:
public final static String VIDEO_DOMAIN_NETEASE = "v.163.com/video";//网易public final static String VIDEO_DOMAIN_QQ = "v.qq.com/play";//QQ
at com.hlj.test.Know.main(Know.java:24)


用的你测试的方法。。。

出现video为null。。。这是什么问题。


import org.osm.video.Video;
import org.osm.video.VideoUtil;

public class Know {

public static void main(String[] args) {
//String url = "http://v.youku.com/v_show/id_XMzAyMjE1Nzgw.html";
String url = "http://www.tudou.com/programs/view/Zkazx5iaUSQ/";
// String url = "http://v.ku6.com/special/show_4024167/Dwq_4xKT5Go4f4F6.html";
// String url = "http://v.ku6.com/show/bFx2VCEiF15U53E6.html";
// String url = "http://6.cn/watch/14757577.html";
// String url = "http://www.56.com/u37/v_NjE3OTQ4NTg.html";
// String url = "http://video.sina.com.cn/p/news/s/v/2011-09-11/133861482545.html";
// String url = "http://tv.sohu.com/20110912/n319076906.shtml";
// String url = "http://v.ifeng.com/documentary/discovery/201109/ed52d159-a3f4-4969-972d-c9e6d51d00ff.shtml";
// String url = "http://www.yinyuetai.com/video/271439";
// String url = "http://www.baidu.com/";

Video video = VideoUtil.getVideoInfo(url);
System.out.println(video);
System.out.println("视频标题:" + video.getTitle());
System.out.println("视频地址:" + video.getFlashUrl());
System.out.println("视频时长:" + video.getTime());
System.out.println("视频来源:" + video.getSource());
System.out.println("视频简介:" + video.getSummary());
System.out.println("视频缩略图:" + video.getThumbnail());
System.out.println("视频原始地址:" + video.getPageUrl());
System.out.println("视频网页代码:" + video.getHtmlCode());
}
} 16 楼 jinbaking 2012-06-30 韩国特别人体吧人 17 楼 jinbaking 2012-06-30 你好!现在这段代码好像只有6房间和56视频网好使了,其他的都不能用了啊~~有什么新的方法吗?

读书人网 >编程

热点推荐