<?php
function getVKVideoInfo($link, $getFilesizes = false) {
$html = file_get_contents(urldecode($link));
preg_match('/(?<=var vars = ).+}/', $html, $matches);
$json = json_decode(mb_convert_encoding($matches[0], "UTF-8", "CP1251"), true);
$result['name'] = urldecode($json['md_title']);
foreach ($json as $key => $value) {
if (strpos($key, 'url') === 0) {
preg_match('/^.+videos\/\w+\.(\d{3,4}).(\w{3,5})(?>=?)/', $value, $matches);
preg_match('/^.+videos\/\w+\.?(\d{3,4})?.(\w{3,5})(?>=?)/', $value, $matches);
$temp = array('link' => $value, 'quality' => $matches[1], 'format' => $matches[2]);
if ($getFilesizes) {
$head = array_change_key_case(get_headers($matches[0], true));
$temp['size'] = round($head['content-length'] / 1024 / 1024, 2);
}
$result['urls'][] = $temp;
unset($temp);
}
}
return $result;
}
function echoVKVideoLinks($data) {
foreach ($data['urls'] as $video) {
echo 'Скачать <a href="' . $video['link'] .'">' . $data['name'] . '.' . $video['format'] . '</a>';
if (isset($video['quality']) && $video['quality'] != '') {
echo ' в качестве ' . $video['quality'] . 'p';
}
if (isset($video['size']) && $video['size'] != '') {
echo ' (' . $video['size'] . "МБ)";
}
echo '<br>';
}
}
function returnVKVideoLinks($data, $title = false, $encodeUrls = false) {
$msg = '';
foreach ($data['urls'] as $video) {
$link = $encodeUrls ? '/downloadvideo/' . base64url_encode($video['link']) : $video['link'];
$anchor = $title ? $title : mb_convert_encoding($data['name'], "CP1251", "UTF-8");
$format = $video['format'] != '' ? $video['format'] : 'flv';
$msg .= 'Скачать <a href="' . $link .'" target="_blank" download>' . $anchor . '.' . $format . '</a>';
if (isset($video['quality']) && $video['quality'] != '') {
$msg .= ' в качестве ' . $video['quality'] . 'p';
}
if (isset($video['size']) && $video['size'] != '') {
$msg .= ' <sup>(' . $video['size'] . "МБ)</sup>";
}
$msg .= '<br>';
}
return $msg;
}
function base64url_encode($data) {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
function base64url_decode($data) {
return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
}
echo returnVKVideoLinks(getVKVideoInfo('https://vk.com/video_ext.php?oid=317204999&id=171444669&hash=de8b10f2b0efeca4',true),'test',false);