记录和分析一下抖音短视频无水印解析的原理和思路
首先我们找一个测试的视频
然后我们打开 F12 看一下他的这个视频路径
参数有line
和video_id
line
是固定格式
video_id
呢?一看就是视频 ID.我们先打开那个分享出来的链接查看源代码找一下看看有没有.
直接打开这个链接:https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0200f9b0000bffu2asmavf2i72vo7jg&line=0
已经是自动 302 跳转了.没办法查看他的源代码.那么我们用 CURL 模拟一下.让他禁止 302 跳转.看看可以取到什么.
<?php function curlGet($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_HTTPHEADER, ["user-agent: Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25"]); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); $output = curl_exec($ch); curl_close($ch); return $output; } var_dump(curlGet('https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0200f9b0000bffu2asmavf2i72vo7jg&line=0'));
哇靠...取不到东西..怎么是空的呢? 别的地方也没有能取到视频地方了啊..
问题第一定在这个链接里!经过我一顿操作发现.
https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0200f9b0000bffu2asmavf2i72vo7jg&line=0
把 playwm 变成 play
https://aweme.snssdk.com/aweme/v1/play/?video_id=v0200f9b0000bffu2asmavf2i72vo7jg&line=0
var_dump(curlGet('https://aweme.snssdk.com/aweme/v1/play/?video_id=v0200f9b0000bffu2asmavf2i72vo7jg&line=0'));
然后我们归拢一下代码.写上一个完整的代码吧.
<?php /** * @Author KIENG * @DateTime 2019-03-03 * @copyright [blog.kieng.cn] * @license [BY-NC-SA/3.0/CN] * @version [1.0] * @param [type] $id [抖音视频 ID] * @return [type] [无水印链接] */ function douYin($dy_url) { $data = curlGet($dy_url, true); preg_match('/video_id=(.*?)&/', $data, $id); if (empty($id[1])) { return json_encode(['code' => 0, 'msg' => '解析错误']); } $url = 'https://aweme.snssdk.com/aweme/v1/play/?video_id=' . $id[1] . '&line=0'; $data_new = curlGet($url); preg_match('/<a href=\"http:\/\/(.*?)\">/', $data_new, $link); if (empty($link[1])) { return json_encode(['code' => 0, 'msg' => '解析错误']); } $link = 'https://' . $link[1]; return json_encode(['code' => 1, 'url' => $link]); } function curlGet($url, $redirect = false) { $header[] = 'User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $redirect); $output = curl_exec($ch); curl_close($ch); return $output; } $url = $_GET['url']; //使用方法 header('Content-type: application/json'); //告诉浏览器是 JSON 格式 写不写无所谓 echo douYin($url);
附上我写的一个成品网站源码:
蓝奏云