1. 程式人生 > 實用技巧 >PHP 實現抖音視訊播放去水印

PHP 實現抖音視訊播放去水印

原理

# 播放地址
https://aweme.snssdk.com/aweme/v1/playwm/?video_id=。。。。。
# 無水印地址
https://aweme.snssdk.com/aweme/v1/play/?video_id=。。。。。
# 區別
引數 paly/playwm
# 注意
據推測,猶豫抖音對PC做了限制,現在只有模擬手機發送請求才可以實現無水印播放。

程式碼處理

/**
 * 返回無水印播放地址
 * @desc 使用方法 域名url=視訊的分享地址
 */  
public function index()
{
    // 通過 url 獲取到 解析後的地址
    $url = $_GET['url'];
    $res = $this->curl_http_exec($url);
    preg_match('/href="(.*?)">Found/', $res, $matches);
    $url_share = $matches[1];
    // 根據解析後的地址獲取到 item_ids
    preg_match('/video\/(.*?)\//', $url_share, $matches);
    $item_ids = $matches[1];
    // 根據 item_ids 獲取播放地址
    $arr = json_decode($this->curl_http_exec('https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=' . $matches[1]), true);
    $url_play = $arr['item_list'][0]["video"]["play_addr"]["url_list"][0];
    // 根據播放地址 獲取到無水印播放地址
    $url_play_remove_mark = str_replace('playwm', 'play', $url_play);
    preg_match('/href="(.*?)">Found/', $this->curl_http_exec($url_play_remove_mark), $matches);
    $videoUrl = str_replace('&', '&', $matches[1]);
    echo $videoUrl;
}

/**
 * 獲取地址中的內容
 * @param $url
 * @return bool|string
 */
public function curl_http_exec($url)
{
    // $Header = array("User-Agent:Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1");
    $Header = array("User-Agent:Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1");
    $con = curl_init((string)$url);
    curl_setopt($con, CURLOPT_HEADER, false); # 啟用時會將標頭檔案的資訊作為資料流輸出。
    curl_setopt($con, CURLOPT_SSL_VERIFYPEER, false); # 禁用後cURL將終止從服務端進行驗證。
    curl_setopt($con, CURLOPT_RETURNTRANSFER, true); # 將curl_exec()獲取的資訊以檔案流的形式返回,而不是直接輸出。
    curl_setopt($con, CURLOPT_HTTPHEADER, $Header); # 用來設定HTTP頭欄位的陣列
    curl_setopt($con, CURLOPT_TIMEOUT, 5000); # 設定cURL允許執行的最長秒數。
    $result = curl_exec($con); # 抓取URL並把它傳遞給瀏覽器
    curl_close($con); # //關閉cURL資源,並且釋放系統資源
    return $result;
}