1. 程式人生 > >java抖音視訊解析(去水印)

java抖音視訊解析(去水印)

博主第二篇文章,轉載請說明出處,有問題請提出來,和博主一起討論(抖音分享地址更改)

1.真正解析水印並不是解析(理解誤區)

2.拿到視訊地址後解析出原始地址(有軟體可以做到模糊水印,但是隻能模糊,真真去掉是

有些許困難的,需要對視訊一秒一秒取幀處理,博主實力有限,做不到)

博主喜歡用程式碼來表示自己所說的,廢話不多說,附程式碼,大家觀摩

package com.test.douyin;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

/**
 * tips:匯入jsoup包
 * 《特別注意!!!!!》
 * 這裡用的是jdk8,如果執行直接報錯,先換一下jdk再試試,有後面的同學已經遇到了
 * 
 * @author hedong
 */
@SuppressWarnings("all")
public class DYDecode {

    public static void main(String[] args) {
		String url = "http://v.douyin.com/dwGThr/";// 解析的網址,長短連結都可以
		String playAddr = getURI(NewUrlDecode(urlAnalysisMethod(url))).trim();// 有空白符
		String cover = getCover(urlAnalysisMethod(url)).trim();// 有空白符
		DecodeVideo dv = new DecodeVideo();
		dv.setPlayAddr(playAddr);
		dv.setCover(cover);
		System.out.println(dv);

		/*
		 * tips:最後得到的結果 DecodeVideo
		 * [cover=https://p3.pstatp.com/large/86920013ac9cb67fe09a.jpg,
		 * playAddr=http://v3-dy-z.ixigua.com/d153fa2e0d25730c9a6ffa949e228598/5b2b4b7e/
		 * video/m/2203826b87d19034f608c53f014a2f5ece01157576f0000077573318095/]
		 */ }

	public static String urlAnalysisMethod(String url) {
		try {
			if (isContainChinese(url)) {
				url = cuthttpschinese(url);
			}
			if (url.length() < 40) {
				url = getURI(url);
			}
			return url;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String NewUrlDecode(String url) {
		Document doc = null;
		try {
			doc = Jsoup.connect(url).timeout(5000).get();

		} catch (Exception e) {
			e.printStackTrace();
		}
		// System.out.println(doc.data());
		Elements elem = doc.getElementsByTag("script");
		String url1 = elem.toString();
		int start = url1.indexOf("playAddr");
		url1 = url1.substring(start);
		int end = url1.indexOf("\",");
		return url1.substring(11, end).replaceAll("playwm", "play");
	}

	// 獲取封面,專案要用到
	public static String getCover(String url) {
		Document doc = null;
		try {
			doc = Jsoup.connect(url).timeout(5000).get();

		} catch (Exception e) {
			e.printStackTrace();
		}
		Elements elem = doc.getElementsByTag("script");
		String url1 = elem.toString();
		int start = url1.indexOf("cover");
		String str = url1.substring(start);
		int end = str.indexOf("}");
		return str.substring(8, end).replaceAll("\"", "");
	}

	/**
	 * @param url
	 * @return
	 */
	// 獲取真實地址
	public static String getURI(String url) {
		HttpClient httpClient = new DefaultHttpClient();
		HttpContext httpContext = new BasicHttpContext();
		HttpGet httpGet = new HttpGet(url);
		try {
			// 將HttpContext物件作為引數傳給execute()方法,則HttpClient會把請求響應互動過程中的狀態資訊儲存在HttpContext中
			HttpResponse response = httpClient.execute(httpGet, httpContext);
			// 獲取重定向之後的主機地址資訊
			HttpHost targetHost = (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
			// 獲取實際的請求物件的URI,即重定向之後的地址
			HttpUriRequest realRequest = (HttpUriRequest) httpContext.getAttribute(ExecutionContext.HTTP_REQUEST);
			return (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST)
					+ ((HttpUriRequest) httpContext.getAttribute(ExecutionContext.HTTP_REQUEST)).getURI().toString();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			httpClient.getConnectionManager().shutdown();
		}
	}

	/**
	 * @param str
	 * @return
	 */
	// 檢查是否有中文
	public static boolean isContainChinese(String str) {
		Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
		Matcher m = p.matcher(str);
		if (m.find()) {
			return true;
		}
		return false;
	}

	/**
	 * 
	 * @param str
	 * @return
	 */
	// 擷取到http開始的欄位
	public static String cuthttpschinese(String str) {
		int start = str.indexOf("http");
		return str.substring(start);
	}
	
}
package com.test.viodedecode;
/**
 * @author hedong
 */
public class DecodeVideo {
	
	private String cover;
	private String playAddr;
	public String getCover() {
		return cover;
	}
	public void setCover(String cover) {
		this.cover = cover;
	}
	public String getPlayAddr() {
		return playAddr;
	}
	public void setPlayAddr(String playAddr) {
		this.playAddr = playAddr;
	}
	@Override
	public String toString() {
		return "DecodeVideo [cover=" + cover + ", playAddr=" + playAddr + "]";
	}
	
 
}

謝謝觀看,如有問題,聯絡博主哦

博主郵箱[email protected]