Java获取抖音无印视频

有时候,想下载抖音里的视频 有权限或者有水印。这篇文章可以教你怎么下载抖音无印视频

复制分享链接 url 和 抖音API获取视频信息接口 如下:

1
2
String url = "4.61 Nws:/ 复制打开抖音,一起来回顾2016年骑士夺冠时刻# nba# 骑士# 詹... https://v.douyin.com/j472ue4/";
String API = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=";

定义正则匹配https://后面链接地址 :https://v.douyin.com/j472ue4/

1
2
3
4
5
6
7
 String regex = "https+://[^\\s]*";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(url);
String baseUrl = "";
while(m.find()) {
baseUrl = m.group();
}

1.使用HtmlUnit 创建WebClient浏览器:

1
2
3
//无参构造创建 默认使用谷歌浏览器
WebClient webClient = new WebClient();

2.浏览器设置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 		//设置支持AJAX
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
////开启页面重定向
webClient.getOptions().setRedirectEnabled(true);
//关闭ssl安全访问
webClient.getOptions().setUseInsecureSSL(false);
//关闭异常抛出
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setThrowExceptionOnScriptError(false);
//忽略解析 css js false
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setJavaScriptEnabled(false);
//设置是否允许本机 ActiveX。默认值为假。请注意,除非您完全信任 JavaScript 代码,否则绝不应该允许运行本机 ActiveX 组件,因为它不受 Java 虚拟机控制
webClient.getOptions().setActiveXNative(false);
  1. 获取HtmlPage信息
1
2
//String baseUrl = "https://v.douyin.com/j472ue4/";  
page = webClient.getPage(baseUrl);

4.获取跳转url

1
2
 String  realUrl=page.getUrl().toString();
//https://www.douyin.com/video/7111515518200237343?previous_page=app_code_link

5.截取 video/后面开头到?之间的Id ,然后拼接API 请求接口 返回JSON数据

String getJson=API+realUrl.substring(s.lastIndexOf("/") + 1, s.indexOf("?"));

String s2 = HttpUtil.get(getJson);

JSON 数据如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205

{
"extra": {
"logid": "2022082509232601021206603512314891",
"now": 1661390606818
},
"filter_list": [],
"item_list": [
{
"author": {
"avatar_larger": {
"uri": "1080x1080/aweme-avatar/mosaic-legacy_3116e0009185157261372",
"url_list": [
"https://p9.douyinpic.com/aweme/1080x1080/aweme-avatar/mosaic-legacy_3116e0009185157261372.jpeg?from=116350172",
"https://p3.douyinpic.com/aweme/1080x1080/aweme-avatar/mosaic-legacy_3116e0009185157261372.jpeg?from=116350172",
"https://p5.douyinpic.com/aweme/1080x1080/aweme-avatar/mosaic-legacy_3116e0009185157261372.jpeg?from=116350172"
]
},
"avatar_medium": {
"uri": "720x720/aweme-avatar/mosaic-legacy_3116e0009185157261372",
"url_list": [
"https://p11.douyinpic.com/aweme/720x720/aweme-avatar/mosaic-legacy_3116e0009185157261372.jpeg?from=116350172",
"https://p3.douyinpic.com/aweme/720x720/aweme-avatar/mosaic-legacy_3116e0009185157261372.jpeg?from=116350172",
"https://p6.douyinpic.com/aweme/720x720/aweme-avatar/mosaic-legacy_3116e0009185157261372.jpeg?from=116350172"
]
},
"avatar_thumb": {
"uri": "100x100/aweme-avatar/mosaic-legacy_3116e0009185157261372",
"url_list": [
"https://p6.douyinpic.com/aweme/100x100/aweme-avatar/mosaic-legacy_3116e0009185157261372.jpeg?from=116350172",
"https://p3.douyinpic.com/aweme/100x100/aweme-avatar/mosaic-legacy_3116e0009185157261372.jpeg?from=116350172",
"https://p11.douyinpic.com/aweme/100x100/aweme-avatar/mosaic-legacy_3116e0009185157261372.jpeg?from=116350172"
]
},
"follow_status": 0,
"nickname": "咪咕篮球",
"short_id": "612386281",
"signature": "",
"uid": "52899368367",
"unique_id": "migulanqiu"
},
"author_user_id": 52899368367,
"aweme_id": "7111515518200237343",
"aweme_type": 4,
"category": 0,
"cha_list": [
{
"cha_name": "詹姆斯",
"cid": "1600800922548231",
"cover_item": {
"uri": "douyin-admin-obj/19b4f627e23cfbde0801b0ba050cdd38",
"url_list": [
"https://p9-sign.douyinpic.com/douyin-admin-obj/19b4f627e23cfbde0801b0ba050cdd38~c5_100x100.jpeg?x-expires=1661410800&x-signature=6F8Q7oeZVfwzSuNa2KJD0ZZlzPM%3D&from=715538335",
"https://p26-sign.douyinpic.com/douyin-admin-obj/19b4f627e23cfbde0801b0ba050cdd38~c5_100x100.jpeg?x-expires=1661410800&x-signature=Ssv2qvBgQeXYh2%2FCt%2FNFKTeLbvg%3D&from=715538335",
"https://p3-sign.douyinpic.com/douyin-admin-obj/19b4f627e23cfbde0801b0ba050cdd38~c5_100x100.jpeg?x-expires=1661410800&x-signature=bDsDxbLeFTUBk6WwN7vkL86kkEo%3D&from=715538335"
]
},
"desc": "勒布朗·詹姆斯(LeBron James),1984年12月30日出生于美国俄亥俄州阿克伦),美国职业篮球运动员,司职小前锋,绰号“小皇帝”,现效力于NBA洛杉矶湖人队。他在2003年NBA选秀中于首轮第1顺位被克利夫兰骑士队选中,职业生涯已经9次进入总决赛,并取得了3次总冠军,3次FMVP,此外他还获得了4次MVP、3次全明星MVP、1次最佳新秀、15次最佳阵容,堪称当今联盟现役第一人。",
"hash_tag_profile": "douyin-admin-obj/19b4f627e23cfbde0801b0ba050cdd38",
"is_commerce": false,
"type": 2,
"user_count": 0,
"view_count": 0
}
],
"create_time": 1655778761,
"desc": "一起来回顾2016年骑士夺冠时刻#nba#骑士#詹姆斯 ",
"duration": 109065,
"forward_id": "0",
"group_id": 7111515518200237343,
"group_id_str": "7111515518200237343",
"is_live_replay": false,
"is_preview": 0,
"music": {
"author": "咪咕篮球",
"cover_hd": {
"uri": "1080x1080/aweme-avatar/mosaic-legacy_3116e0009185157261372",
"url_list": [
"https://p11.douyinpic.com/aweme/1080x1080/aweme-avatar/mosaic-legacy_3116e0009185157261372.jpeg?from=116350172",
"https://p6.douyinpic.com/aweme/1080x1080/aweme-avatar/mosaic-legacy_3116e0009185157261372.jpeg?from=116350172",
"https://p26.douyinpic.com/aweme/1080x1080/aweme-avatar/mosaic-legacy_3116e0009185157261372.jpeg?from=116350172"
]
},
"cover_large": {
"uri": "1080x1080/aweme-avatar/mosaic-legacy_3116e0009185157261372",
"url_list": [
"https://p11.douyinpic.com/aweme/1080x1080/aweme-avatar/mosaic-legacy_3116e0009185157261372.jpeg?from=116350172",
"https://p6.douyinpic.com/aweme/1080x1080/aweme-avatar/mosaic-legacy_3116e0009185157261372.jpeg?from=116350172",
"https://p26.douyinpic.com/aweme/1080x1080/aweme-avatar/mosaic-legacy_3116e0009185157261372.jpeg?from=116350172"
]
},
"cover_medium": {
"uri": "720x720/aweme-avatar/mosaic-legacy_3116e0009185157261372",
"url_list": [
"https://p26.douyinpic.com/aweme/720x720/aweme-avatar/mosaic-legacy_3116e0009185157261372.jpeg?from=116350172",
"https://p3.douyinpic.com/aweme/720x720/aweme-avatar/mosaic-legacy_3116e0009185157261372.jpeg?from=116350172",
"https://p11.douyinpic.com/aweme/720x720/aweme-avatar/mosaic-legacy_3116e0009185157261372.jpeg?from=116350172"
]
},
"cover_thumb": {
"uri": "168x168/aweme-avatar/mosaic-legacy_3116e0009185157261372",
"url_list": [
"https://p6.douyinpic.com/img/aweme-avatar/mosaic-legacy_3116e0009185157261372~c5_168x168.jpeg?from=116350172",
"https://p3.douyinpic.com/img/aweme-avatar/mosaic-legacy_3116e0009185157261372~c5_168x168.jpeg?from=116350172",
"https://p26.douyinpic.com/img/aweme-avatar/mosaic-legacy_3116e0009185157261372~c5_168x168.jpeg?from=116350172"
]
},
"duration": 109,
"id": 7111515691374627620,
"mid": "7111515691374627620",
"play_url": {
"uri": "https://sf6-cdn-tos.douyinstatic.com/obj/ies-music/7111515686754257677.mp3",
"url_list": [
"https://sf6-cdn-tos.douyinstatic.com/obj/ies-music/7111515686754257677.mp3",
"https://sf3-cdn-tos.douyinstatic.com/obj/ies-music/7111515686754257677.mp3"
]
},
"status": 1,
"title": "@咪咕篮球创作的原声一咪咕篮球"
},
"risk_infos": {
"content": "",
"reflow_unplayable": 0,
"type": 0,
"warn": false
},
"share_info": {
"share_desc": "在抖音,记录美好生活",
"share_title": "一起来回顾2016年骑士夺冠时刻#nba#骑士#詹姆斯 ",
"share_weibo_desc": "#在抖音,记录美好生活#一起来回顾2016年骑士夺冠时刻#nba#骑士#詹姆斯 "
},
"share_url": "https://www.iesdouyin.com/share/video/7111515518200237343/?region=&mid=7111515691374627620&u_code=0&did=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&iid=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&with_sec_did=1&titleType=title",
"statistics": {
"aweme_id": "7111515518200237343",
"comment_count": 691,
"digg_count": 5210,
"play_count": 0,
"share_count": 394
},
"text_extra": [
{
"end": 20,
"hashtag_id": 1608846127610893,
"hashtag_name": "nba",
"start": 16,
"type": 1
},
{
"end": 23,
"hashtag_id": 1569792844453889,
"hashtag_name": "骑士",
"start": 20,
"type": 1
},
{
"end": 27,
"hashtag_id": 1600800922548231,
"hashtag_name": "詹姆斯",
"start": 23,
"type": 1
}
],
"video": {
"cover": {
"uri": "tos-cn-p-0015/2c041a27f655486089d1f56c12beb483",
"url_list": [
"https://p26-sign.douyinpic.com/tos-cn-p-0015/2c041a27f655486089d1f56c12beb483~c5_300x400.jpeg?x-expires=1662598800&x-signature=zpsx%2Fpw0NMX2vRPy2u7bRTqath8%3D&from=4257465056_large&s=PackSourceEnum_DOUYIN_REFLOW&se=false&sc=cover&l=2022082509232601021206603512314891",
"https://p3-sign.douyinpic.com/tos-cn-p-0015/2c041a27f655486089d1f56c12beb483~c5_300x400.jpeg?x-expires=1662598800&x-signature=3EVMB600kNSmkjHUiHLy64qzMTs%3D&from=4257465056_large&s=PackSourceEnum_DOUYIN_REFLOW&se=false&sc=cover&l=2022082509232601021206603512314891",
"https://p9-sign.douyinpic.com/tos-cn-p-0015/2c041a27f655486089d1f56c12beb483~c5_300x400.jpeg?x-expires=1662598800&x-signature=F9SExfQXnkTyQyaWozghYOesNj8%3D&from=4257465056_large&s=PackSourceEnum_DOUYIN_REFLOW&se=false&sc=cover&l=2022082509232601021206603512314891"
]
},
"duration": 109065,
"dynamic_cover": {
"uri": "tos-cn-p-0015/2c041a27f655486089d1f56c12beb483",
"url_list": [
"https://p26-sign.douyinpic.com/obj/tos-cn-p-0015/2c041a27f655486089d1f56c12beb483?x-expires=1662598800&x-signature=hVdktX6lu6AN5hwlHlmaZthY278%3D&from=4257465056_large",
"https://p3-sign.douyinpic.com/obj/tos-cn-p-0015/2c041a27f655486089d1f56c12beb483?x-expires=1662598800&x-signature=CeQK17ZaP9uH%2B6xv2hQPU0bwQzM%3D&from=4257465056_large",
"https://p9-sign.douyinpic.com/obj/tos-cn-p-0015/2c041a27f655486089d1f56c12beb483?x-expires=1662598800&x-signature=fhISrjx%2Bhx91BF%2FISJ7uZmBtbOg%3D&from=4257465056_large"
]
},
"has_watermark": true,
"height": 1440,
"is_long_video": 1,
"origin_cover": {
"uri": "tos-cn-p-0015/ec13012ec2374d0582c1ff9da21eee63_1655778765",
"url_list": [
"https://p6-sign.douyinpic.com/tos-cn-p-0015/ec13012ec2374d0582c1ff9da21eee63_1655778765~tplv-dy-360p.jpeg?x-expires=1662598800&x-signature=BWvFIfha5FLJHBX%2BusVveTtHNTo%3D&from=4257465056&se=false&biz_tag=feed_cover&l=2022082509232601021206603512314891",
"https://p26-sign.douyinpic.com/tos-cn-p-0015/ec13012ec2374d0582c1ff9da21eee63_1655778765~tplv-dy-360p.jpeg?x-expires=1662598800&x-signature=3LbPQPKGcPw6U6wH8vzvv53z3lY%3D&from=4257465056&se=false&biz_tag=feed_cover&l=2022082509232601021206603512314891",
"https://p3-sign.douyinpic.com/tos-cn-p-0015/ec13012ec2374d0582c1ff9da21eee63_1655778765~tplv-dy-360p.jpeg?x-expires=1662598800&x-signature=8bcO%2Bf%2Bmd91qnu7LgWVN%2BwF0Tkc%3D&from=4257465056&se=false&biz_tag=feed_cover&l=2022082509232601021206603512314891"
]
},
"play_addr": {
"uri": "v0200fg10000caoiq33c77uesglpt9gg",
"url_list": [
"https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0200fg10000caoiq33c77uesglpt9gg&ratio=720p&line=0"
]
},
"ratio": "540p",
"vid": "v0200fg10000caoiq33c77uesglpt9gg",
"width": 1080
}
}
],
"status_code": 0
}

6.转换成JSON对象

1
2
3
4
5
6
7
8
9
10
11
//取名item_list列表第一组数据
JSONObject jsonObject = JSONUtil.parseObj(s2);
JSONObject item = jsonObject
.getJSONArray("item_list")
.getJSONObject(0);

//再取其下video数据
JSONObject video = jsonObject.getJSONObject("video");
//取出键名play_addr下的url_list列表第一组数据
String videoUrl= video.getJSONObject("play_addr").getJSONArray("url_list").getStr(0);
//"https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0200fg10000caoiq33c77uesglpt9gg&ratio=720p&line=0"

注意:

此链接的playwm中的wm需要去掉,才是无水印视频,否则依旧留有水印

String finalUrl=videoUrl.replace(“playwm”, “play”);

7.根据链接地址下载到本地

1
HttpUtil.downloadFile(finalUrl, new File("D:\\123.mp4"));

8.完整代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
String url = "4.61 Nws:/ 复制打开抖音,一起来回顾2016年骑士夺冠时刻# nba# 骑士# 詹... https://v.douyin.com/j472ue4/";
String API = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=";
String regex = "https+://[^\\s]*";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(url);
String baseUrl = "";
while(m.find()) {
baseUrl = m.group();
}
WebClient webClient = new WebClient();
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setJavaScriptEnabled(false);
webClient.getOptions().setActiveXNative(true);
Page page = webClient.getPage(baseUrl);
String s=page.getUrl().toString();
String s2 = HttpUtil.get(API + s.substring(s.lastIndexOf("/") + 1, s.indexOf("?")));
JSONObject item = JSONUtil.parseObj(s2)
.getJSONArray("item_list")
.getJSONObject(0);
JSONObject videoObject = item.getJSONObject("video");
String finalURl = videoObject.getJSONObject("play_addr").getJSONArray("url_list").getStr(0).replace("playwm", "play");
HttpUtil.downloadFile( finalURl, new File("D:\\123.mp4"));