通过分析芒果TV手机H5网页版来获取视频直链,该版本需要传入一个cookie,如果想要解析vip视频,就需要传一个vip cookie。清晰度版本只有一个标清版,没有办法切换获取不同的清晰度。
1、以下面链接作为实例演示:
https://m.mgtv.com/b/328613/5369527.html
2、通过下面的链接获取pm2的值
https://m.da.mgtv.com/m/player?_type_object=p,v&p={%22oversea%22:0,%22m%22:{%22p%22:4540,%22ptype%22:%22front%22,%22pu%22:%22https%3A%2F%2Fm.mgtv.com%2Fb%2F328613%2F5369527.html%22},%22c%22:{%22type%22:22,%22os%22:%22android%205.0%22,%22version%22:%225.1.0%22,%22rs%22:%22360*640%22,%22lt%22:0,%22ts%22:1553960358913,%22cw%22:360,%22ch%22:640,%22cori%22:0,%22sd%22:3},%22u%22:{%22passport%22:%22%22,%22ck%22:%222204c7c9-56e9-47f8-8e16-ee7592c52914%22,%22sid%22:%221553960111762%22,%22cxid%22:%22%22,%22isContinue%22:0},%22atc%22:{%22tk2%22:%224UzMwYTOzUTNx0Ddpx2Y8BjLx4SN9IXZ2xXMzATM98mbwxHM9AXa2xnNzcjN3YDM1IzM1MjMwIDOwETM9QWa1xXPklGZ%22}}&v={%22v%22:{%22url%22:%22https%3A%2F%2Fm.mgtv.com%2Fb%2F328613%2F5369527.html%22,%22vip%22:0,%22uname%22:%22%22,%22ucode%22:%22%22,%22id%22:5369527,%22hid%22:328613,%22rid%22:2,%22clip_type%22:1,%22vtt%22:1265,%22ispreview%22:0,%22ispay%22:0}}&callback=jsonp_qdcrgz90v069jgw
(1)、url解码以后的链接如下图:
(2)、通过分析上面链接主要有p参数和v参数组成
p参数和v参数都是一个json字符串,p参数字符串由下面数据组成
//链接信息 $m = [ "p" => 4540, "ptype" => "front", "pu" => urlencode($url),//$url为要解析的链接https://m.mgtv.com/b/328613/5369527.html ]; //客户端信息 $c = [ "type" => 22, "os" => "android 5.0", "version" => "5.1.0", "rs" => "360*640", "lt" => 0, "ts" => msectime(),//1553502233256, "cw" => 360, "ch" => 640, "cori" => 0, "sd" => 3 ]; //用户信息 $cookieArr是把cookie转成的数组,cookie里面包含uuid, mg_uuid, MQGUID $uname = str_replace(substr($cookieArr['uuid'], 13, 3), '*屏蔽的关键字*', $cookieArr['uuid']); $u = [ "passport" => $uname, "ck" => $cookieArr['mg_uuid'], "sid" => msectime().'',//当前时间毫秒数 "cxid" => "", "isContinue" => 0 ]; //这个地方涉及到了Tk2的算法getTk2方法在下面 $atc = [ 'tk2' => $this->getTk2('did=|uid='.$cookieArr['MQGUID'].'|vip=0|pno=1031|ver=5.1.0|clit='.time()) ]; $p = [ "oversea" => 0, "m" => $m, "c" => $c, "u" => $u, "atc" => $atc, ]; /** * 获取tk2的值 */ private function getTk2($str){ $base64Str = base64_encode($str); $replaceStr = str_replace(array("+","/","="),array("_","~","-"), $base64Str); return strrev($replaceStr); }
v参数字符串由下面数据组成
$videoInfo为要解析的链接里面的视频信息,可以通过获取链接页面源码来获取。 $url = 'https://m.mgtv.com/b/328613/5369527.html'; $content = httpRequest($url, 'GET', null, $this->header);//获取链接源码 $jsonStr = cut_str($content, '__INITIAL_STATE__=', '}}}}</scr').'}}}}';//截取字符串获取中间内容 $jsonArr = json_decode($jsonStr, true); $videoInfo = $jsonArr['playPage']['videoinfo']; $ispreview = 0; if($videoInfo['isFullPreview']){ $ispreview = 1; } $v = [ 'v' => [ "url" => urlencode($url),//$url要解析的链接 "vip" => 1, "uname" => $uname,//通p参数里面的passport "ucode" => "", "id" => $videoInfo['partId'], "hid" => (int)$videoInfo['clipId'], "rid" => 2, "clip_type" => 1, "vtt" => $videoInfo['time'], "ispreview" => $ispreview, "ispay" => 0 ] ];
最后拼接链接
$playerUrl = 'https://m.da.mgtv.com/m/player?_type_object=p,v&p='.json_encode($p).'&v='.json_encode($v); $playerUrl = str_replace(['"',' '], ['%22','%20'], $playerUrl);
至此,需要请求的链接拼接完成,通过该链接可以获取下一次请求需要的pm2的值,请求返回结果如下:
3、拼接getSource的链接
$v5mUrl = 'https://v5m.api.mgtv.com/remaster/uc/v0/getSource?t='.msectime().'&abroad=0&partId='.$videoInfo['partId'].'&clipId='.$videoInfo['clipId'].'&plId=0&pm2='.$pm2.'&tk2='.$this->getTk2('clit=1553599207').'&_support=10000000';
这个链接需要用的上面请求获取到的pm2的值。通过该链接获取到的数据为:
4、通过上面链接拼接atcl链接,这里可以拼接m3u8的,也可以拼接mp4的,拼接mp4得到的链接如下:
https://web-disp.titan.mgtv.com/atcl?pm2=dvgrwoFXRW9vWiMLo1GPefz29zXi65ZTHx6ARSqkqJf018J~58l7MGBqBnEA0is3iaelKI0bqGD8zQfdN_PjInclZiJgsAHB_xAoU0iIjtsh1~ipk4dCUiQNJ3vb_e7hUeg1~0BZRSizbczOPtgJQs3EnQIUEWoCB2izY40zUua6Q1zKXte5uFSzwyZgBOVIIMXAzDSyWC9e6sbOgNVg0XMnGGmpfqHoZ04juZ279L8QVIa_8kiMtXOORghPigiwYLcBveRhLb6QZj55j82DqEHLNwsx_oWahVrLIUbgRFPmZ1Xktl0uT6U6wS1b7ztCK~ejy_sqS46ac4PgpPmG0yg_i0coBAzEvoGUW3CrhKibuhJSOYFk6qUkiGLqz7iYf2NPYbUnI8UwFrZI7ObDVFhH~gpr~WwKMxK1F5Tcl_09wSljDf58FWW7DiwMjrJDFRsSfqx_OyNKdvJX
5、然后再访问atcl链接,可以获取到如下结果:
里面info的值就是视频的直链,至此整个获取过程完成。该版本的缺点是不能切换清晰度。难点在于tk2的算法。