Javascript 如何从 url 获取 youtube 视频 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10591547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get youtube video id from url
提问by xorinzor
I am trying to check whether a url is a valid youtube video URL and get the youtube video ID from it, so far I am using a simple javascript split function in order to achieve this, however this has some minor disadvantages as youtube has multiple URL's.
我正在尝试检查一个 url 是否是一个有效的 youtube 视频 URL 并从中获取 youtube 视频 ID,到目前为止我正在使用一个简单的 javascript 拆分函数来实现这一点,但是这有一些小缺点,因为 youtube 有多个 URL .
I have been viewing other stackoverflow threads however all of them only support 1 specific URL which is not what I need.
我一直在查看其他 stackoverflow 线程,但是它们都只支持 1 个特定的 URL,这不是我需要的。
I need something that matches all these URL's:
我需要匹配所有这些 URL 的东西:
http(s)://www.youtu.be/videoID
http(s)://www.youtube.com/watch?v=videoID
http(s)://www.youtu.be/videoID
http(s)://www.youtube.com/watch?v=videoID
(and optionally any other short URL's which the script automatically detects whether it contains a youtube video)
(以及可选的任何其他短 URL,脚本会自动检测它是否包含 youtube 视频)
Any ideas which can be handled by the browser quick/efficient is greatly appreciated!
非常感谢浏览器可以快速/高效地处理的任何想法!
回答by The Mask
Try this:
尝试这个:
var url = "...";
var videoid = url.match(/(?:https?:\/{2})?(?:w{3}\.)?youtu(?:be)?\.(?:com|be)(?:\/watch\?v=|\/)([^\s&]+)/);
if(videoid != null) {
console.log("video id = ",videoid[1]);
} else {
console.log("The youtube url is not valid.");
}
see regex:
见正则表达式:
/
(?:https?:\/{2})? // Optional protocol, if have, must be http:// or https://
(?:w{3}\.)? // Optional sub-domain, if have, must be www.
youtu(?:be)? // The domain. Match 'youtu' and optionally 'be'.
\.(?:com|be) // the domain-extension must be .com or .be
(?:\/watch\?v=|\/)([^\s&]+) //match the value of 'v' parameter in querystring from 'watch' directory OR after root directory, any non-space value.
/
回答by edocetirwi
Maybe you should look at the Youtube API and try to see if there is a way to get a videoID by parsing the URL though the API.
也许您应该查看 Youtube API 并尝试查看是否有办法通过 API 解析 URL 来获取 videoID。
Look at this SO post: Youtube API - Extract video ID
看看这个 SO 帖子: Youtube API - Extract video ID
回答by frosty
This could be quick:
这可能很快:
var url = 'http://www.youtu.be/543221';
//http://www.youtube.com/watch?v=SNfYz6Yw0W8&feature=g-all-esi would work also
var a = url.split("v=")[1];
a = a != undefined ? a : url.split("youtu.be/")[1];
b = a.split("&")[0];
the variable c will have your id. Quick. The regex is nicer... harder to read though. I have modified my code to account for both.
变量 c 将包含您的 ID。快的。正则表达式更好......虽然更难阅读。我已经修改了我的代码以解决这两个问题。
回答by Tony
There are too many kind:
种类太多了:
- latest short format:
http://youtu.be/NLqAF9hrVbY
- iframe:
http://www.youtube.com/embed/NLqAF9hrVbY
- iframe (secure):
https://www.youtube.com/embed/NLqAF9hrVbY
- object param:
http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US
- object embed:
http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US
- watch:
http://www.youtube.com/watch?v=NLqAF9hrVbY
- users:
http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo
- ytscreeningroom:
http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I
- any/thing/goes!:
http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/2/PPS-8DMrAn4
- any/subdomain/too:
http://gdata.youtube.com/feeds/api/videos/NLqAF9hrVbY
- more params:
http://www.youtube.com/watch?v=spDj54kf-vY&feature=g-vrec
- query may have dot:
http://www.youtube.com/watch?v=spDj54kf-vY&feature=youtu.be
(Source: How do I find all YouTube video ids in a string using a regex?)
- 最新的短格式:
http://youtu.be/NLqAF9hrVbY
- 框架:
http://www.youtube.com/embed/NLqAF9hrVbY
- iframe(安全):
https://www.youtube.com/embed/NLqAF9hrVbY
- 对象参数:
http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US
- 对象嵌入:
http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US
- 手表:
http://www.youtube.com/watch?v=NLqAF9hrVbY
- 用户:
http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo
- ytscreeningroom:
http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I
- 什么都可以!:
http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/2/PPS-8DMrAn4
- 任何/子域/太:
http://gdata.youtube.com/feeds/api/videos/NLqAF9hrVbY
- 更多参数:
http://www.youtube.com/watch?v=spDj54kf-vY&feature=g-vrec
- 查询可能有点:(
http://www.youtube.com/watch?v=spDj54kf-vY&feature=youtu.be
来源:How do I find all YouTube video ids in a string using a regex?)
The best way is limiting input-data.
最好的方法是限制输入数据。
Good luck
祝你好运
回答by The Finest Artist
try this code
试试这个代码
var url = "...";
var videoid = url.match((?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11}));
if(videoid != null) {
console.log("video id = ",videoid[1]);
} else {
console.log("The youtube url is not valid.");
}
The Regex is from YouTube video ID regex
正则表达式来自 YouTube 视频 ID 正则表达式
回答by bl?mp?
you can do it easily using preg_match here is the example:$url = "http://www.youtube.com/watch?v=YzOt12co4nk&feature=g-vrec";
preg_match('/v=([0-9a-zA-Z]+)/', $url, $matches);
$vid = $matches[1];
Now you will have the video id as: $vid = YzOt12co4nk;
您可以使用 preg_match 轻松完成这里的示例:$url = "http://www.youtube.com/watch?v=YzOt12co4nk&feature=g-vrec";
preg_match('/v=([0-9a-zA-Z]+)/', $url, $matches);
$vid = $matches[1];
现在您将拥有视频 ID: $vid = YzOt12co4nk;