1. 程式人生 > 其它 >PHP程式碼實現快手去水印

PHP程式碼實現快手去水印

技術標籤:javaphpandroid移動開發thinkphp

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;
    }