前些天刷抖音,刷到个好看的动漫视频。本想着做个手机壁纸,但是下载到本地后发现有水印。这可不行啊,多影响美观,于是就写了个获取无水印视频的Demo。
Github传送门 biu biu biu ~~~
抖音视频有个复制链接的功能,这个链接是能在网页打开的,如下:
https://v.douyin.com/qsSFEV/
这是个短链接,复制到浏览器地址栏后得到的长链接是下面的格式:
https://www.iesdouyin.com/share/video/6780619456570789127/?region=CN&mid=6771282500762471182&u_code=14fai9b88&titleType=title&utm_source=copy_link&utm_campaign=client_share&utm_medium=android&app=aweme
找到html里的video标签的src元素就是视频的播放链接地址。但这个播放链接的地址是带有水印的,如下:
https://aweme.snssdk.com/aweme/v1/playwm/?s_vid=93f1b41336a8b7a442dbf1c29c6bbc567de80acd4fb17215462db57f4eabe430bb64051acde98da903f0d8cdc9f23f24485b28f6a85ef87dafed6915bb0494f6&line=0
只需要替换掉链接中的playwm成play就是无水印的视频了(是不是很简单)。
/**
* 获取剪切版内容
*
* @return 剪切版内容
*/
public String getShareText() {
ClipboardManager cm = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData data = null;
if (cm != null) {
data = cm.getPrimaryClip();
}
ClipData.Item item = null;
if (data != null) {
item = data.getItemAt(0);
}
String content = null;
if (item != null) {
content = item.getText().toString();
}
return content;
}
/**
* 获取链接地址
*
* @param text 获取浏览器分享出来的text文本
*/
public static String getCompleteUrl(String text) {
Pattern p = Pattern.compile("((http|ftp|https)://)(([a-zA-Z0-9._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9&%_./-~-]*)?", Pattern.CASE_INSENSITIVE);
Matcher matcher = p.matcher(text);
boolean find = matcher.find();
if (find) {
return matcher.group();
} else {
return "";
}
}
Document doc = Jsoup.connect(url).userAgent(userAgent).get();
Elements video = doc.select("video[src]");
for (Element el : video) {
String videoUrl = el.attr("src");
videoUrl = videoUrl.replace("playwm", "play");
// 获取重定向的URL
videoUrl = getRealUrl(videoUrl);
}
/**
* 获取重定向地址
*/
private String getRealUrl(String urlStr) {
String realUrl = urlStr;
try {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("user-agent", "Mozilla/5.0.html (iPhone; U; CPU iPhone OS 4_3_3 like Mac " +
"OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) " +
"Version/5.0.html.2 Mobile/8J2 Safari/6533.18.5 ");
conn.setInstanceFollowRedirects(false);
int code = conn.getResponseCode();
String redirectUrl = "";
if (302 == code) {
redirectUrl = conn.getHeaderField("Location");
}
if (redirectUrl != null && !redirectUrl.equals("")) {
realUrl = redirectUrl;
}
conn.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
return realUrl;
}
然后有了这个地址就可以播放了(貌似这个只能在移动端播放)
差不多就只有这些了,具体的就看代码吧:
Github传送门 biu biu biu ~~~
如果你有什么意见和反馈,欢迎到Github提issue(最喜欢别人提issue了)哈哈哈~