PHP代码实现微视去水印
前言
1、一直在寻找微视去水印的方法,PHP的要不然过时了,要不然就是java的,或者是Python的。
2、更过分的是有人提供api收费接口打着,开源源码的旗号。
3、这里我就提供纯粹的源码思路,怎么啦!怎么啦
实现思路
1、参考博客:快手去水印解析获取源标题、作者、头像、封面图以及源视频下载地址
页面分析
1、拿到一个快手分享页面 http://kphbeijing.m.chenzhongtech.com/s/ARmv5BoW
然后我们获取这个短视频的文案标题、视频封面、视频地址
获取数据
$headers = [
'Connection' => 'keep-alive',
'User-Agent'=>'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Mobile Safari/537.36'
];
$client = new Client(['headers'=>$headers]);
//允许重定向获取html
$res = $client->request('GET', $url,['allow_redirects' => true]);
$html = (string)$res->getBody();
$Query = QueryList::getInstance();
$json = $Query->html($html)->find('div[id=hide-pagedata]')->attr('data-pagedata');
$video_data = json_decode($json,true);
获取视频详细信息
//获取图片id
$photoId = $video_data['photoId'];
$param = "client_key=56c3713c&photoIds=".$photoId;
//计算sign
$replace = str_replace("&", "",$param).'23caab00356c';
$sig =md5($replace);
$queryUrl = 'http://api.gifshow.com/rest/n/photo/info?'.$param."&sig=".$sig;
//获取url地址之后不能让他重定向
$res = $client->request('GET', $queryUrl,['allow_redirects' => false]);
$body = (string)$res->getBody();
$result = json_decode($body,true);
- 上面的代码java版本的解释一下,下面 new String(new byte[]{50, 51, 99, 97, 97, 98, 48, 48, 51, 53, 54, 99})明显是一个固定的字符串java运行结果23caab00356c。然后是SecureUtil.md5().digestHex(replace)这句明显就是SecureUtil.md5(replace)也就是phper看得懂的MD5加密:
String replace = StrUtil.replace(param, "&", "") + new String(new byte[]{50, 51, 99, 97, 97, 98, 48, 48, 51, 53, 54, 99});
String sig = SecureUtil.md5().digestHex(replace);
视频封面、视频地址获取
$video_data = $result['photos'][0];
$data['video_src']= $video_data['main_mv_url'];
$data['video_cover'] = $video_data['thumbnail_url'];
文案标题获取
$video_title = $video_data['caption'];
$title = explode('@',$video_title);
for ($i=0;$i<count($title);$i++){
$video_title = str_replace('@'.$title[$i],"",$video_title);
}
$title = explode('#',$video_title);
for ($i=0;$i<count($title);$i++){
$video_title = str_replace('#'.$title[$i],"",$video_title);
}
$data['video_title'] = $video_title;
最后代码总结
public function kuaiShou($url){
$headers = [
'Connection' => 'keep-alive',
'User-Agent'=>'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Mobile Safari/537.36'
];
$client = new Client(['headers'=>$headers]);
//允许重定向获取html
$res = $client->request('GET', $url,['allow_redirects' => true]);
$html = (string)$res->getBody();
$Query = QueryList::getInstance();
$json = $Query->html($html)->find('div[id=hide-pagedata]')->attr('data-pagedata');
$video_data = json_decode($json,true);
//获取图片id
$photoId = $video_data['photoId'];
$param = "client_key=56c3713c&photoIds=".$photoId;
//计算sign
$replace = str_replace("&", "",$param).'23caab00356c';
$sig =md5($replace);
$queryUrl = 'http://api.gifshow.com/rest/n/photo/info?'.$param."&sig=".$sig;
//获取url地址之后不能让他重定向
$res = $client->request('GET', $queryUrl,['allow_redirects' => false]);
$body = (string)$res->getBody();
$result = json_decode($body,true);
//获取到视频相关数据
$video_data = $result['photos'][0];
$data['video_src']= $video_data['main_mv_url'];
$data['video_cover'] = $video_data['thumbnail_url'];
//获取文案标题
$video_title = $video_data['caption'];
$title = explode('@',$video_title);
for ($i=0;$i<count($title);$i++){
$video_title = str_replace('@'.$title[$i],"",$video_title);
}
$title = explode('#',$video_title);
for ($i=0;$i<count($title);$i++){
$video_title = str_replace('#'.$title[$i],"",$video_title);
}
$data['video_title'] = $video_title;
return $data;
}