精易论坛

标题: 最新抖音短视频无水印地址解析原理(抖音免费解析接口),以PHP代码为例 [打印本页]

作者: qushuiyin    时间: 2019-12-12 18:39
标题: 最新抖音短视频无水印地址解析原理(抖音免费解析接口),以PHP代码为例
用户从抖音中复制链接,出来的地址,打开是带有水印的,要想通过复制的链接解析获取无水印的视频地址,可以看如下代码

PHP源码:

<?php
$content = newCurlGet($url, $userAgent, 1);
$itemId = '';

if ( preg_match('|itemId\: \"(.*?)\",|ims', $content, $match) ) {
$itemId = $match[1];
}


$dytk = '';
if ( preg_match('|dytk\: \"(.*?)\"|ims', $content, $match) ) {
$dytk = $match[1];
}

$data = [];

if (!empty($itemId) && !empty($dytk)) {

$apiUrl = 'https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=%s&dytk=%s';

$apiUrl = sprintf($apiUrl, $itemId, $dytk);

$content = $this->newCurlGet($apiUrl, $userAgent, 1);

if (!empty($content)) {

$response = json_decode($content, true);

if (is_array($response['item_list']) && count($response['item_list']) > 0) {

$title = $response['item_list'][0]['desc'];

$img = $response['item_list'][0]['video']['cover']['url_list'][0];



//获取最终的地址

$baseUrl = '';
$videoUrl = $response['item_list'][0]['video']['play_addr']['url_list'][0];
echo $videoUrl;

}
}
}

function newCurlGet( $url = '', $userAgent = '', $follow = 1 ) {
//初始化参数
$header= [];
$urlInfo= [];
$param= [];

if ( empty($url) ) return false;

//解析url地址
$urlInfo= parse_url($url);
$host= $urlInfo['host'];
$referer    = $urlInfo['scheme'].'://'.$host;



//构造header头信息

$header= [
'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language:zh-CN,zh;q=0.8,en;q=0.6,ja;q=0.4,zh-TW;q=0.2',
'User-Agent:'.$userAgent,
'Host:'.$host,
'Referer:'.$referer,
];

$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_ENCODING, "gzip, deflate,sdch" );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
curl_setopt ( $ch, CURLOPT_USERAGENT, $userAgent );
if ( strtolower( $urlInfo["scheme"]) == 'https' ){
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
}

curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_MAXREDIRS, 5 );
curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, $follow );
curl_setopt ( $ch, CURLOPT_TIMEOUT, 3 );
$response = curl_exec( $ch );
curl_close ( $ch );
return $response;
}

这里需要说明的是,获取到的videoUrl并不是最终的地址,这个地址302跳转之后才是最终的无水印视频地址,可以通过如下的代码获得最终的无水印地址

$header = [
'accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'sec-fetch-mode:navigate',
'sec-fetch-site:none',
'pragma:no-cache',

];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $videoUrl);
curl_setopt ( $ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
curl_close($ch);

if ( preg_match('/Location: (.+)\s/i', $content, $match) ) {
echo trim($match[1]);

}

但是经过测试发现,抖音会根据访问频率对访问的服务器进行封ip,而且Location拿到的地址并不是每次都可以拿到的,
所以需要循环多次的去拿才可以,这样就会造成需要非常稳定而且廉价的代理ip才可以。建议的做法还是找一些第三方解析平台,
推荐第三方解析平台:Videoparse(https://www.videoparse.cn/)。







欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4