腾讯视频解析,Java实现
生活随笔
收集整理的這篇文章主要介紹了
腾讯视频解析,Java实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天學到一個Android網絡視頻播放程序,但是不知道為什么找了很多個網站的URL都不能播放,用碩鼠找的很多冷門網站的視頻也不能用。
所以就想是不是MediaPlayer播放視頻的方法實在太OUT了,我想應該是的,但是網上找了個解析騰訊視頻的代碼(竟然還能用,而且還能解析最新的,看來中國的技術真的是敵不動我不動啊): package TX;import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL;/**獲取的網絡視頻地址無任何廣告,為CDN最后返回的結果,可直接播放或下載。有些視頻過大,CDN有分段處理,如果多段以“$”隔開。只對獲取騰訊網絡視頻進行了整理,實際上各大網絡視頻獲取方式都一樣,只要分析下請求鏈接與參數,然后模擬請求整理成代碼即可。*/ /** 解析騰訊視頻 */ public class TX {public String getTencentMovieSource(String url) {String html = this.getHtml(url, true).replaceAll(" ", "");String vid = this.getValue(html, "vid:", 1, "\",", 0);String urlXml = "http://vv.video.qq.com/geturl?platform=1&otype=xml&vid="+ vid;if (urlXml.indexOf("|") == -1)return this.parseXmlSource(urlXml);else {String urls = "";String[] uls = urlXml.replace("|", "-").split("-");for (int i = 0; i < uls.length; i++) {String htmls = "http://vv.video.qq.com/geturl?platform=1&otype=xml&vid="+ uls[i];urls += this.parseXmlSource(htmls) + "$";}return urls.substring(0, urls.lastIndexOf("$"));}}private String parseXmlSource(String urlXml) {String videoXml = getHtml(urlXml, false);return getValue(videoXml, "<url>", "</url>");}private String getHtml(String url, boolean isformat) {System.out.println("Request URL:" + url);try {URL u = new URL(url);HttpURLConnection httpConn = (HttpURLConnection) u.openConnection();// 設置user agent確保系統與瀏覽器版本兼容HttpURLConnection.setFollowRedirects(true);httpConn.setRequestMethod("GET");httpConn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727)");InputStream is = u.openStream();int length = 0;ByteArrayOutputStream bos = new ByteArrayOutputStream();while ((length = is.read()) != -1) {bos.write(length);}if (isformat)return new String(bos.toByteArray(), "UTF-8").replace("\r", "").replace("\n", "");elsereturn new String(bos.toByteArray(), "UTF-8");} catch (Exception e) {e.printStackTrace();return null;}}private String getValue(String html, String s1, String s2) {try {String subHtml = html.substring(html.indexOf(s1));subHtml = subHtml.substring(s1.length());int s2Len = subHtml.indexOf(s2);return String.valueOf(subHtml.substring(0, s2Len));} catch (Exception e) {e.printStackTrace();return null;}}private String getValue(String html, String s1, int s1length, String s2,int s2length) {try {StringBuffer subHtml = new StringBuffer(html.substring(html.indexOf(s1)));return String.valueOf(subHtml.substring(s1.length() + s1length,subHtml.indexOf(s2) - s2length));} catch (Exception e) {e.printStackTrace();return null;}}public static void main(String[] args) {String s = new TX().getTencentMovieSource("http://v.qq.com/cover/8/8oec1592nwztc70.html?vid=b01973axj9x");System.out.println("視頻源地址:" + s);} }
成功的測試:
總結
以上是生活随笔為你收集整理的腾讯视频解析,Java实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++之依赖倒置原则
- 下一篇: 微商城搭建为什么看重源代码?新商云电商系