MENU

一次简单的抖音无水印视频解析过程

• December 26, 2019 • Read: 453 • 瞎折腾

首先我们找一个测试的视频
https://v.douyin.com/V6B9x9/
20191226211728.png

我们可以通过F12查看网页源代码,找到视频的播放地址
20191226212008.png

可以看到它的视频是这个链接进行 302 跳转之后得到的

打开这个链接
https://aweme.snssdk.com/aweme/v1/playwm/?s_vid=93f1b41336a8b7a442dbf1c29c6bbc561471f36124577541d8011ab57c7057c1a6b74220df0c196acb217c82d97c12471ddbb145c439d5af917d27f2f19338d1&line=0

已经是自动 302 跳转了.没办法查看他的源代码.那么我们用 CURL 模拟一下.让他禁止 302 跳转.

<?php
function curlGet($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ["user-agent: Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25"]);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}
var_dump(curlGet('https://aweme.snssdk.com/aweme/v1/play/?s_vid=93f1b41336a8b7a442dbf1c29c6bbc561471f36124577541d8011ab57c7057c1a6b74220df0c196acb217c82d97c12471ddbb145c439d5af917d27f2f19338d1&line=0'));

注意

要把URL里面的 playwm 变成 play

禁止 302 跳转后可以抓到视频链接
http://v26-dy.ixigua.com/782888ffc68428759636218c2a57b146/5e04cd87/video/tos/hxsy/tos-hxsy-ve-0015/d10c9ce22acb4cdab6ad27564334e30e/?a=1128&br=5742&bt=2871&cr=0&cs=0&dr=0&ds=3&er=&l=201912262210140100080722151C3188B7&lr=&qs=0&rc=M3Z4dGk6cHJtcjMzaWkzM0ApOmc0Zzw0PGU8Nzo0aDo3NGc0NW5yX28xNi5fLS01LTBzc19gYmA2YjEyYS4vYDVgNDI6Yw%3D%3D

20191226221114.png

这就是无水印的视频了!

然后我们归拢一下代码.写上一个完整的代码吧

<?php
/* 
Author    Myitmx
DateTime  2019-12-26
copyright [www.myitmx.com]
*/
 
function GetVideos($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ["user-agent: Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25"]);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); 
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

function GetUrl($url)
{
    $UserAgent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_ENCODING, '');
    curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    $data = curl_exec($curl);
    curl_close($curl);
    return $data;
}
//URL
$url = $_GET['url'];

if (empty($url)) {
    echo json_encode(['code' => 0, 'msg' => '请输入正确视频网址,格式为:https://api.myitmx.cn/douyin.php?url=视频地址']);
}else{
    $data = GetUrl($url);
    //获取
    preg_match('/playAddr: "(?<url>[^"]+)"/i', $data, $url);
    preg_match('/<p class="desc">(?<desc>[^<>]*)<\/p>/i', $data, $name);
    $name = $name['desc'];
    $url = $url['url'];
    if(empty($url))
    {
        echo json_encode(['code' => 0, 'msg' => '解析错误']);
        exit;
    }
    
    preg_match('/s_vid=(.*?)&/', $url, $id);
    $url = 'https://aweme.snssdk.com/aweme/v1/play/?s_vid=' . $id[1] . '&line=0';
    $data_new = GetVideos($url);
    preg_match('/<a href=\"http:\/\/(.*?)\">/', $data_new, $link);
    
    if (empty($link[1])) {
        echo json_encode(['code' => 0, 'msg' => '解析错误']);
        exit;
    }
    
    $link = 'http://' . $link[1];
    echo json_encode(['code' => 1, 'name' => $name, 'url' => $link]);
}
Archives QR Code
QR Code for this page
Tipping QR Code