...
PHP

php抖音短视频去水印

闲来无事

搞了个抖音无水印视频解析

上代码

<?php

/**
* 抖音短视频去水印
* @author huyiwei
*/
// error_reporting(0);
class douyin
{
static public function deVideoInfo($url)
{
// 设置UA
ini_set('user_agent', 'user-agent: Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3');
// 获取跳转url
$url = self::getReferrerUrl($url);


if (!$url) {
return '获取跳转url失败';
}

// 拿到网址部分
$params = parse_url($url);

// 分割地址
$params = explode('/',$params['path']);

// 遍历获取正确的id
$mid = null;
foreach ($params as $key => $value) {
// 如果为数字则就是我们要找的id
if(is_numeric($value)){
$mid = $value;
break;
}
}
if(!$mid) return '获取视频id失败';
// 请求链接
$url = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=$mid";
$arrContextOptions = [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
]
];
// 构建ajax请求
$str = file_get_contents($url, false, stream_context_create($arrContextOptions));
$json = json_decode($str, 1);
if(!array_key_exists('status_code',$json) || $json['status_code'] != 0){
return 'ajax请求失败';
}
// 异常捕获 因为有多维数组
try {
// 得到视频地址
$videourl = $json['item_list'][0]['video']['play_addr']['url_list'][0];
// 替换playwm为play才为真是无水印链接
$videourl = str_replace('playwm', 'play', $videourl);
// 获取跳转后的视频连接
$videourl = self::getReferrerUrl($videourl);
// 构造返回数组
$res = [
'name' => $json['item_list'][0]['desc'], //视频名称
'nickname' => $json['item_list'][0]['author']['nickname'], //作者昵称
'cover' => 'https://p3.pstatp.com/large/' . $json['item_list'][0]['video']['cover']['uri'], // 封面
'audio' => $json['item_list'][0]['music']['play_url']['uri'], // 音频数据
'video' => $videourl, //视频连接
'like' => $json['item_list'][0]['statistics']['digg_count'], //点赞数
'comment' => $json['item_list'][0]['statistics']['comment_count'] //评论数
];
return $res;
} catch (\Throwable $th) {
// throw $th;
return '数组解析出错';
}
}



//获取跳转链接
static public function getReferrerUrl($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36');
curl_exec($ch);
$infos = curl_getinfo($ch);
if($infos['url'] == $url){
$infos['url'] = $infos['redirect_url'];
}
curl_close($ch);
return $infos['url'];
}
}


使用:

$res = douyin::deVideoInfo('https://v.douyin.com/JfRdYs5/');
if (is_array($res)) {
var_dump($res);
}else{
echo '解析失败-'.$res;
}

// 输出:
// Array
// (
// [name] => 谁才是抖音最神秘的人!@黑脸V(今晚8点直播首秀)
// [nickname] => 跑腿界的彭于晏
// [cover] => https://p3.pstatp.com/large/tos-cn-p-0015/930af914016a4e4c86d20df91bd7723e_1590660583
// [video] => http://v26-dy.ixigua.com/ef3c10d4b78b7b194683e74a764ed652/5ed0eb38/video/n/tosedge-tos-agsy-ve-0015/5096e53f1eb74aa98ccd572b95d02235/?a=1128&br=5619&bt=1873&cr=0&cs=0&dr=0&ds=6&er=&l=202005291759220100080710660EF20036&lr=&mime_type=video%2Fmp4&qs=0&rc=anI1PHk8amZydTMzOmkzM0ApN2g7OWRmNjxnN2hlZzs0OGdkNDUtY2FmbzRfLS1iLS9zcy8vNjNeM2E2XzE2LjZeYWA6Yw%3D%3D&vl=&vr=
// [audio] => http://p9-dy.byteimg.com/obj/ies-music/e61708b23a1efddd5988973213da0a00.mp3
// [like] => 896610
// [comment] => 12615
// )


一个小时学会Git 放弃setInterval 用 requestAnimationFrame 完美解决 js 定时器卡顿
biu biu biu
分享一个领红包插件代码 Uniapp 小程序海报生成代码 vue-clipboard2(vue剪切板功能) PHP Nginx TP5 出现 require(): open_basedir restriction in effect /:/tmp/:/proc/ 报错 分享一个跨平台翻译软件TTime