Javascript 如何获取youtube视频的直接url
目前所有关于制作自己的youtube视频下载程序的教程都过时了。从视频信息插入整个令牌不起作用,使用javascript方法也不起作用。有没有人知道目前有什么方法可以不用下载别人的程序就可以做到这一点?我只想知道该怎么做,这样我就可以自己编程了(为了好玩:D)Javascript 如何获取youtube视频的直接url,javascript,download,youtube,Javascript,Download,Youtube,目前所有关于制作自己的youtube视频下载程序的教程都过时了。从视频信息插入整个令牌不起作用,使用javascript方法也不起作用。有没有人知道目前有什么方法可以不用下载别人的程序就可以做到这一点?我只想知道该怎么做,这样我就可以自己编程了(为了好玩:D) 提前谢谢 你为什么要重新发明轮子?使用Rapidleech脚本 顺便说一句,如果你还需要重新发明轮子,这里是Rapidleech的Youtube插件的源代码(可读性很强: class youtube\u com扩展了DownloadCl
提前谢谢 你为什么要重新发明轮子?使用Rapidleech脚本 顺便说一句,如果你还需要重新发明轮子,这里是Rapidleech的Youtube插件的源代码(可读性很强:
class youtube\u com扩展了DownloadClass{
/*
一些关于年龄验证和错误URL的废话
$fmt是质量/格式编号,是一个整数
*/
公共功能下载($link){
$this->fmts=数组(38,37,22,45,35,44,34,43,18,5,17);
$yt_fmt=空($_请求['yt_fmt'])?'':$_请求['yt_fmt'];
$this->fmturlmaps=$this->GetVideosArr($fmt\u url\u映射);
if(空($yt_fmt)和&!isset($_GET[“audl”])返回$this->QSelector($link);
elseif(isset($U请求['ytube U mp4'])和&$U请求['ytube U mp4']=='on'&&&!empty($yt_fmt)){
//寻找并下载我们能找到的最高质量的?
如果($yt_fmt==‘最高’){
foreach($this->fmts作为$fmt){
如果(数组\键\存在($fmt,$this->fmturlmaps)){
$furl=$this->fmturlmap[$fmt];
打破
}
}
}else{//获取用户指定的格式(确保它实际存在)
如果(!$furl=$this->fmturlmaps[$yt_fmt])html_错误(“未找到指定的视频格式”);
$fmt=$yt_fmt;
}
}否则{//只需获取默认情况下Youtube播放的内容(在某些情况下,它也可能是最高质量的格式)
$fmt=key($this->fmturlmap);
$furl=$this->fmturlmap[$fmt];
}
如果(preg|u match(“^5 | 34 | 35$%”,$fmt))$ext='.flv';
elseif(preg_match(“^17$%”,$fmt))$ext='.3gp';
elseif(preg|u match(“^18 | 22 | 37 | 38$%”,$fmt))$ext='.mp4';
elseif(preg|u match(“^43 | 44 | 45$%”,$fmt))$ext='.webm';
else$ext='.flv';
如果(!preg_match('#(.*)\s+-\sYouTube[\r|\n |\t|\s]*#Us',$this->page,$title))html_错误('未找到视频标题!下载已停止');
如果(!preg_match('/video_id=(.+?)(\\\\\\\\\\\\\\\”&\\\\\\\\(\\\u0026))/',$this->page,$video_id))html_错误('video id not found');
$FileName=str\u replace(数组(“\\”、“/”、“:”、“*”、“?”、“\”、“、”、“|”)、“\uhtml\u实体解码(trim($title[1])、ENT\u引号)”。“-[{$video\u id[1]}][f$fmt]$ext”;
如果(stristr($full,“|”)){
$u_arr=爆炸(“|”,$furl);
$furl=preg_replace(“#:/([^/]+)#,”:/“$u arr[2],$u arr[0]);
}
如果(isset($_请求['ytdirect'])&&&$_请求['ytdirect']=='on')
{
回声“
”;
回声“;
}
其他的
{
$this->RedirectDownload(urldecode($furl),$FileName,$this->cookie,0,0,$FileName);
}
}
从今天起,2012年6月15日确实很简单;但你必须注意未来的变化。
在这里(用Javascript)
试试看{
var url=document.body.innerHTML.match(/“url\u encoded\u fmt\u stream\u map”:“url=([^”]+)/)[1]
URL=decodeURIComponent(URL)。替换(/\\u0026/g,“&”)
url=url。替换(/&quality.+?(?=,url)/g',);;
url=url.split(',url='))
//URL是一个包含所有可能特性的数组
//要下载一个,您可以使用以下内容:
//document.location=URL[0]
//第一种通常是最高质量的
}捕获(e){
console.error(“Youtube可能已更改其API”)
}
从2018年1月1日起,下面描述的旧技术似乎无法可靠地工作。Youtube的页面内变量结构似乎发生了变化,我无法确定更新版本。
这是一个更新的(2017年6月)版本,可直接从任何浏览器中提供的开发工具控制台工作。当前应适用于任何Youtube媒体类型,包括4k(如果可用)
样本输出
我并不完全肯定它仍然有效,但它非常易于分析(作为一个Python脚本)。使用HTML5视频播放器,这将非常简单。只需查找
视频
标签并检查其来源。我很高兴今天有人能为此提供一个可行的解决方案。我想你正在寻找一个类似youtube的东西。2tera.com于2012年12月17日试用了它,但它不起作用。YT最近改变了它的外观……它是sti在那里,只有url=
改变了位置…需要Regex专家:)@lukejacksonn不在普通浏览器中。任何加载到iframe中的站点(本例中为youtube.com)是沙盒。这是为了防止跨站点脚本等安全问题。因此,基本上,您对iframe中页面的javascript环境几乎没有访问权限。您必须将自己的脚本注入iframe页面,以便能够跨站点进行通信。您不能在youtube.com上这样做,因为您无法访问mobile Safari中的开发工具,除非您在桌面Safari及其开发工具打开的情况下通过usb电缆连接到Mac。在这种情况下,它将工作。@Anthony这实际上是由于@Mohammaddayan指出4k视频未列出。我已经对js对象有点熟悉,重新检查它让我找到adaptive\u fmts
。答案与第一条评论相同。除非您正在将youtube加载到第三方浏览器(如WKWebKit或UIWebKit)中,您可以在其中插入javascript,或者如果您正在开发允许您执行相同操作的浏览器扩展,否则您只能从控制台访问ytplayer
对象。或者如果您刮取页面,
class youtube_com extends DownloadClass {
/*
Some blah blah about the age verification and erroneous URLs
$fmt is quality/format number and is an integer
*/
public function Download($link) {
$this->fmts = array(38,37,22,45,35,44,34,43,18,5,17);
$yt_fmt = empty($_REQUEST['yt_fmt']) ? '' : $_REQUEST['yt_fmt'];
$this->fmturlmaps = $this->GetVideosArr($fmt_url_maps);
if (empty($yt_fmt) && !isset($_GET["audl"])) return $this->QSelector($link);
elseif (isset($_REQUEST['ytube_mp4']) && $_REQUEST['ytube_mp4'] == 'on' && !empty($yt_fmt)) {
//look for and download the highest quality we can find?
if ($yt_fmt == 'highest') {
foreach ($this->fmts as $fmt) {
if (array_key_exists($fmt, $this->fmturlmaps)) {
$furl = $this->fmturlmaps[$fmt];
break;
}
}
} else { //get the format the user specified (making sure it actually exists)
if (!$furl = $this->fmturlmaps[$yt_fmt]) html_error ('Specified video format not found');
$fmt = $yt_fmt;
}
} else { //just get the one Youtube plays by default (in some cases it could also be the highest quality format)
$fmt = key($this->fmturlmaps);
$furl = $this->fmturlmaps[$fmt];
}
if (preg_match ('%^5|34|35$%', $fmt)) $ext = '.flv';
elseif (preg_match ('%^17$%', $fmt)) $ext = '.3gp';
elseif (preg_match ('%^18|22|37|38$%', $fmt)) $ext = '.mp4';
elseif (preg_match ('%^43|44|45$%', $fmt)) $ext = '.webm';
else $ext = '.flv';
if (!preg_match('#<title>(.*)\s+-\sYouTube[\r|\n|\t|\s]*</title>#Us', $this->page, $title)) html_error('No video title found! Download halted.');
if (!preg_match ('/video_id=(.+?)(\\\|"|&|(\\\u0026))/', $this->page, $video_id)) html_error('Video id not found.');
$FileName = str_replace (Array ("\\", "/", ":", "*", "?", "\"", "<", ">", "|"), "_", html_entity_decode(trim($title[1]), ENT_QUOTES)) . "-[{$video_id[1]}][f$fmt]$ext";
if (stristr($furl, '|')) {
$u_arr = explode('|', $furl);
$furl = preg_replace('#://([^/]+)#', "://".$u_arr[2], $u_arr[0]);
}
if (isset($_REQUEST['ytdirect']) && $_REQUEST['ytdirect'] == 'on')
{
echo "<br /><br /><h4><a style='color:yellow' href='" . urldecode($furl) . "'>Click here or copy the link to your download manager to download</a></h4>";
echo "<input name='dlurl' style='width: 1000px; border: 1px solid #55AAFF; background-color: #FFFFFF; padding:3px' value='" . urldecode($furl) . "' onclick='javascript:this.select();' readonly></input>";
}
else
{
$this->RedirectDownload (urldecode($furl), $FileName, $this->cookie, 0, 0, $FileName);
}
}
// ES6 version
const videoUrls = ytplayer.config.args.adaptive_fmts
.split(',')
.map(item => item
.split('&')
.reduce((prev, curr) => (curr = curr.split('='),
Object.assign(prev, {[curr[0]]: decodeURIComponent(curr[1])})
), {})
)
.reduce((prev, curr) => Object.assign(prev, {
[curr.quality_label || curr.type]: curr
}), {});
console.log(videoUrls);
// ES5 version
var videoUrls = ytplayer.config.args.adaptive_fmts
.split(',')
.map(function (item) {
return item
.split('&')
.reduce(function (prev, curr) {
curr = curr.split('=');
return Object.assign(prev, {[curr[0]]: decodeURIComponent(curr[1])})
}, {});
})
.reduce(function (prev, curr) {
return Object.assign(prev, {
[curr.quality_label || curr.type]: curr
});
}, {});
console.log(videoUrls);
{
"1080p": {
"itag": "248",
"xtags": "",
"lmt": "1440215955569849",
"url": "https://r16---sn-n4v7kn7r.c.youtube.com/videoplayback?itag=248&keepalive=ye…2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csc%2Csource%2Cexpire&mt=1498245944",
"type": "video/webm;+codecs=\"vp9\"",
"bitrate": "1733724",
"index": "243-1977",
"size": "1920x1080",
"projection_type": "1",
"fps": "30",
"clen": "31192903",
"init": "0-242",
"quality_label": "1080p"
},
"720p": {
"itag": "247",
"xtags": "",
"lmt": "1440215905109639",
"url": "https://r16---sn-n4v7kn7r.c.youtube.com/videoplayback?itag=247&keepalive=ye…2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csc%2Csource%2Cexpire&mt=1498245944",
"type": "video/webm;+codecs=\"vp9\"",
"bitrate": "726076",
"index": "243-1933",
"size": "1280x720",
"projection_type": "1",
"fps": "30",
"clen": "15801933",
"init": "0-242",
"quality_label": "720p"
},
"480p": {
"itag": "244",
"xtags": "",
"lmt": "1440215890236689",
"url": "https://r16---sn-n4v7kn7r.c.youtube.com/videoplayback?itag=244&keepalive=ye…2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csc%2Csource%2Cexpire&mt=1498245944",
"type": "video/webm;+codecs=\"vp9\"",
"bitrate": "396541",
"index": "243-1933",
"size": "854x480",
"projection_type": "1",
"fps": "30",
"clen": "7928237",
"init": "0-242",
"quality_label": "480p"
},
"360p": {
"itag": "243",
"xtags": "",
"lmt": "1440215888783441",
"url": "https://r16---sn-n4v7kn7r.c.youtube.com/videoplayback?itag=243&keepalive=ye…2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csc%2Csource%2Cexpire&mt=1498245944",
"type": "video/webm;+codecs=\"vp9\"",
"bitrate": "223695",
"index": "243-1933",
"size": "640x360",
"projection_type": "1",
"fps": "30",
"clen": "5127362",
"init": "0-242",
"quality_label": "360p"
},
"240p": {
"itag": "242",
"xtags": "",
"lmt": "1440215900971640",
"url": "https://r16---sn-n4v7kn7r.c.youtube.com/videoplayback?itag=242&keepalive=ye…2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csc%2Csource%2Cexpire&mt=1498245944",
"type": "video/webm;+codecs=\"vp9\"",
"bitrate": "113952",
"index": "242-1931",
"size": "426x240",
"projection_type": "1",
"fps": "30",
"clen": "2597162",
"init": "0-241",
"quality_label": "240p"
},
"144p": {
"itag": "278",
"xtags": "",
"lmt": "1440215900119192",
"url": "https://r16---sn-n4v7kn7r.c.youtube.com/videoplayback?itag=278&keepalive=ye…2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csc%2Csource%2Cexpire&mt=1498245944",
"type": "video/webm;+codecs=\"vp9\"",
"bitrate": "60303",
"index": "242-1930",
"size": "256x144",
"projection_type": "1",
"fps": "15",
"clen": "1798744",
"init": "0-241",
"quality_label": "144p"
},
"audio/mp4;+codecs=\"mp4a.40.2\"": {
"bitrate": "128266",
"itag": "140",
"xtags": "",
"lmt": "1440578358539132",
"index": "592-1271",
"clen": "8482615",
"projection_type": "1",
"url": "https://r16---sn-n4v7kn7r.c.youtube.com/videoplayback?itag=140&keepalive=ye…2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csc%2Csource%2Cexpire&mt=1498245944",
"type": "audio/mp4;+codecs=\"mp4a.40.2\"",
"init": "0-591"
},
"audio/webm;+codecs=\"vorbis\"": {
"bitrate": "118499",
"itag": "171",
"xtags": "",
"lmt": "1440215938192462",
"index": "4452-5366",
"clen": "6383456",
"projection_type": "1",
"url": "https://r16---sn-n4v7kn7r.c.youtube.com/videoplayback?itag=171&keepalive=ye…2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csc%2Csource%2Cexpire&mt=1498245944",
"type": "audio/webm;+codecs=\"vorbis\"",
"init": "0-4451"
},
"audio/webm;+codecs=\"opus\"": {
"bitrate": "154966",
"itag": "251",
"xtags": "",
"lmt": "1440215889283443",
"index": "272-1186",
"clen": "9526605",
"projection_type": "1",
"url": "https://r16---sn-n4v7kn7r.c.youtube.com/videoplayback?itag=251&keepalive=ye…2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csc%2Csource%2Cexpire&mt=1498245944",
"type": "audio/webm;+codecs=\"opus\"",
"init": "0-271"
}
}